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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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;
Copy link
Owner

@skovhus skovhus May 23, 2024

Choose a reason for hiding this comment

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

This looks wrong to me. Does it make sense to keep it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't necessarily disagree (more for stylistic reasons than anything technical), but it does match the output of the andCallThrough() transform here.

But now that I'm thinking about it, I wonder if the behavior should be different for existingSpy, as simply dropping the .and.callThrough() wouldn't restore the original implementation. Maybe the more accurate transform would be:

spyOn(stuff).and.callThrough(); -> jest.spyOn(stuff);

existingSpy.and.callThrough(); -> existingSpy.mockRestore();

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

decided to make that change. let me know what you think.

Copy link
Owner

Choose a reason for hiding this comment

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

Seems to make sense, thanks.

`
)
})
Expand Down
5 changes: 1 addition & 4 deletions src/transformers/jasmine-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,7 @@ 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
j(path).replaceWith(path.node.callee.object.object)
break
}
// if it's `*.and.callFake()`, replace with jest's
Expand Down