Skip to main content

Command Palette

Search for a command to run...

JEST matcher functions

Published
View as Markdown
  1. Introduction
    In the Jest testing framework, matchers are functions that allow you to validate various conditions in your tests. They are used to assert that a value meets certain criteria. Matchers are often used with the expect function to create assertions in tests.
    Matchers in Jest provide a flexible way to assert different conditions in your tests, from basic equality and truthiness to complex conditions and custom rules. They are essential tools for writing comprehensive and reliable tests.

  2. Basic Matchers
    i) toBe(value): Checks if a value is exactly equal (using ===) to the expected value.

    ii) toEqual(value): Checks if a value is deeply equal to the expected value (useful for objects and arrays).

    iii) not: Used to negate a matcher.

     expect(2 + 2).not.toBe(5);
    
  3. Truthiness Matchers
    i) toBeNull(): Checks if a value is null.

    ii) toBeUndefined(): Checks if a value is undefined.

    iii) toBeDefined(): Checks if a value is not undefined.

    iv) toBeTruthy(): Checks if a value is truthy.

    v) toBeFalsy(): Checks if a value is falsy.

     expect(null).toBeNull();
     expect(undefined).toBeUndefined();
     expect(true).toBeTruthy();
     expect(false).toBeFalsy();
    
  4. Number Matchers
    i) toBeGreaterThan(number): Checks if a value is greater than the expected number.

    ii) toBeGreaterThanOrEqual(number): Checks if a value is greater than or equal to the expected number.

    iii) toBeLessThan(number): Checks if a value is less than the expected number.

    iv) toBeLessThanOrEqual(number): Checks if a value is less than or equal to the expected number.

    v) toBeCloseTo(number, numDigits): Checks if a value is close to the expected number, useful for floating-point comparisons.

     expect(10).toBeGreaterThan(5);
     expect(4.2).toBeLessThan(5);
     expect(0.1 + 0.2).toBeCloseTo(0.3, 5); // Floating point comparison
    
  5. String Matchers
    i) toMatch(regexOrString): Checks if a string matches a regular expression or contains a substring.

     expect('hello world').toMatch(/world/);
     expect('hello world').toMatch('world');
    
  6. Array and Iterable Matchers
    i) toContain(item): Checks if an array or iterable contains a specific item.

    ii) toContainEqual(item): Checks if an array or iterable contains an item that is deeply equal to the expected item.

    iii) toHaveLength(number): Checks if an array or iterable has a specific length.

     expect([1, 2, 3]).toContain(2);
     expect([{a: 1}, {b: 2}]).toContainEqual({a: 1});
     expect([1, 2, 3]).toHaveLength(3);
    
  7. Exception Matchers
    i) toThrow(error?): Checks if a function throws an error when it is called. You can optionally specify the error message, constructor, or a regex to match the error.

     expect(() => {
       throw new Error('something bad happened');
     }).toThrow('something bad happened');
    
     expect(() => {
       throw new Error('something bad happened');
     }).toThrow(/bad/);