diff --git a/CHANGELOG.md b/CHANGELOG.md index 77dbc93912c2..31fc65468fd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixes - `[jest-runner, jest-runtime]` fix: `require.main` undefined with `createRequire()` ([#10610](https://github.com/facebook/jest/pull/10610)) +- `[jest-runtime]` add missing `module.path` property ([#10615](https://github.com/facebook/jest/pull/10615)) - `[jest-validate]` Show suggestion only when unrecognized cli param is longer than 1 character ([#10604](https://github.com/facebook/jest/pull/10604)) - `[jest-validate]` Validate `testURL` as CLI option ([#10595](https://github.com/facebook/jest/pull/10595)) diff --git a/packages/jest-runtime/src/__tests__/runtime_require_module.test.js b/packages/jest-runtime/src/__tests__/runtime_require_module.test.js index 81901ceed0be..7a9702898ebb 100644 --- a/packages/jest-runtime/src/__tests__/runtime_require_module.test.js +++ b/packages/jest-runtime/src/__tests__/runtime_require_module.test.js @@ -42,6 +42,7 @@ describe('Runtime requireModule', () => { 'filename', 'id', 'loaded', + 'path', 'parent', 'paths', ]); @@ -59,6 +60,7 @@ describe('Runtime requireModule', () => { 'filename', 'id', 'loaded', + 'path', 'parent', 'paths', ]); diff --git a/packages/jest-runtime/src/__tests__/test_root/RegularModule.js b/packages/jest-runtime/src/__tests__/test_root/RegularModule.js index 270ee52b541d..3efd32c093d3 100644 --- a/packages/jest-runtime/src/__tests__/test_root/RegularModule.js +++ b/packages/jest-runtime/src/__tests__/test_root/RegularModule.js @@ -36,6 +36,7 @@ exports.jest = jest; exports.lazyRequire = lazyRequire; exports.object = {}; exports.parent = module.parent; +exports.path = module.path; exports.paths = module.paths; exports.setModuleStateValue = setModuleStateValue; exports.module = module; diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index f079120bd20f..166c32671e2b 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -83,8 +83,7 @@ const defaultTransformOptions: InternalModuleOptions = { supportsStaticESM: false, }; -type InitialModule = Partial & - Pick; +type InitialModule = Omit; type ModuleRegistry = Map; const OUTSIDE_JEST_VM_RESOLVE_OPTION = Symbol.for( @@ -546,6 +545,7 @@ class Runtime { filename: modulePath, id: modulePath, loaded: false, + path: path.dirname(modulePath), }; moduleRegistry.set(modulePath, localModule); @@ -646,6 +646,7 @@ class Runtime { filename: modulePath, id: modulePath, loaded: false, + path: path.dirname(modulePath), }; this._loadModule( @@ -981,16 +982,17 @@ class Runtime { return; } - const filename = localModule.filename; + const module = localModule as Module; + + const filename = module.filename; const lastExecutingModulePath = this._currentlyExecutingModulePath; this._currentlyExecutingModulePath = filename; const origCurrExecutingManualMock = this._isCurrentlyExecutingManualMock; this._isCurrentlyExecutingManualMock = filename; - const dirname = path.dirname(filename); - localModule.children = []; + module.children = []; - Object.defineProperty(localModule, 'parent', { + Object.defineProperty(module, 'parent', { enumerable: true, get() { const key = from || ''; @@ -998,9 +1000,9 @@ class Runtime { }, }); - localModule.paths = this._resolver.getModulePaths(dirname); - Object.defineProperty(localModule, 'require', { - value: this._createRequireImplementation(localModule, options), + module.paths = this._resolver.getModulePaths(module.path); + Object.defineProperty(module, 'require', { + value: this._createRequireImplementation(module, options), }); const transformedCode = this.transformFile(filename, options); @@ -1053,17 +1055,17 @@ class Runtime { try { compiledFunction.call( - localModule.exports, - localModule as NodeModule, // module object - localModule.exports, // module exports - localModule.require as typeof require, // require implementation - dirname, // __dirname - filename, // __filename + module.exports, + module, // module object + module.exports, // module exports + module.require, // require implementation + module.path, // __dirname + module.filename, // __filename this._environment.global, // global object ...lastArgs.filter(notEmpty), ); } catch (error) { - this.handleExecutionError(error, localModule); + this.handleExecutionError(error, module); } this._isCurrentlyExecutingManualMock = origCurrExecutingManualMock; @@ -1168,6 +1170,7 @@ class Runtime { filename, id: filename, loaded: false, + path: path.dirname(filename), }); }; @@ -1606,7 +1609,7 @@ class Runtime { ].filter(notEmpty); } - private handleExecutionError(e: Error, module: InitialModule): never { + private handleExecutionError(e: Error, module: Module): never { const moduleNotFoundError = Resolver.tryCastModuleNotFoundError(e); if (moduleNotFoundError) { if (!moduleNotFoundError.requireStack) {