Skip to the content.

General testing

Here’s a list of rules to keep in mind when testing.

Other general rules:

Use .each to handle multiple possibilities

Supported by linters: NO

Incorrect:

describe('add', () => {
  describe('Given 1 and 2', () => {
    const firstValue = 1;
    const secondValue = 2;
    const expectedResult = 3;

    describe('When adding', () => {
      const result = add(firstValue, secondValue);

      test('Then sum', () => {
        expect(result).toBe(expectedResult);
      });
    });
  });

  // And so on
});

Correct:

describe('add', () => {
  describe.each([
    [1, 2, 3],
    [2, 3, 5],
    [3, 4, 7],
  ])('Given %i and %i', (firstValue: number, secondValue: number, expectedResult: number) => {
    describe('When adding', () => {
      const result = add(firstValue, secondValue);

      test(`Then sum to ${expectedResult}`, () => {
        expect(result).toBe(expectedResult);
      });
    });
  });
});