Jest allows to write and execute test.
npm install --save-dev jest
# To use ES6 module import statements:
npm install --save-dev babel-jest @babel/core @babel/preset-env
In package.json:
{
"scripts": {
"test": "jest"
}
}
Create a babel.config.js in the root dir
module.exports = {
presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
};
Create a .test.js version of the .js file to test.
For example: sum.js -> sum.test.js
In the .test.js file:
import { functionToTest } from './sum.js'
test('Write comment here', () => {
expect(functionToTest(argument)).toBe('expected result');
});
// example:
test('Test if string is capitalize', () => {
expect(capitalize('Test')).toBe('TEST');
});
Then run npm test