Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add missing module.path field to modules #10615

Merged
merged 2 commits into from Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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> &
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Partial made this missing not be a type error

Pick<Module, 'children' | 'exports' | 'filename' | 'id' | 'loaded'>;
type InitialModule = Omit<Module, 'require' | 'parent' | 'paths'>;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might consider doing new Module and inspect what's on it to see what fields we should include, but meh

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