-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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: independently mock each instance's methods for mocked class #4564
fix: independently mock each instance's methods for mocked class #4564
Conversation
✅ Deploy Preview for fastidious-cascaron-4ded94 canceled.
|
83c480e
to
5d5d0c1
Compare
d98562e
to
66c48af
Compare
aa42ff4
to
943d2ac
Compare
const type = getType(value) | ||
const isFunction = type.includes('Function') && typeof value === 'function' | ||
if (isFunction) { | ||
// TODO: ability to restore? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to restore it in jest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked the behavior on Jest here https://stackblitz.com/edit/jest-example-z6bfab?file=mocked-class-restore.test.js
I might be using mockRestore
incorrectly, but Jest doesn't seem to support such use case.
I added a similar test case for Vitest 5b60bbc and mockRestore
is currently not working either but in a different way.
Would you wish to align this behavior? (or maybe make it better?)
Personally I think supporting this use case could be done separately as a nice-to-have feature.
Actually, I haven't checked what current Vitest's behavior is. I'll compare with that too later.
(EDIT: here it is https://stackblitz.com/edit/vitest-dev-vitest-hao9hx?file=test%2Fmocked-class-restore.test.ts)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I missed one important context #3438 and the rational of this mockRestore
override:
vitest/packages/vitest/src/runtime/mocker.ts
Lines 353 to 357 in 736b61c
mock.mockRestore = () => { | |
mock.mockReset() | |
mock.mockImplementation(() => undefined) | |
return mock | |
} |
I don't think my current implementation considers global mock restore use case. I think I understand what's the expected behavior, so let me deal with this and I'll add an appropriate test case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I update the code so that constructor mocking is preserved after vi.restoreAllMocks
, which I think is a desired behavior aligning with #3438.
I made separate test cases mocked-class-restore-explicit.test.ts
(to test mockFn.mockRestore
) and mocked-class-restore-all.test.ts
(to test vi.restoreAllMocks
). The "explicit" scenario is still not aligned with Jest's behavior but I feel this use case is unusual and might be difficult to support.
I'd like to know what you think about this scenario. Thanks!
Description
I think the relevant code on jest is around here and it looks like jest is mocking each instance method on construction:
https://github.com/jestjs/jest/blob/2c3d2409879952157433de215ae0eee5188a4384/packages/jest-mock/src/index.ts#L678-L691
I also created https://stackblitz.com/edit/jest-example-z6bfab?file=mocked-class.test.js to compare with jest's behavior.
The approach I took here might not be robust to cover some potential edge cases, but at least for common usages, this fix is a simple one and reasonably makes sense to me.
I was also wondering if such fix/feature could be moved to tinyspy, but I'm not too familiar with the whole concept, so I would like maintainers to review my current approach at this stage to gather opinions.
I left a few TODOs in the code, so I would also appreciate if you have any input for those.
Thanks for the review!
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yaml
unless you introduce a new test example.Tests
pnpm test:ci
.Documentation
pnpm run docs
command.Changesets
feat:
,fix:
,perf:
,docs:
, orchore:
.