Skip to content

Commit f0a56e0

Browse files
cexbrayatsplincode
authored andcommitted
fix: spyOn should not rely on hasOwnProperty from the spied object (jestjs#11721)
1 parent abcc369 commit f0a56e0

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- `[jest-types]` Compat with `@types/node` v16 ([#11645](https://github.com/facebook/jest/pull/11645))
88
- `[jest-environment-node]` Add `Event` and `EventTarget` to node global environment. ([#11705](https://github.com/facebook/jest/issues/11705))
9+
- `[jest-mock]` Fix `spyOn` to use `Object.prototype.hasOwnProperty` [#11721](https://github.com/facebook/jest/pull/11721)
910

1011
### Chore & Maintenance
1112

packages/jest-mock/src/__tests__/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,18 @@ describe('moduleMocker', () => {
12161216
expect(originalCallArguments[1]).toBe(secondArg);
12171217
expect(spy).not.toHaveBeenCalled();
12181218
});
1219+
1220+
it('should work with object of null prototype', () => {
1221+
const Foo = Object.assign(Object.create(null), {
1222+
foo() {},
1223+
});
1224+
1225+
const spy = moduleMocker.spyOn(Foo, 'foo');
1226+
1227+
Foo.foo();
1228+
1229+
expect(spy).toHaveBeenCalled();
1230+
});
12191231
});
12201232

12211233
describe('spyOnProperty', () => {

packages/jest-mock/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,10 @@ export class ModuleMocker {
974974
);
975975
}
976976

977-
const isMethodOwner = object.hasOwnProperty(methodName);
977+
const isMethodOwner = Object.prototype.hasOwnProperty.call(
978+
object,
979+
methodName,
980+
);
978981

979982
let descriptor = Object.getOwnPropertyDescriptor(object, methodName);
980983
let proto = Object.getPrototypeOf(object);

0 commit comments

Comments
 (0)