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:
Fatih Acet 2019-04-01 20:55:17 +00:00
commit 9269ac1b69
2 changed files with 28 additions and 4 deletions

View file

@ -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();
}
}

View file

@ -5,10 +5,15 @@ import axios from '~/lib/utils/axios_utils';
import { initializeTestTimeout } from './helpers/timeout';
import { getJSONFixture, loadHTMLFixture, setHTMLFixture } from './helpers/fixtures';
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
afterEach(() => {
jest.runAllTimers();
});
}),
);
initializeTestTimeout(300);