Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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