Skip to content

Javascript unit testing

ΖΛΚ edited this page Dec 13, 2017 · 2 revisions

Jest

At Nimbletank we use Jest javascript unit test

Install Jest

Install Jest using npm:

npm install --save-dev jest

Or via yarn:

yarn add --dev jest

Test script for package.json

"test": "jest --verbose",
"test:watch": "jest --watch --verbose",
"test:coverage": "jest --coverage --verbose"

How to run the above sripts

npm run test
npm run test:watch
npm run test:coverage

Usage example

http://facebook.github.io/jest/docs/en/using-matchers.html#content

toBe

The simplest way to test a value is with exact equality.

test('two plus two is four', () => {
  expect(2 + 2).toBe(4);
});

To test a function value is with exact equality.

test('Title for the test case', () => {
    let result = function(value);
    expect(result).toBe(expectedValue);
});

toEqual

test('Title for the test case', () => {
    let result = function(value);
    expect(result).toEqual(expectedValue);
});

Configuration

http://facebook.github.io/jest/docs/en/configuration.html

jest.config.js config

We will be using jest.config.js config instead of package.json config

module.exports = {
    verbose: true,
    coveragePathIgnorePatterns: ["./lib/"]
};

More config options here