Skip to content
Closed
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
18 changes: 16 additions & 2 deletions modules/Expectation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import isEqual from 'is-equal'
import isRegExp from 'is-regex'
import tmatch from 'tmatch'
import has from 'has'
import inspect from 'object-inspect'
import assert from './assert'
import { isSpy } from './SpyUtils'
import {
Expand Down Expand Up @@ -473,8 +474,21 @@ class Expectation {
)

assert(
spy.calls.some(call => isEqual(call.arguments, expectedArgs)),
'spy was never called with %s',
spy.calls.length > 0,
('spy was not called')
)
const genMessage = () => {
const actualArguments = spy.calls
.map(call => `- ${inspect(call.arguments)}`)
.join('\n')

return `spy was not called with:\n%s.\n\nIt was called with:\n ${actualArguments}`
}
const hasBeenCalled = spy.calls.some(call => isEqual(call.arguments, expectedArgs))

assert(
hasBeenCalled,
hasBeenCalled ? null : genMessage(),
expectedArgs
)

Expand Down
16 changes: 16 additions & 0 deletions modules/__tests__/spyOn-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ describe('A function that was spied on', () => {
spy.restore()
expect(video.play).toNotEqual(spy)
})

it('was called with the correct args', () => {
spy.restore()
video.play('some', 'args')
try {
expect(spy).toHaveBeenCalledWith('foo', 'bar')
} catch (err) {
const expected =
`|spy was not called with:
|[ \'foo\', \'bar\' ].
|
|It was called with:
| - [ \'some\', \'args\' ]`.replace(/^ *\|/gm, '')
expect(err.message).toEqual(expected)
}
})
})

describe('A function that was spied on but not called', () => {
Expand Down