diff --git a/CHANGELOG.md b/CHANGELOG.md index 43160fd7f324..d23b48285149 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -98,6 +98,7 @@ - `[jest-runtime]` Fix mistake as test files when run coverage issue. ([#7506](https://github.com/facebook/jest/pull/7506)) - `[jest-cli]` print info about passWithNoTests flag ([#7309](https://github.com/facebook/jest/pull/7309)) - `[pretty-format]` Omit unnecessary symbol filter for object keys ([#7457](https://github.com/facebook/jest/pull/7457)) +- `[jest-runtime]` Fix `requireActual` on node_modules with mock present ([#7404](https://github.com/facebook/jest/pull/7404)) ### Chore & Maintenance diff --git a/packages/jest-runtime/src/__tests__/runtime_require_actual.test.js b/packages/jest-runtime/src/__tests__/runtime_require_actual.test.js new file mode 100644 index 000000000000..9cbedf543008 --- /dev/null +++ b/packages/jest-runtime/src/__tests__/runtime_require_actual.test.js @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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'; + +let createRuntime; + +describe('Runtime requireActual', () => { + beforeEach(() => { + createRuntime = require('createRuntime'); + }); + + it('requires node module when manual mock exists', () => + createRuntime(__filename).then(runtime => { + const exports = runtime.requireActual( + runtime.__mockRootPath, + 'mocked-node-module', + ); + expect(exports.isManualMockModule).toBe(false); + })); +}); diff --git a/packages/jest-runtime/src/__tests__/test_root/__mocks__/mocked-node-module.js b/packages/jest-runtime/src/__tests__/test_root/__mocks__/mocked-node-module.js new file mode 100644 index 000000000000..caa044941920 --- /dev/null +++ b/packages/jest-runtime/src/__tests__/test_root/__mocks__/mocked-node-module.js @@ -0,0 +1,11 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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'; + +exports.isManualMockModule = true; diff --git a/packages/jest-runtime/src/__tests__/test_root/node_modules/mocked-node-module/index.js b/packages/jest-runtime/src/__tests__/test_root/node_modules/mocked-node-module/index.js new file mode 100644 index 000000000000..f64a42aaca22 --- /dev/null +++ b/packages/jest-runtime/src/__tests__/test_root/node_modules/mocked-node-module/index.js @@ -0,0 +1,11 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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'; + +exports.isManualMockModule = false; diff --git a/packages/jest-runtime/src/index.js b/packages/jest-runtime/src/index.js index bb8be2d1824b..0d18f701c9b9 100644 --- a/packages/jest-runtime/src/index.js +++ b/packages/jest-runtime/src/index.js @@ -286,6 +286,7 @@ class Runtime { from: Path, moduleName?: string, options: ?InternalModuleOptions, + isRequireActual: ?boolean, ) { const moduleID = this._resolver.getModuleID( this._virtualMocks, @@ -301,6 +302,7 @@ class Runtime { moduleName && this._resolver.getMockModule(from, moduleName); if ( (!options || !options.isInternalModule) && + !isRequireActual && !moduleResource && manualMock && manualMock !== this._isCurrentlyExecutingManualMock && @@ -363,6 +365,10 @@ class Runtime { return this.requireModule(from, to, {isInternalModule: true}); } + requireActual(from: Path, moduleName: string) { + return this.requireModule(from, moduleName, undefined, true); + } + requireMock(from: Path, moduleName: string) { const moduleID = this._resolver.getModuleID( this._virtualMocks, @@ -841,7 +847,7 @@ class Runtime { : this.requireModuleOrMock.bind(this, from.filename); moduleRequire.cache = Object.create(null); moduleRequire.extensions = Object.create(null); - moduleRequire.requireActual = this.requireModule.bind(this, from.filename); + moduleRequire.requireActual = this.requireActual.bind(this, from.filename); moduleRequire.requireMock = this.requireMock.bind(this, from.filename); moduleRequire.resolve = (moduleName, options) => this._requireResolve(from.filename, moduleName, options);