Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: spyOn should not rely on hasOwnProperty from the spied object #11721

Merged
merged 3 commits into from Aug 16, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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