Skip to content

Commit

Permalink
Check _isMockFunction is true rather than truthy
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Ristea committed Sep 28, 2018
1 parent 728ca11 commit 7e0ad40
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions e2e/__tests__/reset_modules.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
19 changes: 19 additions & 0 deletions e2e/reset_modules/__tests__/reset_modules.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
5 changes: 5 additions & 0 deletions e2e/reset_modules/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
6 changes: 3 additions & 3 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -702,7 +702,7 @@ class ModuleMockerClass {
this._getSlots(component).forEach(slot => {
if (
type === 'function' &&
component._isMockFunction &&
component._isMockFunction === true &&
slot.match(/^mock/)
) {
return;
Expand All @@ -727,7 +727,7 @@ class ModuleMockerClass {
}

isMockFunction(fn: any): boolean {
return !!(fn && fn._isMockFunction);
return !!fn && fn._isMockFunction === true;
}

fn(implementation?: any): any {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ class Runtime {
(typeof globalMock === 'object' && globalMock !== null) ||
typeof globalMock === 'function'
) {
globalMock._isMockFunction && globalMock.mockClear();
globalMock._isMockFunction === true && globalMock.mockClear();
}
});

Expand Down

0 comments on commit 7e0ad40

Please sign in to comment.