diff --git a/spec/frontend/helpers/timeout.js b/spec/frontend/helpers/timeout.js new file mode 100644 index 0000000000000000000000000000000000000000..318593a48a414137decba451164e1ce804283a2e --- /dev/null +++ b/spec/frontend/helpers/timeout.js @@ -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)!`); + } + }); +}; diff --git a/spec/frontend/test_setup.js b/spec/frontend/test_setup.js index d892889b98d6e7382c8e1ced74f1dee3f82f9941..8c36d8ff49f54b0804764515af6fbab96ceaf97a 100644 --- a/spec/frontend/test_setup.js +++ b/spec/frontend/test_setup.js @@ -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 => {