Skip to content

Commit

Permalink
fix(resolver): support node prefix from ESM (#11817)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Sep 8, 2021
1 parent 831c3de commit b1ee6de
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Fixes

- `[jest-resolver]` Support `node:` prefix when importing Node core modules with ESM ([#11817](https://github.com/facebook/jest/pull/11817))
- `[jest-types]` Export the `PrettyFormatOptions` interface ([#11801](https://github.com/facebook/jest/pull/11801))

### Chore & Maintenance
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ FAIL __tests__/index.js
12 | module.exports = () => 'test';
13 |
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:561:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:566:17)
at Object.require (index.js:10:1)
`;

Expand Down Expand Up @@ -70,6 +70,6 @@ FAIL __tests__/index.js
12 | module.exports = () => 'test';
13 |
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:561:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:566:17)
at Object.require (index.js:10:1)
`;
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Ran all test suites matching /native-esm.tla.test.js/i.
exports[`on node ^12.16.0 || >=13.7.0 runs test with native ESM 1`] = `
Test Suites: 1 passed, 1 total
Tests: 20 passed, 20 total
Tests: 21 passed, 21 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /native-esm.test.js/i.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ FAIL __tests__/test.js
| ^
9 |
at Resolver.resolveModule (../../packages/jest-resolve/build/resolver.js:313:11)
at Resolver.resolveModule (../../packages/jest-resolve/build/resolver.js:318:11)
at Object.require (index.js:8:18)
`;
6 changes: 6 additions & 0 deletions e2e/native-esm/__tests__/native-esm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
* LICENSE file in the root directory of this source tree.
*/

import dns from 'dns';
// the point here is that it's the node core module
// eslint-disable-next-line no-restricted-imports
import {readFileSync} from 'fs';
import {createRequire} from 'module';
import {dirname, resolve} from 'path';
import {fileURLToPath} from 'url';
import prefixDns from 'node:dns';
import {jest as jestObject} from '@jest/globals';
import staticImportedStatefulFromCjs from '../fromCjs.mjs';
import {double} from '../index';
Expand Down Expand Up @@ -188,3 +190,7 @@ test('can mock module', async () => {
expect(Object.keys(importedMock)).toEqual(['foo']);
expect(importedMock.foo).toEqual('bar');
});

test('supports imports using "node:" prefix', () => {
expect(dns).toBe(prefixDns);
});
9 changes: 7 additions & 2 deletions packages/jest-resolve/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,12 @@ export default class Resolver {
const skipResolution =
options && options.skipNodeResolution && !moduleName.includes(path.sep);

const resolveNodeModule = (name: Config.Path, throwIfNotFound = false) =>
Resolver.findNodeModule(name, {
const resolveNodeModule = (name: Config.Path, throwIfNotFound = false) => {
if (this.isCoreModule(name)) {
return name;
}

return Resolver.findNodeModule(name, {
basedir: dirname,
extensions,
moduleDirectory,
Expand All @@ -186,6 +190,7 @@ export default class Resolver {
rootDir: this._options.rootDir,
throwIfNotFound,
});
};

if (!skipResolution) {
module = resolveNodeModule(moduleName, Boolean(process.versions.pnp));
Expand Down

0 comments on commit b1ee6de

Please sign in to comment.