Repository navigation
Conversation
…teSpy with an original function
`jasmine.createSpy(name, originalFn)` stores the original function but does not invoke it:
the default spy plan is a no-op stub, and the original function only runs after
`.and.callThrough()`. The refactor-jasmine-vitest schematic maps this call to
`vi.fn(originalFn)`, which invokes the function immediately, so a migrated bare spy
suddenly calls through and can change test behavior.
Append `.mockReturnValue(undefined)` when `createSpy` is given an original function and is
not chained with an `.and` strategy. This keeps the original function available for
`mockReset()` while preserving the stub-by-default behavior, mirroring how bare `spyOn`
calls are already handled. Chained spies are unchanged: `.and.callThrough()` keeps mapping
to a plain `vi.fn(originalFn)` and strategies such as `.and.returnValue()` keep replacing
the stub.
The updated spec fails without the fix: `jasmine.createSpy('mySpy', () => 'foo')` was
migrated to `vi.fn(() => 'foo').mockName('mySpy')`, which calls the function.
There was a problem hiding this comment.
Code Review
This pull request updates the Jasmine-to-Vitest refactoring transformer to preserve stub-by-default semantics for jasmine.createSpy(name, originalFn). It now appends .mockReturnValue(undefined) when the spy is not chained with .and.callThrough(), preventing the immediate execution of the original function. The feedback suggests expanding test coverage by adding a test case for other chained strategies, such as .and.returnValue(...), to ensure they are also correctly transformed without appending the default stub.
…or createSpy with an original function
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
jasmine.createSpy(name, originalFn)stores the original function but does not invoke it: the default spy plan is a no-op stub, and the original function only runs after.and.callThrough(). The refactor-jasmine-vitest schematic maps this call tovi.fn(originalFn), which invokes the function immediately, so a migrated bare spy suddenly calls through and can change test behavior.Append
.mockReturnValue(undefined)whencreateSpyis given an original function and is not chained with an.andstrategy. This keeps the original function available formockReset()while preserving the stub-by-default behavior, mirroring how barespyOncalls are already handled. Chained spies are unchanged:.and.callThrough()keeps mapping to a plainvi.fn(originalFn)and strategies such as.and.returnValue()keep replacing the stub.The updated spec fails without the fix:
jasmine.createSpy('mySpy', () => 'foo')was migrated tovi.fn(() => 'foo').mockName('mySpy'), which calls the function.