jest (Javascript Library) Cheatsheet




Jest is a javascript library which privides utility functions to write unit tests. Jest also consists of testrunner which is used to generate a test report by running program in command line.

Installation

  1. Using NPM
npm install jest --save--dev

Note: Install jest as dev dependency

Popular jest matcher functions

Equality

Matcher_functionDescription
expect(...).toBe();Check for the equality of data
expect(...).toEqual()Check for the equality of data

Truthiness

Matcher_functionDescription
expect(...).toBeDefined();To check whether a function returning data or not
expect(...).toBeNull();Check whether something is null
expect(...).toBeTruthy();Check whether it returns true
expect(...).toBeFalsy();Check whether it returns false

Numbers

Matcher_functionDescription
expect(...).toBeGreaterThan();Checks whether number is greater or not
expect(...).toBeGreaterThanOrEqual();Checks whether number is greater or equal
expect(...).toBeLessThan()Checks whether number is smaller or not
expect(...).toBeLessThanOrEqual()Checks whether number is smaller or equal

Strings

Matcher_functionDescription
expect(...).toMatch(/regularExp/);

Arrays

Matcher_functionDescription
expect(...).toContain();Checks whether element is present in array or not

Objects

Matcher_functionDescription
expect(...).toBe();Check for the equality of object
expect(...).toEqual()Check for the equality of object
expect(...).toMatchObject()Check for the equality of object

Exceptions

Matcher_functionDescription
expect(() => { someCode }).toThrow();Check for the equality of exceptions


┬ęCopyright 2020 OneCompiler | Privacy Policy | Terms & Conditions