Skip to content

Commit

Permalink
fix: add missing module.path field to modules (#10615)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 11, 2020
1 parent acd7c83 commit 6f85827
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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))

Expand Down
Expand Up @@ -42,6 +42,7 @@ describe('Runtime requireModule', () => {
'filename',
'id',
'loaded',
'path',
'parent',
'paths',
]);
Expand All @@ -59,6 +60,7 @@ describe('Runtime requireModule', () => {
'filename',
'id',
'loaded',
'path',
'parent',
'paths',
]);
Expand Down
Expand Up @@ -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;
Expand Down
37 changes: 20 additions & 17 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -83,8 +83,7 @@ const defaultTransformOptions: InternalModuleOptions = {
supportsStaticESM: false,
};

type InitialModule = Partial<Module> &
Pick<Module, 'children' | 'exports' | 'filename' | 'id' | 'loaded'>;
type InitialModule = Omit<Module, 'require' | 'parent' | 'paths'>;
type ModuleRegistry = Map<string, InitialModule | Module>;

const OUTSIDE_JEST_VM_RESOLVE_OPTION = Symbol.for(
Expand Down Expand Up @@ -546,6 +545,7 @@ class Runtime {
filename: modulePath,
id: modulePath,
loaded: false,
path: path.dirname(modulePath),
};
moduleRegistry.set(modulePath, localModule);

Expand Down Expand Up @@ -646,6 +646,7 @@ class Runtime {
filename: modulePath,
id: modulePath,
loaded: false,
path: path.dirname(modulePath),
};

this._loadModule(
Expand Down Expand Up @@ -981,26 +982,27 @@ 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 || '';
return moduleRegistry.get(key) || null;
},
});

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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1168,6 +1170,7 @@ class Runtime {
filename,
id: filename,
loaded: false,
path: path.dirname(filename),
});
};

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6f85827

Please sign in to comment.