diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts index d2da1824..03980f02 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.mockRestore(); ` ) }) diff --git a/src/transformers/jasmine-globals.ts b/src/transformers/jasmine-globals.ts index 69471932..d3ec1afc 100644 --- a/src/transformers/jasmine-globals.ts +++ b/src/transformers/jasmine-globals.ts @@ -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