Skip to content

Commit

Permalink
fix(jest-resolve): usage of node: imports for builtin modules
Browse files Browse the repository at this point in the history
  • Loading branch information
theoludwig committed Jul 24, 2021
1 parent 7a64ede commit 7cea174
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-resolve]` Allow usage of `node:` URLs to load Node.js builtin modules. ([#11638](https://github.com/facebook/jest/pull/11638))

### Chore & Maintenance

### Performance
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:558:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:561: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:558:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:561:17)
at Object.require (index.js:10:1)
`;
14 changes: 14 additions & 0 deletions packages/jest-resolve/src/__tests__/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ describe('isCoreModule', () => {
const isCore = resolver.isCoreModule('constants');
expect(isCore).toEqual(false);
});

it('returns true if using `node:` URLs and `moduleName` is a core module.', () => {
const moduleMap = ModuleMap.create('/');
const resolver = new Resolver(moduleMap, {} as ResolverConfig);
const isCore = resolver.isCoreModule('node:assert');
expect(isCore).toEqual(true);
});

it('returns false if using `node:` URLs and `moduleName` is not a core module.', () => {
const moduleMap = ModuleMap.create('/');
const resolver = new Resolver(moduleMap, {} as ResolverConfig);
const isCore = resolver.isCoreModule('node:not-a-core-module');
expect(isCore).toEqual(false);
});
});

describe('findNodeModule', () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-resolve/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ export default class Resolver {
}

isCoreModule(moduleName: string): boolean {
moduleName = moduleName.startsWith('node:')
? moduleName.slice(5)
: moduleName;
return (
this._options.hasCoreModules &&
isBuiltinModule(moduleName) &&
Expand Down

0 comments on commit 7cea174

Please sign in to comment.