Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
- `[jest-runtime]` Fix error when `require()` is called after the Jest environment has been torn down ([#15951](https://github.com/jestjs/jest/pull/15951))
- `[jest-runtime]` Fix missing error when `import()` is called after the Jest environment has been torn down ([#16080](https://github.com/jestjs/jest/pull/16080))
- `[jest-runtime]` Fix virtual `unstable_mockModule` registrations not respected in ESM ([#16081](https://github.com/jestjs/jest/pull/16081))
- `[jest-runtime]` Apply `moduleNameMapper` when resolving modules with `require.resolve()` and the `paths` option ([#16135](https://github.com/jestjs/jest/pull/16135))

### Chore & Maintenance

Expand Down
6 changes: 6 additions & 0 deletions e2e/resolve-with-paths/__tests__/resolveWithPaths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ test('throws module not found error if the module cannot be found from given pat
expect.objectContaining({code: 'MODULE_NOT_FOUND'}),
);
});

test('resolves moduleNameMapper with paths option', () => {
expect(require.resolve('@foo/js', {paths: [__dirname]})).toBe(
require.resolve('../js/index.js'),
);
});
8 changes: 8 additions & 0 deletions e2e/resolve-with-paths/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = 'mapped module';
5 changes: 4 additions & 1 deletion e2e/resolve-with-paths/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"jest": {
"testEnvironment": "node"
"testEnvironment": "node",
"moduleNameMapper": {
"^@foo/js$": "<rootDir>/js/index.js"
}
}
}
20 changes: 20 additions & 0 deletions packages/jest-runtime/src/internals/__tests__/cjsRequire.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function makeResolution(
isCoreModule: jest.fn(() => false),
resolveCjs: jest.fn(),
resolveCjsFromDirIfExists: jest.fn(() => null),
resolveCjsStub: jest.fn(() => null),
...overrides,
} as unknown as Resolution;
}
Expand Down Expand Up @@ -170,6 +171,25 @@ describe('RequireBuilder', () => {
);
});

test('resolves moduleNameMapper before walking options.paths', () => {
const resolveCjsStub = jest.fn(
(_from: string, _moduleName: string) => '/mapped.js',
);
const resolveCjsFromDirIfExists = jest.fn(() => null);
const builder = makeBuilder({
resolution: makeResolution({
resolveCjsFromDirIfExists,
resolveCjsStub,
}),
});

expect(resolveVia(builder, '@foo/js', {paths: ['./a']})).toBe(
'/mapped.js',
);
expect(resolveCjsStub).toHaveBeenCalledWith('/a/b/from.js', '@foo/js');
expect(resolveCjsFromDirIfExists).not.toHaveBeenCalled();
});

test('falls back to mock module when resolveCjs throws', () => {
const builder = makeBuilder({
resolution: makeResolution({
Expand Down
8 changes: 8 additions & 0 deletions packages/jest-runtime/src/internals/cjsRequire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,22 @@ export class RequireBuilder {
return module;
}
} else if (options.paths) {
const module = this.resolution.resolveCjsStub(from, moduleName);

if (module) {
return module;
}

for (const searchPath of options.paths) {
const absolutePath = path.resolve(from, '..', searchPath);

// required to also resolve files without leading './' directly in the path
const module = this.resolution.resolveCjsFromDirIfExists(
absolutePath,
moduleName,
[absolutePath],
);

if (module) {
return module;
}
Expand Down
Loading