Skip to content

Commit

Permalink
fix: spyOn should not rely on hasOwnProperty from the spied object (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cexbrayat committed Aug 16, 2021
1 parent cccc155 commit f3ae13c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Fixes

- `[jest-environment-node]` Add `Event` and `EventTarget` to node global environment. ([#11705](https://github.com/facebook/jest/issues/11705))
- `[jest-mock]` Fix `spyOn` to use `Object.prototype.hasOwnProperty` [#11721](https://github.com/facebook/jest/pull/11721)

### Chore & Maintenance

Expand Down
12 changes: 12 additions & 0 deletions packages/jest-mock/src/__tests__/index.test.ts
Expand Up @@ -1216,6 +1216,18 @@ describe('moduleMocker', () => {
expect(originalCallArguments[1]).toBe(secondArg);
expect(spy).not.toHaveBeenCalled();
});

it('should work with object of null prototype', () => {
const Foo = Object.assign(Object.create(null), {
foo() {},
});

const spy = moduleMocker.spyOn(Foo, 'foo');

Foo.foo();

expect(spy).toHaveBeenCalled();
});
});

describe('spyOnProperty', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-mock/src/index.ts
Expand Up @@ -976,7 +976,10 @@ export class ModuleMocker {
);
}

const isMethodOwner = object.hasOwnProperty(methodName);
const isMethodOwner = Object.prototype.hasOwnProperty.call(
object,
methodName,
);

let descriptor = Object.getOwnPropertyDescriptor(object, methodName);
let proto = Object.getPrototypeOf(object);
Expand Down

0 comments on commit f3ae13c

Please sign in to comment.