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(jasmine-globals): fix transform for existing spyOn #580

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/transformers/jasmine-globals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ test('spyOn', () => {
jest.spyOn(stuff).and.resolveTo('lmao');
jest.spyOn(stuff).and.rejectWith('oh no');
const fetchSpy = spyOn(window, 'fetch').and.resolveTo({json: {}});
existingSpy.and.callThrough();
`,
`
jest.spyOn().mockReturnValue();
Expand All @@ -42,6 +43,7 @@ test('spyOn', () => {
jest.spyOn(stuff).mockResolvedValue('lmao');
jest.spyOn(stuff).mockRejectedValue('oh no');
const fetchSpy = jest.spyOn(window, 'fetch').mockResolvedValue({json: {}});
existingSpy.mockRestore();
`
)
})
Expand Down
15 changes: 11 additions & 4 deletions src/transformers/jasmine-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,17 @@ export default function jasmineGlobals(fileInfo, api, options) {
// if it's `*.and.callThrough()` we should remove
// `and.callThrough()` because jest calls through by default
case 'callThrough': {
const { callee } = path.node.callee.object.object
const arg = path.node.callee.object.object.arguments
path.node.callee = callee
path.node.arguments = arg
// if this comes from an `Identifier` (e.g. `existingSpy.and.callThrough()`),
// we assume the intent is to restore an existing spy
// to its original implementation using `*.mockRestore()`
if (path.node.callee.object.object.type === 'Identifier') {
path.node.callee.object = path.node.callee.object.object
path.node.callee.property.name = 'mockRestore'
} else {
// otherwise, we just remove the `.and.callThrough()`
// since this is the default behavior in jest
j(path).replaceWith(path.node.callee.object.object)
}
break
}
// if it's `*.and.callFake()`, replace with jest's
Expand Down
Loading