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(jest-mock): spyOn should support 0 key in objects (#14077) #14082

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- `[jest-mock]` Tweak typings to allow `jest.replaceProperty()` replace methods ([#14008](https://github.com/facebook/jest/pull/14008))
- `[jest-snapshot]` Fix a potential bug when not using prettier and improve performance ([#14036](https://github.com/facebook/jest/pull/14036))
- `[@jest/transform]` Do not instrument `.json` modules ([#14048](https://github.com/facebook/jest/pull/14048))
- `[jest-mock]` Fix `jest.spyOn()` to correctly handle `0` as a method/property name ([#14082](https://github.com/facebook/jest/pull/14082))

### Chore & Maintenance

Expand Down
22 changes: 22 additions & 0 deletions packages/jest-mock/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,15 @@ describe('moduleMocker', () => {
expect(() => {
moduleMocker.spyOn(null, 'method');
}).toThrow('spyOn could not find an object to spy on for method');
expect(() => {
moduleMocker.spyOn({} as Record<string, any>, '');
}).toThrow('No property name supplied');
expect(() => {
moduleMocker.spyOn({} as Record<number, any>, NaN);
}).toThrow('No property name supplied');
expect(() => {
moduleMocker.spyOn({}, undefined);
}).toThrow('No property name supplied');
expect(() => {
moduleMocker.spyOn({}, 'method');
}).toThrow(
Comment on lines +1311 to 1322
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a way these message (and also some of "Cannot spy on the method property because it is not a function" below) are somewhat misleading. How about reworking the whole check logic like this or so:

if (methodKey == null) {
  throw new Error('No property name supplied');
}

if (accessType) {
  return this._spyOnProperty(object, methodKey, accessType);
}

const original = object[methodKey];

if (!original) {
  throw new Error(
    `Property ${String(methodKey)} does not exist in the provided object`,
  );
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

@octaharon octaharon Apr 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you ask me, I have no objections. But the messages you refer to were implemented back in 2018 (#5107) for reasons I'm not aware of, and changing those go beyond the scope of fixing the related bug and this PR, I believe. Feel free to do it in a separate PR though, but please note that the code you suggested will not fix the bug and can possibly create more bugs, because methodKey==null is an incorrect way to check for an object key, since it's true only for undefined and null, at least according to MDN

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since it's true for methodKey===0

Not sure I follow this. methodKey == null would exclude only undefined and null, hence the message 'No property name supplied' is correct. This message does not sound correct if user passed '' or NaN as a methodKey. Do I miss something?

Copy link
Author

@octaharon octaharon Apr 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That message for sure doesn't sound correct, but still it was there for 5 years, and I don't believe I'm in position to decide the way it should be changed.

I personally don't see how null and '' are different in this context, for instance, yet getting a message like that would not help me much if I've passed a variable as the second argument (i.e. "supplied" something), which could possibly be any of the above. If I was to make a statement, I'd say if (!object[methodKey]) is enough to cover all the edge cases here, resulting in the same error, i.e. Method ${methodKey} doesn't exist in the provided object, assuming spyOn expects a function. Also I don't believe testing for particular error strings is a good approach in general, at least when they are literal strings instead of shared constants. The aforementioned tests I've implemented do match the existing behaviour, which was not covered before.

However, I don't mind putting in a few extra lines of code, but if are willing to take the responsibility for those changes, please provide the exact requirements.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to do it in a separate PR though

See #14087

Expand All @@ -1320,6 +1329,19 @@ describe('moduleMocker', () => {
);
});

it('should not throw when spying on a method named `0`', () => {
let haveBeenCalled = false;
const obj = {
0: () => {
haveBeenCalled = true;
},
};
const spy = moduleMocker.spyOn(obj, 0);
obj[0].call(null);
expect(haveBeenCalled).toBe(true);
expect(spy).toHaveBeenCalled();
});

it('supports clearing a spy', () => {
let methodOneCalls = 0;
const obj = {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ export class ModuleMocker {
);
}

if (!methodKey) {
if (!methodKey && !Number.isFinite(methodKey)) {
throw new Error('No property name supplied');
}

Expand Down