diff options
author | Winnie Hellmann <winnie@gitlab.com> | 2019-03-08 17:07:44 +0300 |
---|---|---|
committer | Luke Bennett <lbennett@gitlab.com> | 2019-03-08 19:21:10 +0300 |
commit | f0666830710568398119cc3251016d633387d29e (patch) | |
tree | 730a7a08de8bce221d4969ac35f1105b8d66dac1 /spec/frontend/helpers | |
parent | 26477f388952f9b8d02fcca6ac4395915e48218c (diff) |
Add setTestTimeout for Jest tests
Allows contributors to set the timeout
for individual jest tests.
Diffstat (limited to 'spec/frontend/helpers')
-rw-r--r-- | spec/frontend/helpers/timeout.js | 24 |
1 files changed, 24 insertions, 0 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)!`); + } + }); +}; |