[Log] Jest: Getting Started

Official reference

Create a project directory.

npm init -y
npm i -D jest
touch index.js
touch calc.js

Write code.

function add(a, b) {
    return a + b
}
module.exports = add

Make test directory.

mkdir test
touch test/calc.test.js

Write test code.

const add = require('../calc');

test('adds 1 + 2 to equal 3', () => {
  expect(add(1, 2)).toBe(3);
});

Edit package.json.

  "main": "index.js",
  "scripts": {
    "test": "jest"
  },

Run test.

npm test

Screen Shot 2021-01-02 at 14.44.29.png

No Comments Yet