Everything we write should be tested. When I grade your code I’m really testing it to make sure it does what its is supposed to do.
When you write a test, you are able to make sure say what your code it doing. So if you make a change to the code that you have a test for, you are sure that it will still do the things that it is supposed to do.
Every language has its own testing tools for
JavaScript jest
is a regularly used
one.
npm install --save-dev jest
This will add the Jest library to the application so we can use it to run tests.
"scripts": {
"start": "node ./bin/www",
"dev": "nodemon ./bin/www --ignore sessions",
"refresh": "node migrate",
"test": "jest"
},
npm test
mkdir test
touch sum.test.js
```JS
const sum = require('../sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});~
function sum(a,b) {
return 3;
}
module.exports = sum;
function sum(a,b) {
return a+b;
}
module.exports = sum;
/* eslint-disable no-undef */