Skip to content

Commit

Permalink
Mock inherited static properties and methods (#6921)
Browse files Browse the repository at this point in the history
* Mocked inherited static properties and methods
* Update CHANGELOG.md
  • Loading branch information
rubennorte committed Aug 30, 2018
1 parent 66560c3 commit c1d9c2e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions packages/jest-mock/src/__tests__/jest_mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c1d9c2e

Please sign in to comment.