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

feat(jest-mock): allow jest.replaceProperty to replace undefined or nonexistent properties #13958

Conversation

mrazauskas
Copy link
Contributor

Summary

As discussed in #13496, it would be useful if jest.replaceProperty could replace undefined or nonexistent properties as well.

Here I implemented this through tolerateUndefined option. The name can be different, of course.

Test plan

Tests are added.

throw new Error(`${String(propertyKey)} is not declared configurable`);
}
if (descriptor?.writable === false) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

writable check was missing. I bumped into it while playing with this case:

https://github.com/facebook/jest/blob/bb39cb2c617a3334bf18daeca66bd87b7ccab28b/packages/babel-jest/src/__tests__/getCacheKey.test.ts#L195-L197

Hm.. What to do with non-writable values like process.version? Would be nice to replace them too. Perhaps via tolerateFrozen option? (Alternatives like tolerateNonWritable, or tolerateNonwritable don’t look convincing.)

@@ -663,6 +663,22 @@ test('isLocalhost returns false when HOSTNAME is not localhost', () => {
});
```

Jest will make sure that the target property exists on the object and its value is not `undefined`. To skip this precaution, use `tolerateUndefined` option:
Copy link
Member

Choose a reason for hiding this comment

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

I don't really like the name tolerateUndefined. I don't have any better suggestions off the top of my head, tho.

tolerateMissing, allowMissing, setMissing... neither sounds good, but tolerates is even worse imo 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What about respectUndefined? Not sure. Hm.. Did you see my comment on non-writable values? What about option strict: false which would allow creating undefined props and overwriting non-writable values? Simple thing.

The target property cannot be undefined and non-writable at the same time. So in a way one option which turns off precautions should be enough.

Copy link
Contributor

Choose a reason for hiding this comment

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

Does replaceProperty actually disallow existing but undefined properties? I would consider this a bug.

And for non-existing properties, it probably makes more sense to add jest.addProperty instead which would act like replaceProperty + tolerateUndefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for thinking together. How do you see replacement of non-writeable props? Should it just work or do we need an option?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think for mocking, replacing non-writeable is fine (though they should remain non-writeable). I view non-writeable more as a hint for the runtime behavior while testing setup comes before the actual runtime.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think I want addProperty - then I think we'd need a removeProperty as well. It might have been better for replaceProperty to have been called setProperty, then taken options for replacement strategy. Anyways, I think a single API on jest is enough.

@mrazauskas mrazauskas marked this pull request as draft March 1, 2023 10:21
@mrazauskas mrazauskas closed this Nov 16, 2023
@mrazauskas mrazauskas deleted the feat-allow-replacing-undefined-properties branch November 16, 2023 13:51
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 Dec 17, 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

4 participants