Skip to content

Commit

Permalink
Fix requireActual of node module (#7404)
Browse files Browse the repository at this point in the history
  • Loading branch information
phapp88 authored and SimenB committed Dec 24, 2018
1 parent c5fa387 commit bfdd4e1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
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

0 comments on commit bfdd4e1

Please sign in to comment.