Add setTestTimeout for Jest tests

Allows contributors to set the timeout
for individual jest tests.
This commit is contained in:
Winnie Hellmann 2019-03-08 15:07:44 +01:00 committed by Luke Bennett
parent 26477f3889
commit f066683071
No known key found for this signature in database
GPG key ID: 402ED51FB5D306C2
2 changed files with 26 additions and 16 deletions

View file

@ -0,0 +1,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)!`);
}
});
};

View file

@ -1,23 +1,9 @@
import Vue from 'vue';
import Translate from '~/vue_shared/translate';
import axios from '~/lib/utils/axios_utils';
import { initializeTestTimeout } from './helpers/timeout';
const testTimeoutInMs = 300;
jest.setTimeout(testTimeoutInMs);
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)!`);
}
});
initializeTestTimeout(300);
// fail tests for unmocked requests
beforeEach(done => {