Skip to content

Commit

Permalink
feat: mutate test/hook's timeout before events
Browse files Browse the repository at this point in the history
  • Loading branch information
FauxFaux committed Dec 3, 2020
1 parent 7c73368 commit 4603d5d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
10 changes: 8 additions & 2 deletions packages/jest-circus/src/eventHandler.ts
Expand Up @@ -32,7 +32,10 @@ const eventHandler: Circus.EventHandler = (
}
case 'hook_start': {
if (state.currentlyRunningTest) {
state.currentlyRunningTest.deadline = deadlineFor(event.timeout);
if (undefined === event.hook.timeout || null == event.hook.timeout) {
throw new Error('expected timeout to be set before event fired');
}
state.currentlyRunningTest.deadline = deadlineFor(event.hook.timeout);
}
break;
}
Expand Down Expand Up @@ -192,7 +195,10 @@ const eventHandler: Circus.EventHandler = (
break;
}
case 'test_fn_start': {
event.test.deadline = deadlineFor(event.timeout);
if (undefined === event.test.timeout) {
throw new Error('expected timeout to be set before event fired');
}
event.test.deadline = deadlineFor(event.test.timeout);
break;
}
case 'test_fn_failure': {
Expand Down
12 changes: 6 additions & 6 deletions packages/jest-circus/src/run.ts
Expand Up @@ -148,13 +148,13 @@ const _callCircusHook = async ({
test?: Circus.TestEntry;
testContext?: Circus.TestContext;
}): Promise<void> => {
const timeout = hook.timeout || getState().testTimeout;
await dispatch({hook, name: 'hook_start', timeout});
hook.timeout = hook.timeout || getState().testTimeout;
await dispatch({hook, name: 'hook_start'});

try {
await callAsyncCircusFn(hook, testContext, {
isHook: true,
timeout,
timeout: hook.timeout,
});
await dispatch({describeBlock, hook, name: 'hook_success', test});
} catch (error) {
Expand All @@ -166,8 +166,8 @@ const _callCircusTest = async (
test: Circus.TestEntry,
testContext: Circus.TestContext,
): Promise<void> => {
const timeout = test.timeout || getState().testTimeout;
await dispatch({name: 'test_fn_start', test, timeout});
test.timeout = test.timeout || getState().testTimeout;
await dispatch({name: 'test_fn_start', test});
invariant(test.fn, `Tests with no 'fn' should have 'mode' set to 'skipped'`);

if (test.errors.length) {
Expand All @@ -177,7 +177,7 @@ const _callCircusTest = async (
try {
await callAsyncCircusFn(test, testContext, {
isHook: false,
timeout,
timeout: test.timeout,
});
await dispatch({name: 'test_fn_success', test});
} catch (error) {
Expand Down
2 changes: 0 additions & 2 deletions packages/jest-types/src/Circus.ts
Expand Up @@ -91,7 +91,6 @@ export type AsyncEvent =
| {
name: 'hook_start';
hook: Hook;
timeout: number;
}
| {
name: 'hook_success';
Expand All @@ -109,7 +108,6 @@ export type AsyncEvent =
| {
name: 'test_fn_start';
test: TestEntry;
timeout: number;
}
| {
name: 'test_fn_success';
Expand Down

0 comments on commit 4603d5d

Please sign in to comment.