Skip to content
Permalink
Browse files
added simple spies example
  • Loading branch information
aa7401 committed Nov 30, 2018
1 parent 4d0c220 commit e5a1f2539fe5408788129974176b25fb8c21cdae
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
@@ -0,0 +1,9 @@

'use strict'

const math = require('./math.js')

module.exports.doAdd = (a, b) => math.add(a, b)
module.exports.doSubtract = (a, b) => math.subtract(a, b)
module.exports.doMultiply = (a, b) => math.multiply(a, b)
module.exports.doDivide = (a, b) => math.divide(a, b)
@@ -0,0 +1,19 @@

'use strict'

const app = require('./app')
const math = require('./math')

test('calls math.add', () => {
const addMock = jest.spyOn(math, 'add')

// override the implementation
addMock.mockImplementation(() => 'mock')
expect(app.doAdd(1, 2)).toEqual('mock')
expect(addMock).toHaveBeenCalledWith(1, 2)

// restore the original implementation
addMock.mockRestore()
expect(app.doAdd(1, 2)).toEqual(3)
console.log(addMock.mock.calls)
})
@@ -0,0 +1,7 @@

'use strict'

module.exports.add = (a, b) => a + b
module.exports.subtract = (a, b) => b - a
module.exports.multiply = (a, b) => a * b
module.exports.divide = (a, b) => b / a
@@ -0,0 +1,20 @@
{
"name": "03_spies",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "./node_modules/.bin/jest",
"watch": "./node_modules/.bin/jest --noStackTrace --watchAll"
},
"author": "",
"license": "ISC",
"jest": {
"testEnvironment": "node",
"verbose": true,
"collectCoverage": true
},
"devDependencies": {
"jest": "^23.6.0"
}
}

0 comments on commit e5a1f25

Please sign in to comment.