Skip to content
代码片段 群组 项目
提交 28a23db3 编辑于 作者: Vitaly Slobodin's avatar Vitaly Slobodin
浏览文件

Use Jest's built-in slowTestThreshold for detecting slow tests

上级 06e8a8d8
No related branches found
No related tags found
无相关合并请求
......@@ -169,6 +169,7 @@ module.exports = (path, options = {}) => {
resolver: './jest_resolver.js',
setupFilesAfterEnv: [`<rootDir>/${path}/test_setup.js`, 'jest-canvas-mock'],
restoreMocks: true,
slowTestThreshold: process.env.CI ? 6000 : 500,
transform: {
'^.+\\.(gql|graphql)$': 'jest-transform-graphql',
'^.+_worker\\.js$': './spec/frontend/__helpers__/web_worker_transformer.js',
......
const NS_PER_SEC = 1e9;
const NS_PER_MS = 1e6;
const IS_DEBUGGING = process.execArgv.join(' ').includes('--inspect-brk');
let testTimeoutNS;
export const setTestTimeout = (newTimeoutMS) => {
const newTimeoutNS = newTimeoutMS * NS_PER_MS;
// never accept a smaller timeout than the default
if (newTimeoutNS < testTimeoutNS) {
return;
}
testTimeoutNS = newTimeoutNS;
jest.setTimeout(newTimeoutMS);
};
// Allows slow tests to set their own timeout.
// Useful for tests with jQuery, which is very slow in big DOMs.
let temporaryTimeoutNS = null;
export const setTestTimeoutOnce = (newTimeoutMS) => {
const newTimeoutNS = newTimeoutMS * NS_PER_MS;
// never accept a smaller timeout than the default
if (newTimeoutNS < testTimeoutNS) {
return;
}
temporaryTimeoutNS = newTimeoutNS;
};
export const initializeTestTimeout = (defaultTimeoutMS) => {
setTestTimeout(defaultTimeoutMS);
let testStartTime;
// https://github.com/facebook/jest/issues/6947
beforeEach(() => {
testStartTime = process.hrtime();
});
afterEach(() => {
let timeoutNS = testTimeoutNS;
if (Number.isFinite(temporaryTimeoutNS)) {
timeoutNS = temporaryTimeoutNS;
temporaryTimeoutNS = null;
}
const [seconds, remainingNs] = process.hrtime(testStartTime);
const elapsedNS = seconds * NS_PER_SEC + remainingNs;
// Disable the timeout error when debugging. It is meaningless because
// debugging always takes longer than the test timeout.
if (elapsedNS > timeoutNS && !IS_DEBUGGING) {
throw new Error(
`Test took too long (${elapsedNS / NS_PER_MS}ms > ${timeoutNS / NS_PER_MS}ms)!`,
);
}
});
};
......@@ -2,9 +2,6 @@
// eslint-disable-next-line no-restricted-syntax
import { setImmediate } from 'timers';
import 'helpers/shared_test_setup';
import { initializeTestTimeout } from 'helpers/timeout';
initializeTestTimeout(process.env.CI ? 6000 : 500);
afterEach(() =>
// give Promises a bit more time so they fail the right test
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册