Skip to content

Commit 781448a

Browse files
fix(jest-runtime, jest-resolver): Allow core models to be mocked with "node:" URL paths (#14297) (#14297)
Co-authored-by: Khaled Elmorsy <[email protected]>
1 parent 69f0c89 commit 781448a

File tree

12 files changed

+136
-33
lines changed

12 files changed

+136
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223

224224
### Fixes
225225

226+
- `[jest-runtime, jest-resolve]` Allow core modules to be mocked with the `node:` URL scheme ([#14297](https://github.com/jestjs/jest/pull/14297))
226227
- `[jest-circus]` Prevent false test failures caused by promise rejections handled asynchronously ([#14110](https://github.com/jestjs/jest/pull/14110))
227228
- `[jest-config]` Handle frozen config object ([#14054](https://github.com/facebook/jest/pull/14054))
228229
- `[jest-config]` Allow `coverageDirectory` and `collectCoverageFrom` in project config ([#14180](https://github.com/jestjs/jest/pull/14180))

e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ exports[`moduleNameMapper wrong array configuration 1`] = `
4141
12 | module.exports = () => 'test';
4242
13 |
4343
44-
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:1177:17)
44+
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:1184:17)
4545
at Object.require (index.js:10:1)
4646
at Object.require (__tests__/index.js:10:20)"
4747
`;
@@ -71,7 +71,7 @@ exports[`moduleNameMapper wrong configuration 1`] = `
7171
12 | module.exports = () => 'test';
7272
13 |
7373
74-
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:1177:17)
74+
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:1184:17)
7575
at Object.require (index.js:10:1)
7676
at Object.require (__tests__/index.js:10:20)"
7777
`;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import runJest from '../runJest';
9+
10+
test('supports node url manual mocks', () => {
11+
const result = runJest('node-url-manual-mocks');
12+
expect(result.exitCode).toBe(0);
13+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
const {mock} = require('../testUtils');
8+
console.log(mock);
9+
module.exports = mock;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
const fs = require('node:fs');
8+
const {expectModuleMocked} = require('../testUtils');
9+
10+
jest.mock('node:fs');
11+
12+
it('correctly mocks the module', () => {
13+
expectModuleMocked(fs);
14+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
const fs = require('node:fs');
8+
const {expectModuleMocked} = require('../testUtils');
9+
10+
jest.mock('fs');
11+
12+
it('correctly mocks the module', () => {
13+
expectModuleMocked(fs);
14+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
const fs = require('fs');
8+
const {expectModuleMocked} = require('../testUtils');
9+
10+
jest.mock('node:fs');
11+
12+
it('correctly mocks the module', () => {
13+
expectModuleMocked(fs);
14+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node"
4+
}
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
const mock = {};
8+
module.exports.mock = mock;
9+
10+
module.exports.expectModuleMocked = module => {
11+
// eslint-disable-next-line no-undef
12+
expect(module).toBe(mock);
13+
};

jest.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default {
3030
'packages/jest-runtime/src/__tests__/test_root.*',
3131
'website/.*',
3232
'e2e/runtime-internal-module-registry/__mocks__',
33+
'e2e/node-url-manual-mocks/__mocks__',
3334
],
3435
projects: ['<rootDir>', '<rootDir>/examples/*/'],
3536
snapshotFormat: {

0 commit comments

Comments
 (0)