From c1d9c2ec0c99b35af044563de8789068d6f66297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Thu, 30 Aug 2018 13:29:44 +0100 Subject: [PATCH] Mock inherited static properties and methods (#6921) * Mocked inherited static properties and methods * Update CHANGELOG.md --- CHANGELOG.md | 1 + .../jest-mock/src/__tests__/jest_mock.test.js | 18 ++++++++++++++++++ packages/jest-mock/src/index.js | 4 +++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae7d56b2c17e..3f55fe489469 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - `[jest-cli]` Fix incorrect `testEnvironmentOptions` warning ([#6852](https://github.com/facebook/jest/pull/6852)) - `[jest-each]` Prevent done callback being supplied to describe ([#6843](https://github.com/facebook/jest/pull/6843)) - `[jest-config]` Better error message for a case when a preset module was found, but no `jest-preset.js` or `jest-preset.json` at the root ([#6863](https://github.com/facebook/jest/pull/6863)) +- `[jest-mock]` Fix inheritance of static properties and methods in mocks ([#6921](https://github.com/facebook/jest/pull/6921)) ### Chore & Maintenance diff --git a/packages/jest-mock/src/__tests__/jest_mock.test.js b/packages/jest-mock/src/__tests__/jest_mock.test.js index 9b09ce849199..1d3cdb9855ff 100644 --- a/packages/jest-mock/src/__tests__/jest_mock.test.js +++ b/packages/jest-mock/src/__tests__/jest_mock.test.js @@ -185,6 +185,24 @@ describe('moduleMocker', () => { expect(instanceFooMock.toString.mock).not.toBeUndefined(); }); + it('mocks ES2015 non-enumerable static properties and methods', () => { + class ClassFoo { + static foo() {} + } + ClassFoo.fooProp = () => {}; + + class ClassBar extends ClassFoo {} + + const ClassBarMock = moduleMocker.generateFromMetadata( + moduleMocker.getMetadata(ClassBar), + ); + + expect(typeof ClassBarMock.foo).toBe('function'); + expect(typeof ClassBarMock.fooProp).toBe('function'); + expect(ClassBarMock.foo.mock).not.toBeUndefined(); + expect(ClassBarMock.fooProp.mock).not.toBeUndefined(); + }); + it('mocks methods that are bound multiple times', () => { const func = function func() {}; const multipleBoundFunc = func.bind(null).bind(null); diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index 9b679c73e4c3..aed9c5860590 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -690,7 +690,9 @@ class ModuleMockerClass { if ( (!component.hasOwnProperty && component[slot] !== undefined) || (component.hasOwnProperty && component.hasOwnProperty(slot)) || - (type === 'object' && component[slot] != Object.prototype[slot]) + (type === 'object' && component[slot] != Object.prototype[slot]) || + // $FlowFixMe `Function` definition does not include `prototype` + (type === 'function' && component[slot] != Function.prototype[slot]) ) { const slotMetadata = this.getMetadata(component[slot], refs); if (slotMetadata) {