diff options
author | Winnie Hellmann <winnie@gitlab.com> | 2019-03-08 15:07:44 +0100 |
---|---|---|
committer | Luke Bennett <lbennett@gitlab.com> | 2019-03-08 16:21:10 +0000 |
commit | f0666830710568398119cc3251016d633387d29e (patch) | |
tree | 730a7a08de8bce221d4969ac35f1105b8d66dac1 | |
parent | 26477f388952f9b8d02fcca6ac4395915e48218c (diff) | |
download | gitlab-ce-f0666830710568398119cc3251016d633387d29e.tar.gz |
Add setTestTimeout for Jest tests
Allows contributors to set the timeout
for individual jest tests.
-rw-r--r-- | spec/frontend/helpers/timeout.js | 24 | ||||
-rw-r--r-- | spec/frontend/test_setup.js | 18 |
2 files changed, 26 insertions, 16 deletions
diff --git a/spec/frontend/helpers/timeout.js b/spec/frontend/helpers/timeout.js new file mode 100644 index 00000000000..318593a48a4 --- /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 d892889b98d..8c36d8ff49f 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 => { |