2019-05-08 14:36:38 +00:00
|
|
|
const NS_PER_SEC = 1e9;
|
|
|
|
const NS_PER_MS = 1e6;
|
2019-06-05 21:28:40 +00:00
|
|
|
const IS_DEBUGGING = process.execArgv.join(' ').includes('--inspect-brk');
|
2019-03-08 14:07:44 +00:00
|
|
|
|
2019-05-08 14:36:38 +00:00
|
|
|
let testTimeoutNS;
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
export const setTestTimeout = (newTimeoutMS) => {
|
2019-06-06 16:52:20 +00:00
|
|
|
const newTimeoutNS = newTimeoutMS * NS_PER_MS;
|
|
|
|
// never accept a smaller timeout than the default
|
|
|
|
if (newTimeoutNS < testTimeoutNS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
testTimeoutNS = newTimeoutNS;
|
2019-05-08 14:36:38 +00:00
|
|
|
jest.setTimeout(newTimeoutMS);
|
2019-03-08 14:07:44 +00:00
|
|
|
};
|
|
|
|
|
2019-06-05 21:28:40 +00:00
|
|
|
// Allows slow tests to set their own timeout.
|
|
|
|
// Useful for tests with jQuery, which is very slow in big DOMs.
|
|
|
|
let temporaryTimeoutNS = null;
|
2020-12-23 21:10:24 +00:00
|
|
|
export const setTestTimeoutOnce = (newTimeoutMS) => {
|
2019-06-06 16:52:20 +00:00
|
|
|
const newTimeoutNS = newTimeoutMS * NS_PER_MS;
|
|
|
|
// never accept a smaller timeout than the default
|
|
|
|
if (newTimeoutNS < testTimeoutNS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
temporaryTimeoutNS = newTimeoutNS;
|
2019-06-05 21:28:40 +00:00
|
|
|
};
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
export const initializeTestTimeout = (defaultTimeoutMS) => {
|
2019-05-08 14:36:38 +00:00
|
|
|
setTestTimeout(defaultTimeoutMS);
|
2019-03-08 14:07:44 +00:00
|
|
|
|
|
|
|
let testStartTime;
|
|
|
|
|
|
|
|
// https://github.com/facebook/jest/issues/6947
|
|
|
|
beforeEach(() => {
|
2019-05-08 14:36:38 +00:00
|
|
|
testStartTime = process.hrtime();
|
2019-03-08 14:07:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2019-06-05 21:28:40 +00:00
|
|
|
let timeoutNS = testTimeoutNS;
|
|
|
|
if (Number.isFinite(temporaryTimeoutNS)) {
|
|
|
|
timeoutNS = temporaryTimeoutNS;
|
|
|
|
temporaryTimeoutNS = null;
|
|
|
|
}
|
|
|
|
|
2019-05-08 14:36:38 +00:00
|
|
|
const [seconds, remainingNs] = process.hrtime(testStartTime);
|
|
|
|
const elapsedNS = seconds * NS_PER_SEC + remainingNs;
|
|
|
|
|
2019-06-05 21:28:40 +00:00
|
|
|
// Disable the timeout error when debugging. It is meaningless because
|
|
|
|
// debugging always takes longer than the test timeout.
|
|
|
|
if (elapsedNS > timeoutNS && !IS_DEBUGGING) {
|
2019-05-08 14:36:38 +00:00
|
|
|
throw new Error(
|
2019-06-05 21:28:40 +00:00
|
|
|
`Test took too long (${elapsedNS / NS_PER_MS}ms > ${timeoutNS / NS_PER_MS}ms)!`,
|
2019-05-08 14:36:38 +00:00
|
|
|
);
|
2019-03-08 14:07:44 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|