Commit 9269ac1b authored by Fatih Acet's avatar Fatih Acet

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
parents 2f4a6238 0e9c092d
...@@ -27,6 +27,25 @@ class CustomEnvironment extends JSDOMEnvironment { ...@@ -27,6 +27,25 @@ class CustomEnvironment extends JSDOMEnvironment {
this.global.gon = { this.global.gon = {
ee: testEnvironmentOptions.IS_EE, 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();
} }
} }
......
...@@ -5,10 +5,15 @@ import axios from '~/lib/utils/axios_utils'; ...@@ -5,10 +5,15 @@ import axios from '~/lib/utils/axios_utils';
import { initializeTestTimeout } from './helpers/timeout'; import { initializeTestTimeout } from './helpers/timeout';
import { getJSONFixture, loadHTMLFixture, setHTMLFixture } from './helpers/fixtures'; import { getJSONFixture, loadHTMLFixture, setHTMLFixture } from './helpers/fixtures';
// wait for pending setTimeout()s process.on('unhandledRejection', global.promiseRejectionHandler);
afterEach(() => {
afterEach(() =>
// give Promises a bit more time so they fail the right test
new Promise(setImmediate).then(() => {
// wait for pending setTimeout()s
jest.runAllTimers(); jest.runAllTimers();
}); }),
);
initializeTestTimeout(300); initializeTestTimeout(300);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment