summaryrefslogtreecommitdiff
path: root/spec/frontend/helpers/timeout.js
blob: 318593a48a414137decba451164e1ce804283a2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
let testTimeoutInMs;

export const setTestTimeout = newTimeoutInMs => {
  testTimeoutInMs = newTimeoutInMs;
  jest.setTimeout(newTimeoutInMs);
};

export const initializeTestTimeout = defaultTimeoutInMs => {
  setTestTimeout(defaultTimeoutInMs);

  let testStartTime;

  // https://github.com/facebook/jest/issues/6947
  beforeEach(() => {
    testStartTime = Date.now();
  });

  afterEach(() => {
    const elapsedTimeInMs = Date.now() - testStartTime;
    if (elapsedTimeInMs > testTimeoutInMs) {
      throw new Error(`Test took too long (${elapsedTimeInMs}ms > ${testTimeoutInMs}ms)!`);
    }
  });
};