From db5e472b51cfc68787c9da7bb21b26f192b0ab78 Mon Sep 17 00:00:00 2001 From: Josh Rosenstein Date: Wed, 8 May 2019 14:45:30 -0400 Subject: [PATCH] chore(jest-environment) - update types and type usage in jest-runtime (#8439) --- packages/jest-environment/src/index.ts | 15 ++++++++++++--- packages/jest-runtime/src/index.ts | 12 ++++++------ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index 605227977271..4cb1e90bdf0d 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -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; +// 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 +) => unknown; export declare class JestEnvironment { constructor(config: Config.ProjectConfig, context?: EnvironmentContext); @@ -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; diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 36107a5d68ce..62553cfc0e52 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -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 @@ -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;