Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: absolute path moduleNameMapper + jest.mock issue #8727

Merged
merged 1 commit into from May 4, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@
- `[jest-config, jest-resolve]` [**BREAKING**] Remove support for `browser` field ([#9943](https://github.com/facebook/jest/pull/9943))
- `[jest-haste-map]` Stop reporting files as changed when they are only accessed ([#7347](https://github.com/facebook/jest/pull/7347))
- `[jest-resolve]` Show relative path from root dir for `module not found` errors ([#9963](https://github.com/facebook/jest/pull/9963))
- `[jest-runtime]` Fix absolute path moduleNameMapper + jest.mock bug ([#8727](https://github.com/facebook/jest/pull/8727))

### Chore & Maintenance

Expand Down
5 changes: 5 additions & 0 deletions e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap
Expand Up @@ -5,6 +5,11 @@ PASS __tests__/index.js
✓ moduleNameMapping correct configuration
`;

exports[`moduleNameMapper correct configuration mocking module of absolute path 1`] = `
PASS __tests__/index.js
✓ moduleNameMapping correct configuration
`;

exports[`moduleNameMapper wrong array configuration 1`] = `
FAIL __tests__/index.js
● Test suite failed to run
Expand Down
14 changes: 14 additions & 0 deletions e2e/__tests__/moduleNameMapper.test.ts
Expand Up @@ -35,6 +35,20 @@ test('moduleNameMapper correct configuration', () => {
expect(wrap(rest)).toMatchSnapshot();
});

test('moduleNameMapper correct configuration mocking module of absolute path', () => {
const {stderr, exitCode} = runJest(
'module-name-mapper-correct-mock-absolute-path',
[],
{
stripAnsi: true,
},
);
const {rest} = extractSummary(stderr);

expect(exitCode).toBe(0);
expect(wrap(rest)).toMatchSnapshot();
});

test('moduleNameMapper with mocking', () => {
const {json} = runWithJson('module-name-mapper-mock');
expect(json.numTotalTests).toBe(2);
Expand Down
@@ -0,0 +1,16 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

const importedFn = require('../');

jest.mock('/components/Button');

test('moduleNameMapping correct configuration', () => {
expect(importedFn).toBeDefined();
});
12 changes: 12 additions & 0 deletions e2e/module-name-mapper-correct-mock-absolute-path/index.js
@@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

require('/components/Button');

module.exports = () => 'test';
@@ -0,0 +1,7 @@
{
"jest": {
"moduleNameMapper": {
"^/(.*)$": "<rootDir>/src/$1"
}
}
}
@@ -0,0 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = () => 'Button';
10 changes: 4 additions & 6 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -584,12 +584,10 @@ class Runtime {
}

const manualMockOrStub = this._resolver.getMockModule(from, moduleName);
let modulePath;
if (manualMockOrStub) {
modulePath = this._resolveModule(from, manualMockOrStub);
} else {
modulePath = this._resolveModule(from, moduleName);
}

let modulePath =
this._resolver.getMockModule(from, moduleName) ||
this._resolveModule(from, moduleName);

let isManualMock =
manualMockOrStub &&
Expand Down