Migrating from Mocha to Jest in nodeJS (loopback) app

In order to change the tests to Jest from Mocha, follow this simple procedure:-

Mocha requires the following libraries:

· Chai(let chai = require(‘chai’))

· chaiHttp (let chaiHttp = require(‘chai-http’))

· should (let should = chai.should())

· expect (const expect = require(‘chai’).expect )

Now, replace all the above by the following to achieve the same functionality:

· expect (const expect = require(‘expect’))

· request (const request = require(‘request’))

From the coding point of view, following changes should be done:

1. “Should” is substituted with “Expect”

2. “Equal” is supplanted by “toBe”

Configurational Changes:

Jest follows “convention over configuration” standard and requires little effort to set-up.

All you have to do is install Jest npm package and change “test” command in package.json to “jest”.

At this point, there is no need to specify which tests to run, Jest will trace them automatically as long as we comply to the convention of suffixing test file names with .test.js.

You can execute ES6, ES7, ES8, or ESWTF code simply by installing another npm module, babel-jest.

Jest will itself find the existing .babaelrc and you’ll be good to go.