Created
July 13, 2018 20:14
-
-
Save jremi/1c9c1cad343c9d44e6e44bc55f847e0f to your computer and use it in GitHub Desktop.
Basic example of using Jest for testing exported node module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Filename ./myModule.js | |
*/ | |
let a = (obj)=>{ | |
return obj.x * obj.y | |
} | |
let b = (obj)=>{ | |
return obj.x / obj.y | |
} | |
let c = (obj)=>{ | |
return obj.x + obj.y | |
} | |
module.exports = { | |
a, | |
b, | |
c | |
} | |
/* | |
Install Jest as dev dependency (npm install --save-dev jest) | |
Inside package.json set npm scripts.test to "jest" | |
Run from command line: npm test | |
Filename: ./tests/myModule.test.js | |
*/ | |
let myModule = require('../myModule') | |
it('should multiply two values', () => { | |
expect(myModule.a({ | |
x: 10, | |
y: 10 | |
})).toBe(100) | |
}) | |
it('should divide two values', () => { | |
expect(myModule.b({ | |
x: 10, | |
y: 10 | |
})).toBe(1) | |
}) | |
it('should add two values', () => { | |
expect(myModule.c({ | |
x: 10, | |
y: 10 | |
})).toBe(20) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment