NodeJS - Testing with expect and/or jest

In this article, I’m going to illustrate how to use expect to write test cases.

Please view part-1 of setting up mocha for testing

npm init
npm install mocha --save-dev # this will install mocha as developer dependencies.
npm install expect@1.20.2 --save-dev
npm i jest --save-dev

Here is the sample file to be tested.

// utils.js
module.exports.add = (a, b) => a + b;
module.exports.square = (a) => a * a;

Here is the file for sample code having test cases.

const expect = require('expect');
const utils = require('./utils');

it('should add two numbers', () => {
    var result = utils.add(10, 20);

it('should square a number', () => {
    var result = utils.square(10);

// Extra test function demonstrating different functions of expect library.

it('should expect some values', () => {
    // if both the objects are same
    expect({name: 'Sagar'}).toEqual({name: 'Sagar'});

    // if both the objects are not same
    expect({name: 'Sagar'}).toNotEqual({name: 'Amit'});

    // if it has

    // if it doesn't have

    // if object has a specific property
        name: 'Sagar',
        age: 29,
        location: 'Hyderabad'
        name: 'Sagar'

    // if object does NOT have a specific property
        name: 'Sagar',
        age: 29,
        location: 'Hyderabad'
        name: 'Amit'