Skip to content
Permalink
Browse files
added more spies
  • Loading branch information
aa7401 committed Dec 7, 2018
1 parent eeb3df9 commit 7b34c48fbb2886c4742c46fe2382d5c6806b7b9d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 31 deletions.
@@ -5,22 +5,9 @@ const math = require('./math.js')
const fs = require('fs')

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)

module.exports.saveData = (filename, data, callback) => {
fs.writeFile(filename, data, err => {
if(err) callback(err)
callback(null, data)
})
}

module.exports.timer = doneCallback => {
setTimeout(() => {
doneCallback('process complete')
}, 3000)
}
// 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)

module.exports.getBooks = (filename, callback) => {
fs.readFile(`./${filename}.json`, 'utf8', (err, data) => {
@@ -6,20 +6,23 @@ const math = require('./math')
const fs = require('fs')

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

// override the implementation
addMock.mockImplementation(() => 'mock')
const addMock = jest.spyOn(math, 'add') // creates a Jest Mock function
addMock.mockImplementation(() => 'mock') // override the implementation
expect(app.doAdd(1, 2)).toEqual('mock')
expect(addMock).toHaveBeenCalledWith(1, 2)

// restore the original implementation
addMock.mockRestore()
addMock.mockRestore() // restores the original non-mocked implementation
expect(app.doAdd(1, 2)).toEqual(3)
console.log(addMock.mock.calls)
})

test('get a books collection', done => {
test('get the books collection', done => {
app.getBooks( 'books', (err, data) => {
expect(data).toContain('Alice in Wonderland')
if(err) console.error(err.message)
done()
})
})

test('get a fake books collection', done => {
const bookMock = jest.spyOn(app, 'getBooks')
bookMock.mockImplementation( (filename, callback) => {
fs.readFile('./fake.json', 'utf8', (err, data) => {
@@ -30,7 +33,20 @@ test('get a books collection', done => {
app.getBooks( 'books', (err, data) => {
expect(data).toContain('xxx')
if(err) console.error(err.message)
console.log(data)
bookMock.mockRestore()
done()
})
})

// test('is the fs spy working?', done => {

// })

test('get the fs module to throw an error', done => {
const fsMock = jest.spyOn(fs, 'readFile')
fsMock.mockImplementation( (filename, encoding, callback) => callback(new Error('foo')))
fs.readFile( 'books', 'utf8', err => {
expect(err.message).toContain('foo')
done()
})
})
@@ -2,6 +2,6 @@
'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
// module.exports.subtract = (a, b) => b - a
// module.exports.multiply = (a, b) => a * b
// module.exports.divide = (a, b) => b / a
@@ -12,8 +12,22 @@
"jest": {
"testEnvironment": "node",
"verbose": true,
"collectCoverage": true
},
"collectCoverage": true,
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": -10
}
},
"collectCoverageFrom": [
"**/*.{js,jsx}",
"!**/node_modules/**",
"!**/coverage/**"
]
}
,
"devDependencies": {
"jest": "^23.6.0"
},

0 comments on commit 7b34c48

Please sign in to comment.