Jest Testing Framework – Using Matchers

In this section, we will see how to use matchers in Jest that let you test values in different ways.

In functions.js

const functions = {
  add: (num1, num2) => {

    if (typeof(num1) !== 'number' || typeof(num2) !== 'number'){
      return null;
    }
    return num1 + num2;
  },

  isNull: () => null,
  checkValue: x => x,
  createUser: () => {
    const user = {firstname: 'Dave'};
    user['lastname'] = 'McDowell';
    return user;
  },
  getusers: () => ['Barbabas','Jennette','Cathlene','Ogden','Larry','Adrian'],

};

module.exports=functions;

In functions.test.js

describe("Matchers", () => {

  it("Should use toBe", ()=> {
    expect(functions.add(2,2)).toBe(4);
  });

  it("Should use not toBe", ()=> {
    expect(functions.add(10,30)).not.toBe(50);
  });

  it("Should use toBeNull", ()=> {
    expect(functions.isNull()).toBeNull();
    expect(functions.checkValue(0)).not.toBeNull();
    expect(functions.checkValue("Ping Pong")).not.toBeNull();
  });

  it("Should use toBeDefined", ()=> {
    expect(functions.checkValue(null)).toBeDefined();
    expect(functions.checkValue(0)).toBeDefined();
    expect(functions.checkValue(5)).toBeDefined();
  });

  it("Should use toBeUndefined", ()=> {
    expect(functions.checkValue(null)).not.toBeUndefined();
    expect(functions.checkValue(0)).not.toBeUndefined();
    expect(functions.checkValue(10)).not.toBeUndefined();
  });

  it("Should use toBeTruthy", ()=> {
    expect(functions.checkValue(null)).not.toBeTruthy();
    expect(functions.checkValue(0)).not.toBeTruthy();
    expect(functions.checkValue(20)).toBeTruthy();
  });

  it("Should use toBeFalsy", ()=> {
    expect(functions.checkValue(null)).toBeFalsy();
    expect(functions.checkValue(0)).toBeFalsy();
    expect(functions.checkValue(10)).not.toBeFalsy();
  });

  it("Should use toEqual", ()=> {
    expect(functions.createUser()).toEqual({ firstname: 'Dave', lastname: 'McDowell' });
  });

  it("Should use toBeGreaterThan, toBeGreaterThanOrEqual, toBeLessThan, toBeLessThanOrEqual", ()=> {
    expect(functions.add(5,6)).toBeGreaterThan(7);
    expect(functions.add(5,3)).toBeGreaterThanOrEqual(8);
    expect(functions.add(4,2)).toBeLessThan(10);
    expect(functions.add(3.5,4.5)).toBeLessThanOrEqual(8.5);
  });

  it('Should use toBeCloseTo', () => {
    expect(functions.add(2.331,3.223)).toBeCloseTo(5.55);  //Expected difference: < 0.005
  });

  test('Should use toMatch', () => {
    expect('Jest').not.toMatch(/k/);
    expect('Jest').toMatch(/j/i);  //ignore case sensitive
    expect('megaphone').toMatch(/GAP/i);
  });

  test('Should use toContain (Arrays)', () => {
    expect(functions.getusers()).toContain('Larry');
    expect(functions.getusers()).not.toContain('Peter');
  });

});

Results:

 
PASS ./functions.test.js

  Matchers
    ✓ Should use toBe
    ✓ Should use not toBe (1ms)
    ✓ Should use toBeNull (1ms)
    ✓ Should use toBeDefined (1ms)
    ✓ Should use toBeUndefined (1ms)
    ✓ Should use toBeTruthy
    ✓ Should use toBeFalsy (1ms)
    ✓ Should use toEqual (2ms)
    ✓ Should use toBeGreaterThan, toBeGreaterThanOrEqual, toBeLessThan, toBeLessThanOrEqual (2ms)
    ✓ Should use toBeCloseTo
    ✓ Should use toMatch (2ms)
    ✓ Should use toContain (Arrays) (1ms)


Test Suites: 1 passed, 1 total
Tests:       12 passed, 12 total
Snapshots:   0 total
Time:       1.364s, estimated 2s

Leave a Reply

Your email address will not be published. Required fields are marked *