gitlab-org--gitlab-foss/spec/frontend/helpers/timeout.js
Winnie Hellmann f066683071
Add setTestTimeout for Jest tests
Allows contributors to set the timeout
for individual jest tests.
2019-03-08 16:21:10 +00:00

24 lines
607 B
JavaScript

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)!`);
}
});
};