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 requireActual of node module #7404

Merged
merged 3 commits into from
Dec 24, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions packages/jest-runtime/src/__tests__/runtime_require_actual.test.js
Original file line number Diff line number Diff line change
@@ -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);
}));
});
Original file line number Diff line number Diff line change
@@ -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;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ class Runtime {
from: Path,
moduleName?: string,
options: ?InternalModuleOptions,
isRequireActual: ?boolean,
) {
const moduleID = this._resolver.getModuleID(
this._virtualMocks,
Expand All @@ -301,6 +302,7 @@ class Runtime {
moduleName && this._resolver.getMockModule(from, moduleName);
if (
(!options || !options.isInternalModule) &&
!isRequireActual &&
!moduleResource &&
manualMock &&
manualMock !== this._isCurrentlyExecutingManualMock &&
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down