From c3834ddf563484b01e73e6fff29a5fa6badd94df Mon Sep 17 00:00:00 2001 From: Charley Date: Wed, 22 May 2024 14:00:44 -0500 Subject: [PATCH 1/2] fix transform for existing `spyOn` --- src/transformers/jasmine-globals.test.ts | 2 ++ src/transformers/jasmine-globals.ts | 5 +---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts index d2da1824..35be6a0d 100644 --- a/src/transformers/jasmine-globals.test.ts +++ b/src/transformers/jasmine-globals.test.ts @@ -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(); @@ -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; ` ) }) diff --git a/src/transformers/jasmine-globals.ts b/src/transformers/jasmine-globals.ts index 69471932..fafb55b2 100644 --- a/src/transformers/jasmine-globals.ts +++ b/src/transformers/jasmine-globals.ts @@ -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 From a3e1b24f432c8a58d8a264b26ec06e75f1a7498d Mon Sep 17 00:00:00 2001 From: Charley Date: Thu, 23 May 2024 16:23:42 -0500 Subject: [PATCH 2/2] call `mockRestore` for existing spies --- src/transformers/jasmine-globals.test.ts | 2 +- src/transformers/jasmine-globals.ts | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts index 35be6a0d..03980f02 100644 --- a/src/transformers/jasmine-globals.test.ts +++ b/src/transformers/jasmine-globals.test.ts @@ -43,7 +43,7 @@ test('spyOn', () => { jest.spyOn(stuff).mockResolvedValue('lmao'); jest.spyOn(stuff).mockRejectedValue('oh no'); const fetchSpy = jest.spyOn(window, 'fetch').mockResolvedValue({json: {}}); - existingSpy; + existingSpy.mockRestore(); ` ) }) diff --git a/src/transformers/jasmine-globals.ts b/src/transformers/jasmine-globals.ts index fafb55b2..d3ec1afc 100644 --- a/src/transformers/jasmine-globals.ts +++ b/src/transformers/jasmine-globals.ts @@ -161,7 +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': { - j(path).replaceWith(path.node.callee.object.object) + // 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