mkdir hello
cd hello
npm init
...
test command: jest --coverage
...
npm install --save-dev jest babel-jest babel-preset-env babel-preset-react
For more info, see: https://facebook.github.io/jest/docs/en/getting-started.html
vi .babelrc
{
"presets": ["env", "react"]
}
vi sum.js
const sum = (a, b) => {
return a + b;
};
export default sum;
vi sum.test.js
import sum from './sum';
test('adds 1+2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
jest
or,
npm test
PASS ./sum.test.js
✓ adds 1+2 to equal 3 (2ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.565s, estimated 1s
Ran all test suites.
----------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files | 100 | 100 | 100 | 100 | |
sum.js | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|----------------|
For babel 7, we need to use @babel/preset-env instead of babel-preset-env.
Thanks for the notes. Helped me set testing with Jest in ES6.