Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(jest-environment) - update types and type usage in jest-runtime #8439

Merged
merged 2 commits into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ export type EnvironmentContext = Partial<{
testPath: Config.Path;
}>;

// TODO: type this better: https://nodejs.org/api/modules.html#modules_the_module_wrapper
type ModuleWrapper = (...args: Array<unknown>) => unknown;
// Different Order than https://nodejs.org/api/modules.html#modules_the_module_wrapper , however needs to be in the form [jest-transform]ScriptTransformer accepts
export type ModuleWrapper = (
module: Module,
exports: Module['exports'],
require: Module['require'],
__dirname: string,
__filename: Module['filename'],
global: Global.Global,
jest: Jest,
...extraGlobals: Array<any>
JoshRosenstein marked this conversation as resolved.
Show resolved Hide resolved
) => unknown;

export declare class JestEnvironment {
constructor(config: Config.ProjectConfig, context?: EnvironmentContext);
Expand All @@ -38,7 +47,7 @@ export declare class JestEnvironment {
handleTestEvent?(event: Circus.Event, state: Circus.State): void;
}

export type Module = typeof module;
export type Module = NodeModule;

export interface LocalModuleRequire extends NodeRequire {
requireActual(moduleName: string): unknown;
Expand Down
12 changes: 7 additions & 5 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -744,12 +744,15 @@ class Runtime {
}

const wrapper = runScript[ScriptTransformer.EVAL_RESULT_VARIABLE];
const moduleArguments = new Set([
localModule, // module object
wrapper.call(
localModule.exports,
localModule as NodeModule, // module object
localModule.exports, // module exports
localModule.require, // require implementation
localModule.require as NodeRequireFunction, // require implementation
dirname, // __dirname

filename, // __filename

this._environment.global, // global object
this._createJestObjectFor(
filename,
Expand All @@ -764,8 +767,7 @@ class Runtime {
`You have requested '${globalVariable}' as a global variable, but it was not present. Please check your config or your global environment.`,
);
}),
]);
wrapper.call(localModule.exports, ...Array.from(moduleArguments));
);

this._isCurrentlyExecutingManualMock = origCurrExecutingManualMock;
this._currentlyExecutingModulePath = lastExecutingModulePath;
Expand Down