Skip to content

Commit

Permalink
feat(jasmine): handle spy.throwError (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
jase88 authored Sep 14, 2024
1 parent ba5fbf7 commit 17ca751
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/transformers/jasmine-globals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
});
`
)

Expand Down
21 changes: 21 additions & 0 deletions src/transformers/jasmine-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
})

Expand Down

0 comments on commit 17ca751

Please sign in to comment.