From 17ca751903925b93fa55bd46987905f4369e15c9 Mon Sep 17 00:00:00 2001 From: jase <804836+jase88@users.noreply.github.com> Date: Sat, 14 Sep 2024 22:44:30 +0200 Subject: [PATCH] feat(jasmine): handle spy.throwError (#619) --- src/transformers/jasmine-globals.test.ts | 12 ++++++++++++ src/transformers/jasmine-globals.ts | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts index 7c6b7069..1da75bb5 100644 --- a/src/transformers/jasmine-globals.test.ts +++ b/src/transformers/jasmine-globals.test.ts @@ -126,6 +126,9 @@ test('jasmine.createSpy', () => { jasmine.createSpy().and.resolveTo('lmao'); jasmine.createSpy().and.rejectWith('oh no'); const spy3 = jasmine.createSpy().and.resolveTo('lmao'); + spy.and.throwError('my error message'); + spy.and.throwError(new Error('awesome error')); + spy.and.throwError(42); `, ` jest.fn(); @@ -137,6 +140,15 @@ test('jasmine.createSpy', () => { jest.fn().mockResolvedValue('lmao'); jest.fn().mockRejectedValue('oh no'); const spy3 = jest.fn().mockResolvedValue('lmao'); + spy.mockImplementation(() => { + throw new Error('my error message'); + }); + spy.mockImplementation(() => { + throw new Error('awesome error'); + }); + spy.mockImplementation(() => { + throw 42; + }); ` ) diff --git a/src/transformers/jasmine-globals.ts b/src/transformers/jasmine-globals.ts index 63346e50..69888a43 100644 --- a/src/transformers/jasmine-globals.ts +++ b/src/transformers/jasmine-globals.ts @@ -341,6 +341,27 @@ export default function jasmineGlobals(fileInfo, api, options) { path.node.callee.property.name = 'mockRejectedValue' break } + + case 'throwError': { + const throwArg = path.node.arguments[0] + const isStringLiteral = + throwArg.type === 'Literal' && typeof throwArg.value === 'string' + + path.node.callee.object = path.node.callee.object.object + path.node.callee.property.name = 'mockImplementation' + + path.node.arguments = [ + j.arrowFunctionExpression( + [], + isStringLiteral + ? j.blockStatement([ + j.throwStatement(j.newExpression(j.identifier('Error'), [throwArg])), + ]) + : j.blockStatement([j.throwStatement(throwArg)]) + ), + ] + break + } } })