Commit ad7d62e1 authored by Paul Slaughter's avatar Paul Slaughter

Use process timer for jest timeout testing

parent 603412af
let testTimeoutInMs; const NS_PER_SEC = 1e9;
const NS_PER_MS = 1e6;
export const setTestTimeout = newTimeoutInMs => { let testTimeoutNS;
testTimeoutInMs = newTimeoutInMs;
jest.setTimeout(newTimeoutInMs); export const setTestTimeout = newTimeoutMS => {
testTimeoutNS = newTimeoutMS * NS_PER_MS;
jest.setTimeout(newTimeoutMS);
}; };
export const initializeTestTimeout = defaultTimeoutInMs => { export const initializeTestTimeout = defaultTimeoutMS => {
setTestTimeout(defaultTimeoutInMs); setTestTimeout(defaultTimeoutMS);
let testStartTime; let testStartTime;
// https://github.com/facebook/jest/issues/6947 // https://github.com/facebook/jest/issues/6947
beforeEach(() => { beforeEach(() => {
testStartTime = Date.now(); testStartTime = process.hrtime();
}); });
afterEach(() => { afterEach(() => {
const elapsedTimeInMs = Date.now() - testStartTime; const [seconds, remainingNs] = process.hrtime(testStartTime);
if (elapsedTimeInMs > testTimeoutInMs) { const elapsedNS = seconds * NS_PER_SEC + remainingNs;
throw new Error(`Test took too long (${elapsedTimeInMs}ms > ${testTimeoutInMs}ms)!`);
if (elapsedNS > testTimeoutNS) {
throw new Error(
`Test took too long (${elapsedNS / NS_PER_MS}ms > ${testTimeoutNS / NS_PER_MS}ms)!`,
);
} }
}); });
}; };
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