diff --git a/CHANGELOG.md b/CHANGELOG.md index 1020684b838e..c4bd63e063f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,8 +17,10 @@ - `[jest-haste-map]` Fixed Haste whitelist generation for scoped modules on Windows ([#6980](https://github.com/facebook/jest/pull/6980)) - `[jest-mock]` Fix inheritance of static properties and methods in mocks ([#7003](https://github.com/facebook/jest/pull/7003)) - `[jest-mock]` Fix mocking objects without `Object.prototype` in their prototype chain ([#7003](https://github.com/facebook/jest/pull/7003)) +- `[jest-mock]` Check `_isMockFunction` is true rather than truthy on potential mocks ([#7017](https://github.com/facebook/jest/pull/7017)) - `[jest-cli]` Update jest-cli to show git ref in message when using `changedSince` ([#7028](https://github.com/facebook/jest/pull/7028)) - `[jest-jasmine2`] Fix crash when test return Promise rejected with null ([#7049](https://github.com/facebook/jest/pull/7049)) +- `[jest-runtime]` Check `_isMockFunction` is true rather than truthy on potential global mocks ([#7017](https://github.com/facebook/jest/pull/7017)) ### Chore & Maintenance diff --git a/e2e/__tests__/reset_modules.test.js b/e2e/__tests__/reset_modules.test.js new file mode 100644 index 000000000000..ed4ce5ca5ceb --- /dev/null +++ b/e2e/__tests__/reset_modules.test.js @@ -0,0 +1,16 @@ +/** + * 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. + * + * @flow + */ +'use strict'; + +const runJest = require('../runJest'); + +test('jest.resetModules should not error when _isMockFunction is defined but not boolean', () => { + const result = runJest('reset_modules'); + expect(result.status).toBe(0); +}); diff --git a/e2e/reset_modules/__tests__/reset_modules.test.js b/e2e/reset_modules/__tests__/reset_modules.test.js new file mode 100644 index 000000000000..9f779ab13989 --- /dev/null +++ b/e2e/reset_modules/__tests__/reset_modules.test.js @@ -0,0 +1,19 @@ +/** + * 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'; + +global.testObject = new Proxy( + {}, + { + get: function getter(target, key) { + return key; + }, + } +); +test('jest.resetModules should not error when _isMockFunction is defined but not boolean', () => { + jest.resetModules(); +}); diff --git a/e2e/reset_modules/package.json b/e2e/reset_modules/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/e2e/reset_modules/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index 39bb7a8a8d02..e97ccbcfb3c9 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -687,7 +687,7 @@ class ModuleMockerClass { return metadata; } else if (type === 'function') { metadata.name = component.name; - if (component._isMockFunction) { + if (component._isMockFunction === true) { metadata.mockImpl = component.getMockImplementation(); } } @@ -702,7 +702,7 @@ class ModuleMockerClass { this._getSlots(component).forEach(slot => { if ( type === 'function' && - component._isMockFunction && + component._isMockFunction === true && slot.match(/^mock/) ) { return; @@ -727,7 +727,7 @@ class ModuleMockerClass { } isMockFunction(fn: any): boolean { - return !!(fn && fn._isMockFunction); + return !!fn && fn._isMockFunction === true; } fn(implementation?: any): any { diff --git a/packages/jest-runtime/src/index.js b/packages/jest-runtime/src/index.js index d6612eb42221..507da20aa247 100644 --- a/packages/jest-runtime/src/index.js +++ b/packages/jest-runtime/src/index.js @@ -430,7 +430,7 @@ class Runtime { (typeof globalMock === 'object' && globalMock !== null) || typeof globalMock === 'function' ) { - globalMock._isMockFunction && globalMock.mockClear(); + globalMock._isMockFunction === true && globalMock.mockClear(); } });