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

Check _isMockFunction is true rather than truthy on global properties #7017

Merged
merged 1 commit into from
Sep 28, 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
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