Skip to content

Commit

Permalink
chore(jest-environment) - update types and type usage in jest-runtime (
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshRosenstein authored and SimenB committed May 8, 2019
1 parent 351abe8 commit db5e472
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
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<Global.Global[keyof Global.Global]>
) => 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: 6 additions & 6 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -743,11 +743,12 @@ class Runtime {
return;
}

const wrapper = runScript[ScriptTransformer.EVAL_RESULT_VARIABLE];
const moduleArguments = new Set([
localModule, // module object
//Wrapper
runScript[ScriptTransformer.EVAL_RESULT_VARIABLE].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
Expand All @@ -764,8 +765,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

0 comments on commit db5e472

Please sign in to comment.