diff options
author | Fatih Acet <acetfatih@gmail.com> | 2019-04-01 20:55:17 +0000 |
---|---|---|
committer | Fatih Acet <acetfatih@gmail.com> | 2019-04-01 20:55:17 +0000 |
commit | 9269ac1b6951aa5d239c77bab36bd736b8e71b12 (patch) | |
tree | 8fba6162e26cadd08e1ca3b686d00f43e7337a41 /spec/frontend | |
parent | 2f4a623817618d89866915e585396debb220a7a0 (diff) | |
parent | 0e9c092d003fb9febcbaf9fd33ddbbb40ed3191d (diff) | |
download | gitlab-ce-9269ac1b6951aa5d239c77bab36bd736b8e71b12.tar.gz |
Merge branch 'winh-rejected-jest-promises' into 'master'
Fail Jest tests for unhandled Promise rejections
Closes #56053
See merge request gitlab-org/gitlab-ce!26424
Diffstat (limited to 'spec/frontend')
-rw-r--r-- | spec/frontend/environment.js | 19 | ||||
-rw-r--r-- | spec/frontend/test_setup.js | 13 |
2 files changed, 28 insertions, 4 deletions
diff --git a/spec/frontend/environment.js b/spec/frontend/environment.js index 1067a53906a..34df8019a2e 100644 --- a/spec/frontend/environment.js +++ b/spec/frontend/environment.js @@ -27,6 +27,25 @@ class CustomEnvironment extends JSDOMEnvironment { this.global.gon = { ee: testEnvironmentOptions.IS_EE, }; + + this.rejectedPromises = []; + + this.global.promiseRejectionHandler = error => { + this.rejectedPromises.push(error); + }; + } + + async teardown() { + await new Promise(setImmediate); + + if (this.rejectedPromises.length > 0) { + throw new ErrorWithStack( + `Unhandled Promise rejections: ${this.rejectedPromises.join(', ')}`, + this.teardown, + ); + } + + await super.teardown(); } } diff --git a/spec/frontend/test_setup.js b/spec/frontend/test_setup.js index f09bcb5407e..c57e0e7cfc6 100644 --- a/spec/frontend/test_setup.js +++ b/spec/frontend/test_setup.js @@ -5,10 +5,15 @@ import axios from '~/lib/utils/axios_utils'; import { initializeTestTimeout } from './helpers/timeout'; import { getJSONFixture, loadHTMLFixture, setHTMLFixture } from './helpers/fixtures'; -// wait for pending setTimeout()s -afterEach(() => { - jest.runAllTimers(); -}); +process.on('unhandledRejection', global.promiseRejectionHandler); + +afterEach(() => + // give Promises a bit more time so they fail the right test + new Promise(setImmediate).then(() => { + // wait for pending setTimeout()s + jest.runAllTimers(); + }), +); initializeTestTimeout(300); |