diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index 2dd7fa911480..87951acb0530 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -27,6 +27,7 @@ export type EnvironmentContext = Partial<{ // 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 = ( + this: Module['exports'], module: Module, exports: Module['exports'], require: Module['require'], diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 74e2393091a9..c6c2b8150192 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -12,6 +12,7 @@ import { JestEnvironment, LocalModuleRequire, Module, + ModuleWrapper, } from '@jest/environment'; import {SourceMapRegistry} from '@jest/source-map'; import jestMock = require('jest-mock'); @@ -736,9 +737,7 @@ class Runtime { return; } - //Wrapper - runScript[ScriptTransformer.EVAL_RESULT_VARIABLE].call( - localModule.exports, + const args: Parameters = [ localModule as NodeModule, // module object localModule.exports, // module exports localModule.require as NodeRequireFunction, // require implementation @@ -758,6 +757,20 @@ class Runtime { `You have requested '${globalVariable}' as a global variable, but it was not present. Please check your config or your global environment.`, ); }), + ]; + + const injectedModuleArguments = this.constructInjectedModuleArguments(); + + if (injectedModuleArguments.length !== args.length) { + throw new ReferenceError( + `We are injecting ${args.length} arguments into module wrapper, expected ${injectedModuleArguments.length}.\n\nThis is a bug in Jest, please report it`, + ); + } + + //Wrapper + runScript[ScriptTransformer.EVAL_RESULT_VARIABLE].apply( + localModule.exports, + args, ); this._isCurrentlyExecutingManualMock = origCurrExecutingManualMock;