diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 21:42:06 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 21:42:06 +0300 |
commit | 6e4e1050d9dba2b7b2523fdd1768823ab85feef4 (patch) | |
tree | 78be5963ec075d80116a932011d695dd33910b4e /spec/frontend_integration/test_helpers/utils/overclock_timers.js | |
parent | 1ce776de4ae122aba3f349c02c17cebeaa8ecf07 (diff) |
Add latest changes from gitlab-org/gitlab@13-3-stable-ee
Diffstat (limited to 'spec/frontend_integration/test_helpers/utils/overclock_timers.js')
-rw-r--r-- | spec/frontend_integration/test_helpers/utils/overclock_timers.js | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/spec/frontend_integration/test_helpers/utils/overclock_timers.js b/spec/frontend_integration/test_helpers/utils/overclock_timers.js new file mode 100644 index 00000000000..046c7f8e527 --- /dev/null +++ b/spec/frontend_integration/test_helpers/utils/overclock_timers.js @@ -0,0 +1,65 @@ +/** + * This function replaces the existing `setTimeout` and `setInterval` with wrappers that + * discount the `ms` passed in by `boost`. + * + * For example, if a module has: + * + * ``` + * setTimeout(cb, 100); + * ``` + * + * But a test has: + * + * ``` + * useOverclockTimers(25); + * ``` + * + * Then the module's call to `setTimeout` effectively becomes: + * + * ``` + * setTimeout(cb, 4); + * ``` + * + * It's important to note that the timing for `setTimeout` and order of execution is non-deterministic + * and discounting the `ms` passed could make this very obvious and expose some underlying issues + * with flaky failures. + * + * WARNING: If flaky spec failures show up in a spec that is using this helper, please consider either: + * + * - Refactoring the production code so that it's reactive to state changes, not dependent on timers. + * - Removing the call to this helper from the spec. + * + * @param {Number} boost + */ +// eslint-disable-next-line import/prefer-default-export +export const useOverclockTimers = (boost = 50) => { + if (boost <= 0) { + throw new Error(`[overclock_timers] boost (${boost}) cannot be <= 0`); + } + + let origSetTimeout; + let origSetInterval; + const newSetTimeout = (fn, msParam = 0) => { + const ms = msParam > 0 ? Math.floor(msParam / boost) : msParam; + + return origSetTimeout(fn, ms); + }; + const newSetInterval = (fn, msParam = 0) => { + const ms = msParam > 0 ? Math.floor(msParam / boost) : msParam; + + return origSetInterval(fn, ms); + }; + + beforeEach(() => { + origSetTimeout = global.setTimeout; + origSetInterval = global.setInterval; + + global.setTimeout = newSetTimeout; + global.setInterval = newSetInterval; + }); + + afterEach(() => { + global.setTimeout = origSetTimeout; + global.setInterval = origSetInterval; + }); +}; |