Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Don't reject promises until mock functions are called
  • Loading branch information
haines committed Feb 27, 2019
commit 40be4da07f3248db0c3a3d4129fab74922ac1087
4 changes: 2 additions & 2 deletions src/when.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ class WhenMock {
mockReturnValueOnce: val => _mockReturnValue(matchers, assertCall, true)(val),
mockResolvedValue: val => _mockReturnValue(matchers, assertCall)(Promise.resolve(val)),
mockResolvedValueOnce: val => _mockReturnValue(matchers, assertCall, true)(Promise.resolve(val)),
mockRejectedValue: err => _mockReturnValue(matchers, assertCall)(Promise.reject(err)),
mockRejectedValueOnce: err => _mockReturnValue(matchers, assertCall, true)(Promise.reject(err)),
mockRejectedValue: err => _mockReturnValue(matchers, assertCall)(() => Promise.reject(err)),
mockRejectedValueOnce: err => _mockReturnValue(matchers, assertCall, true)(() => Promise.reject(err)),
mockImplementation: implementation => _mockReturnValue(matchers, assertCall)(implementation),
mockImplementationOnce: implementation => _mockReturnValue(matchers, assertCall, true)(implementation)
})
Expand Down
22 changes: 22 additions & 0 deletions src/when.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,17 @@ describe('When', () => {
await expect(fn('foo')).rejects.toThrow('bar')
})

it('mockRejectedValue: does not reject the Promise until the function is called', done => {
const fn = jest.fn()

when(fn).calledWith('foo').mockRejectedValue(new Error('bar'))

setTimeout(async () => {
await expect(fn('foo')).rejects.toThrow('bar')
done()
}, 0)
})

it('mockRejectedValue: works with expectCalledWith', async () => {
const fn = jest.fn()

Expand All @@ -267,6 +278,17 @@ describe('When', () => {
expect(await fn('foo')).toBeUndefined()
})

it('mockRejectedValueOnce: does not reject the Promise until the function is called', done => {
const fn = jest.fn()

when(fn).calledWith('foo').mockRejectedValueOnce(new Error('bar'))

setTimeout(async () => {
await expect(fn('foo')).rejects.toThrow('bar')
done()
}, 0)
})

it('mockRejectedValueOnce: works with expectCalledWith', async () => {
const fn = jest.fn()

Expand Down