mkdir getting-started-with-jest && cd $_
npm init -y
npm i jest --save-dev
in package.json
"scripts": {
"test": "jest"
},
//make new file calculate.js
const mathOperations = {
sum: function(a,b) {
return a + b;
},
diff: function(a,b) {
return a - b;
},
product: function(a,b) {
return a * b
}
}
module.exports = mathOperations
//make test file name same but test.js extension calculate.test.js
const mathOperations = require('./calculate');
describe("Calculator tests", () => {
test('adding 1 + 2 should return 3', () => {
expect(mathOperations.sum(1, 2)).toBe(3);
});
test('suntract 10 - 5 should return 5', () => {
expect(mathOperations.diff(10,5)).toBe(5);
});
test("multiplying 2 and 8 should return 16", () => {
// arrange and act
var result = mathOperations.product(2,8)
// assert
expect(result).toBe(16);
});
})
// to run test type npm run test
0 Comments
Please do not enter any spam link in comment box.