test each jest code example

Example 1: jest timeout

beforeEach(function () {
        jest.setTimeout(2000) // ms
    });

Example 2: testing with jest

npm i jest --save -dev
-> In Package.json change the Script object(watchAll - jest will run auto)
"test" : "jest --watchAll"
-> test file should be named such way that re used by jest can recognize it
like filename.<test> or <spec> or skip it.js
Ex: sum.test.js or sum.spec.js or sum.js
-> require the target for testing file in this test file then enjoy testing.
-> visit jestWebsite-> Docs.

Example 3: jest multiple test cases

const add = (a, b) => a + b;

const cases = [[2, 2, 4], [-2, -2, -4], [2, -2, 0]];

describe("'add' utility", () => {
  test.each(cases)(
    "given %p and %p as arguments, returns %p",
    (firstArg, secondArg, expectedResult) => {
      const result = add(firstArg, secondArg);
      expect(result).toEqual(expectedResult);
    }
  );
});

Tags:

Misc Example