Skip to content

Commit

Permalink
fix: absolute path moduleNameMapper + jest.mock issue
Browse files Browse the repository at this point in the history
  • Loading branch information
chauchakching committed May 4, 2020
1 parent 968a301 commit 9bf438d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 6 deletions.
3 changes: 3 additions & 0 deletions e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ FAIL __tests__/index.js
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:553:17)
at Object.require (index.js:10:1)
exports[`moduleNameMapper correct configuration mocking module of absolute path 1`] = `
PASS __tests__/index.js
moduleNameMapping correct configuration
`;
exports[`moduleNameMapper wrong configuration 1`] = `
Expand Down
14 changes: 14 additions & 0 deletions e2e/__tests__/moduleNameMapper.test.ts
Original file line number Diff line number Diff line change
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, status} = runJest(
'module-name-mapper-correct-mock-absolute-path',
[],
{
stripAnsi: true,
},
);
const {rest} = extractSummary(stderr);

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

test('moduleNameMapper with mocking', () => {
const {json} = runWithJson('module-name-mapper-mock');
expect(json.numTotalTests).toBe(2);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"jest": {
"moduleNameMapper": {
"^/(.*)$": "<rootDir>/src/$1"
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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

0 comments on commit 9bf438d

Please sign in to comment.