Skip to content

Instantly share code, notes, and snippets.

@akameco
Last active August 14, 2017 19:40
Show Gist options
  • Save akameco/b3e805a8733ca7675c5e057ff6850464 to your computer and use it in GitHub Desktop.
Save akameco/b3e805a8733ca7675c5e057ff6850464 to your computer and use it in GitHub Desktop.
mock console.log
// @flow
const sum = (a, b) => a + b
test('sum', () => {
expect(sum(1, 2)).toBe(3)
})
const spyLog = jest.spyOn(console, 'log')
spyLog.mockImplementation(x => x)
const printSum = (a, b) => {
console.log(`sum = ${sum(a, b)}`)
}
test('printSum', () => {
printSum(1, 2)
expect(console.log).toBeCalled()
expect(spyLog.mock.calls[0][0]).toBe('sum = 3')
spyLog.mockReset()
spyLog.mockRestore()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment