Skip to content

Commit

Permalink
fix: throw error if original funciton threw
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jan 7, 2022
1 parent c9a6383 commit 0595136
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/spy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export function spy<A extends any[], R>(cb?: (...args: A) => R): SpyFn<A, R> {
} catch (err: any) {
result = err
type = 'error'
fn.results.push([type, err])
throw err
}
}
let resultTuple: ResultFn<R> = [type, result]
Expand Down
17 changes: 17 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,20 @@ test('no descriptor', () => {
expect(getter.called).toBe(true)
expect(getter.returns[0]).toBe(42)
})

test('throw error', () => {
const obj = {
err: () => {},
}

spyOn(obj, 'err').willCall(() => {
throw new Error('oh no')
})

try {
obj.err()
expect.fail('should be catched')
} catch (err) {
expect(err.message).toBe('oh no')
}
})

0 comments on commit 0595136

Please sign in to comment.