Jest basic

Setup Jest: Getting started Jest with Typescript

toBe() vs toEqual()

toBe compares strict equality, using ===.
toEqual compares the values of two variables. If it’s an object or array, it checks the equality of all the properties or elements.

Run only the test group

1
2
3
4
5
describe.only("addition test", () => {
it("add 1 + 1 to be 2", () => {
expect(1 + 1).toBe(2);
});
});

Skip the test group

1
2
3
4
5
describe.skip("addition test", () => {
it("add 1 + 1 to be 2", () => {
expect(1 + 1).toBe(2);
});
});

Loop tests with multiple test cases

1
2
3
4
5
6
7
8
9
10
describe("Addition test", () => {
[
{ a: 1, b: 2, c: 3 },
{ a: 2, b: 4, c: 6 }
].forEach(testcase => {
it(`addition: ${testcase.a} + ${testcase.b} to be ${testcase.c}`, () => {
expect(testcase.a + testcase.b).toBe(testcase.c);
});
});
});

Run only fit tests

1
2
3
fit("add 1 + 1 to be 2", () => {
expect(1 + 1).toBe(2);
});

Skip only xit tests

1
2
3
xit("add 1 + 1 to be 2", () => {
expect(1 + 1).toBe(2);
});

References