S
S
ScarletFlash2019-06-14 10:48:56
JavaScript
ScarletFlash, 2019-06-14 10:48:56

How to speed up the passage of unit tests on JS?

Hello!
I cover the application with tests. I decided to start with helpers as the most stable parts of the application.
Previously, I practically did not write tests, and in general, there is no certainty that I am doing everything correctly ...).
Jest is used for testing. The application itself is in TypeScript.
After writing a certain number of tests with a certain number of cases, I realized that each helper takes, on average, 10 seconds, which, it seems to me, is too much for atomic functions without external dependencies. Cases in the test, usually about 3x. It happens more, but the passage time is still about the same. The working machine is powerful - 16 GB of RAM, Core i7. The shell is WSL.
I'm wondering how long these tests take you.

Test Example

import { nameMock } from '@app/mocks';

import { incrementName } from './increment-name.helper';

describe('increment-name.helper.ts', () => {
  const nameSample: string = nameMock;
  const incrementedNameSample: string = `${nameMock} (1)`;
  const incrementedTwiceNameSample: string = `${nameMock} (2)`;

  it('should not increment name if namespace is empty', () => {
    expect(incrementName(nameSample)).toBe(nameSample);
  });

  it('should increment name if there is the same name in namespace', () => {
    expect(incrementName(nameSample, [nameSample])).toBe(incrementedNameSample);
  });

  it('should increment name if there is incremented name in namespace', () => {
    expect(incrementName(nameSample, [nameSample, incrementedNameSample])).toBe(incrementedTwiceNameSample);
  });
});

Helper example

import { isNullOrUndefined } from './is-null-or-undefined.helper';

const getCurrentIncrement: (stringToCheck: string) => number = (stringToCheck: string): number => {
  const incrementedNamePattern: RegExp = new RegExp(/.*[ ][(](\d){1,}[)]$/, 'gm');
  const incrementPattern: RegExp = new RegExp(/[(](\d){1,}[)]$/, 'gm');

  const stringMatchesNamePattern: boolean = incrementedNamePattern.test(stringToCheck);
  if (!stringMatchesNamePattern) {
    return 0;
  }
  const bracedIncrement: string = incrementPattern.exec(stringToCheck)[0];
  enum bracketIndex {
    left = 1,
    right = bracedIncrement.length - 1
  }
  const increment: number = Number(bracedIncrement.substring(bracketIndex.left, bracketIndex.right));
  return !isNullOrUndefined(increment) && !isNaN(increment) ? increment : 0;
};

export const incrementName: (currentName: string, namesArray?: string[]) => string = (
  currentName: string,
  namesArray: string[] = []
): string => {
  if (!Array.isArray(namesArray) || namesArray.length === 0) {
    return currentName;
  }
  const matchingNames: string[] = namesArray.filter((innerName: string) => innerName.includes(currentName));
  if (matchingNames.length === 0) {
    return currentName;
  }
  const highestExistingIncrement: number = matchingNames.reduce(
    (previousInnerIncrement: number, currentInnerName: string) => {
      const currentInnerIncrement: number = getCurrentIncrement(currentInnerName);
      return currentInnerIncrement > previousInnerIncrement ? currentInnerIncrement : previousInnerIncrement;
    },
    0
  );
  return `${currentName} (${highestExistingIncrement + 1})`;
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2019-06-14
@ScarletFlash

An integration test with raising / dropping two databases in docker containers and dynamically loading all the app files to build a DI container - 15 seconds - initialization of all this goodness, then the suite itself, as you're lucky. 10 seconds on average (about 10 tests inside).
These are requests, records to the database, receiving answers, building projections, writing to the second database, and so on.
Unit tests - 20-70ms per suite, up to 10 tests in each. Typescript + jest.
This is on a local laptop where the ide, browser, different virtual machines and so on. The server is faster.
If you have 10 seconds per unit test, then something is obviously going wrong. Most likely, the initialization of the environment takes so much time, what happens there - you need to understand it in detail.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question