-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Merged
sheremet-va
merged 20 commits into
vitest-dev:main
from
hi-ogawa:fix-re-mock-instance-method
Dec 4, 2023
Merged
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2b96a04
fix: mock each class instance's method separately
hi-ogawa 5dbd3ef
test: update assertion (breaking change?)
hi-ogawa fabcc8e
refactor: minor
hi-ogawa fa9e458
test: tweak test
hi-ogawa 5d5d0c1
fix: delegate prototype mock call
hi-ogawa 66c48af
fix: fix arguments delegation
hi-ogawa 9171b4e
chore: rename
hi-ogawa ffd7c53
chore: minor
hi-ogawa 3936c76
fix: support symbol key method
hi-ogawa 943d2ac
refactor: minor
hi-ogawa 7cdc3be
Merge branch 'main' into fix-re-mock-instance-method
hi-ogawa 742fc59
Merge branch 'main' into fix-re-mock-instance-method
hi-ogawa 17c8c70
Merge branch 'main' into fix-re-mock-instance-method
hi-ogawa aae45a7
chore: comment
hi-ogawa 5b60bbc
test: test mockRestore
hi-ogawa b17c791
Merge branch 'main' into fix-re-mock-instance-method
hi-ogawa 736b61c
test: more check
hi-ogawa 24d5acc
test: restoreAllMocks for mocked class methods (wip)
hi-ogawa a5c3a06
fix: handle vi.restoreAllMocks
hi-ogawa e4bad94
Merge branch 'main' into fix-re-mock-instance-method
hi-ogawa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class MockedE { | ||
public doSomething() { | ||
return 'hello' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { expect, test, vi } from 'vitest' | ||
|
||
import { MockedE } from '../src/mockedE' | ||
|
||
vi.mock('../src/mockedE') | ||
|
||
// TODO: move to mocked.test.ts | ||
|
||
test('mock each instance method separately', () => { | ||
expect(MockedE).toBeTypeOf('function') | ||
|
||
const instance1 = new MockedE() | ||
const instance2 = new MockedE() | ||
expect(instance1).not.toBe(instance2) | ||
expect(instance1.doSomething).not.toBe(instance2.doSomething) | ||
expect(vi.mocked(instance1.doSomething).mock).not.toBe(vi.mocked(instance2.doSomething).mock) | ||
|
||
instance1.doSomething() | ||
expect(instance1.doSomething).toBeCalledTimes(1) | ||
expect(instance2.doSomething).toBeCalledTimes(0) | ||
expect(MockedE.prototype.doSomething).toBeCalledTimes(0) | ||
hi-ogawa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
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 testmockFn.mockRestore
) andmocked-class-restore-all.test.ts
(to testvi.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!