Here's instructions on installing Jest and bootstrapping for ES6 module compilation for Node.
Note: This guide assumes you have NodeJS installed.
-
Create a new project folder for the code:
(Assuming project name is
new_kata
)mkdir new_kata cd new_kata
-
In the new project folder, create
package.json
yarn init
or
npm init
-
Add packages
yarn add --dev jest @babel/core @babel/preset-env
or
npm install --save-dev jest @babel/core @babel/preset-env
-
Add test running script in
package.json
"scripts": { "test": "jest", "test:watch": "jest --watch" }
-
Create a
.babelrc
with these content for a smart preset babel configuration{ "presets": [ ["@babel/preset-env", { "targets": { "node": "10" } } ] ] }
-
Create a code (
sum.js
) fileconst sum = (a, b) => a + b export default sum
-
Create a test (
sum.test.js
) fileimport sum from './sum' test('adds 1 + 2 to equal 3', () => { expect(sum(1, 2)).toBe(3); });
-
You can run the test:
yarn test
or
npm test
You should see this:
yarn run v1.9.4 $ jest PASS ./sum.test.js ✓ adds 1 + 2 to equal 3 (3ms) Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: 1.286s Ran all test suites. ✨ Done in 2.39s.
-
Read up more about the types of test matchers available in Jest: https://jestjs.io/docs/en/using-matchers