From ccfecbe68910bc4f315dc0eb752a0a2581547e16 Mon Sep 17 00:00:00 2001 From: vincentchauhk01 Date: Sun, 21 Jul 2019 12:44:20 +0800 Subject: [PATCH] fix: absolute path moduleNameMapper + jest.mock issue --- .../__snapshots__/moduleNameMapper.test.ts.snap | 5 +++++ e2e/__tests__/moduleNameMapper.test.ts | 14 ++++++++++++++ .../__tests__/index.js | 16 ++++++++++++++++ .../index.js | 12 ++++++++++++ .../package.json | 7 +++++++ .../src/components/Button.js | 1 + packages/jest-runtime/src/index.ts | 10 ++++------ 7 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 e2e/module-name-mapper-correct-mock-absolute-path/__tests__/index.js create mode 100644 e2e/module-name-mapper-correct-mock-absolute-path/index.js create mode 100644 e2e/module-name-mapper-correct-mock-absolute-path/package.json create mode 100644 e2e/module-name-mapper-correct-mock-absolute-path/src/components/Button.js diff --git a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap index a3759f42c96f..3111e1e44f40 100644 --- a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap +++ b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap @@ -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 configuration 1`] = ` FAIL __tests__/index.js ● Test suite failed to run diff --git a/e2e/__tests__/moduleNameMapper.test.ts b/e2e/__tests__/moduleNameMapper.test.ts index a4186a3453f2..5663db531d42 100644 --- a/e2e/__tests__/moduleNameMapper.test.ts +++ b/e2e/__tests__/moduleNameMapper.test.ts @@ -27,6 +27,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); diff --git a/e2e/module-name-mapper-correct-mock-absolute-path/__tests__/index.js b/e2e/module-name-mapper-correct-mock-absolute-path/__tests__/index.js new file mode 100644 index 000000000000..959e1bc9b32d --- /dev/null +++ b/e2e/module-name-mapper-correct-mock-absolute-path/__tests__/index.js @@ -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(); +}); diff --git a/e2e/module-name-mapper-correct-mock-absolute-path/index.js b/e2e/module-name-mapper-correct-mock-absolute-path/index.js new file mode 100644 index 000000000000..9552c0b25edd --- /dev/null +++ b/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'; diff --git a/e2e/module-name-mapper-correct-mock-absolute-path/package.json b/e2e/module-name-mapper-correct-mock-absolute-path/package.json new file mode 100644 index 000000000000..67f3c9a77889 --- /dev/null +++ b/e2e/module-name-mapper-correct-mock-absolute-path/package.json @@ -0,0 +1,7 @@ +{ + "jest": { + "moduleNameMapper": { + "^/(.*)$": "/src/$1" + } + } +} diff --git a/e2e/module-name-mapper-correct-mock-absolute-path/src/components/Button.js b/e2e/module-name-mapper-correct-mock-absolute-path/src/components/Button.js new file mode 100644 index 000000000000..4157d61bbc2f --- /dev/null +++ b/e2e/module-name-mapper-correct-mock-absolute-path/src/components/Button.js @@ -0,0 +1 @@ +module.exports = () => 'Button'; diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index e3cba4c951bd..454b157595a9 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -395,12 +395,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 &&