Skip to content

Commit 0a63d40

Browse files
fix: absolute path moduleNameMapper + jest.mock issue (#8727)
1 parent 03dbb2f commit 0a63d40

File tree

8 files changed

+67
-6
lines changed

8 files changed

+67
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- `[jest-config, jest-resolve]` [**BREAKING**] Remove support for `browser` field ([#9943](https://github.com/facebook/jest/pull/9943))
1717
- `[jest-haste-map]` Stop reporting files as changed when they are only accessed ([#7347](https://github.com/facebook/jest/pull/7347))
1818
- `[jest-resolve]` Show relative path from root dir for `module not found` errors ([#9963](https://github.com/facebook/jest/pull/9963))
19+
- `[jest-runtime]` Fix absolute path moduleNameMapper + jest.mock bug ([#8727](https://github.com/facebook/jest/pull/8727))
1920

2021
### Chore & Maintenance
2122

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ PASS __tests__/index.js
55
✓ moduleNameMapping correct configuration
66
`;
77

8+
exports[`moduleNameMapper correct configuration mocking module of absolute path 1`] = `
9+
PASS __tests__/index.js
10+
✓ moduleNameMapping correct configuration
11+
`;
12+
813
exports[`moduleNameMapper wrong array configuration 1`] = `
914
FAIL __tests__/index.js
1015
● Test suite failed to run

e2e/__tests__/moduleNameMapper.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ test('moduleNameMapper correct configuration', () => {
3535
expect(wrap(rest)).toMatchSnapshot();
3636
});
3737

38+
test('moduleNameMapper correct configuration mocking module of absolute path', () => {
39+
const {stderr, exitCode} = runJest(
40+
'module-name-mapper-correct-mock-absolute-path',
41+
[],
42+
{
43+
stripAnsi: true,
44+
},
45+
);
46+
const {rest} = extractSummary(stderr);
47+
48+
expect(exitCode).toBe(0);
49+
expect(wrap(rest)).toMatchSnapshot();
50+
});
51+
3852
test('moduleNameMapper with mocking', () => {
3953
const {json} = runWithJson('module-name-mapper-mock');
4054
expect(json.numTotalTests).toBe(2);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
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+
'use strict';
9+
10+
const importedFn = require('../');
11+
12+
jest.mock('/components/Button');
13+
14+
test('moduleNameMapping correct configuration', () => {
15+
expect(importedFn).toBeDefined();
16+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
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+
'use strict';
9+
10+
require('/components/Button');
11+
12+
module.exports = () => 'test';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"jest": {
3+
"moduleNameMapper": {
4+
"^/(.*)$": "<rootDir>/src/$1"
5+
}
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
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+
module.exports = () => 'Button';

packages/jest-runtime/src/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,10 @@ class Runtime {
584584
}
585585

586586
const manualMockOrStub = this._resolver.getMockModule(from, moduleName);
587-
let modulePath;
588-
if (manualMockOrStub) {
589-
modulePath = this._resolveModule(from, manualMockOrStub);
590-
} else {
591-
modulePath = this._resolveModule(from, moduleName);
592-
}
587+
588+
let modulePath =
589+
this._resolver.getMockModule(from, moduleName) ||
590+
this._resolveModule(from, moduleName);
593591

594592
let isManualMock =
595593
manualMockOrStub &&

0 commit comments

Comments
 (0)