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
This commit is contained in:
commit
9269ac1b69
2 changed files with 28 additions and 4 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue