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

Conversation

octaharon
Copy link

@octaharon octaharon commented Apr 18, 2023

Summary

jest.spyOn previously throwed when applied to a method that is referenced by 0 key of the target module/object, thus being incompatible with design patterns, that rely on Enum indices, and Array-like objects.

See #14077

Test plan

enum IndexedKeys {
    A,
    B,
    C
}

const DynamicDispatchObject:Record<IndexedKeys,CallableFunction>={
    [IndexedKeys.A]:()=>{},
    [IndexedKeys.B]:()=>{},
    [IndexedKeys.C]:()=>{}
}

// Doesn't throw anymore, works as intended
const spiedFunction = jest.spyOn(DynamicDispatchObject, IndexedKeys.A);

Comment on lines +1311 to 1322
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(
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

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants