Skip to content

Commit 19886b5

Browse files
authored
chore: refactor callAsyncCircusFn parameters (#10629)
1 parent d077f2e commit 19886b5

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
- `[docs]` Add step for fetching `backers.json` file in website setup docs ([#10631](https://github.com/facebook/jest/pull/10631))
1414
- `[docs]` Add page detailing environment variables set by Jest ([#10630](https://github.com/facebook/jest/pull/10630))
15+
- `[jest-circus]` Refactor `callAsyncCircusFn` parameters ([#10629](https://github.com/facebook/jest/pull/10629))
1516

1617
### Performance
1718

packages/jest-circus/src/run.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ const _callCircusHook = async ({
143143
const timeout = hook.timeout || getState().testTimeout;
144144

145145
try {
146-
await callAsyncCircusFn(hook.fn, testContext, hook.asyncError, {
146+
await callAsyncCircusFn(hook, testContext, {
147147
isHook: true,
148148
timeout,
149149
});
@@ -166,7 +166,7 @@ const _callCircusTest = async (
166166
}
167167

168168
try {
169-
await callAsyncCircusFn(test.fn, testContext, test.asyncError, {
169+
await callAsyncCircusFn(test, testContext, {
170170
isHook: false,
171171
timeout,
172172
});

packages/jest-circus/src/utils.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import * as path from 'path';
99
import type {Circus} from '@jest/types';
10-
import {convertDescriptorToString, formatTime} from 'jest-util';
10+
import {ErrorWithStack, convertDescriptorToString, formatTime} from 'jest-util';
1111
import isGeneratorFn from 'is-generator-fn';
1212
import co from 'co';
1313
import dedent = require('dedent');
@@ -43,7 +43,7 @@ export const makeDescribe = (
4343
};
4444

4545
export const makeTest = (
46-
fn: Circus.TestFn | undefined,
46+
fn: Circus.TestFn,
4747
mode: Circus.TestMode,
4848
name: Circus.TestName,
4949
parent: Circus.DescribeBlock,
@@ -159,17 +159,18 @@ function checkIsError(error: unknown): error is Error {
159159
}
160160

161161
export const callAsyncCircusFn = (
162-
fn: Circus.AsyncFn,
162+
testOrHook: Circus.TestEntry | Circus.Hook,
163163
testContext: Circus.TestContext | undefined,
164-
asyncError: Circus.Exception,
165-
{isHook, timeout}: {isHook?: boolean | null; timeout: number},
164+
{isHook, timeout}: {isHook: boolean; timeout: number},
166165
): Promise<unknown> => {
167166
let timeoutID: NodeJS.Timeout;
168167
let completed = false;
169168

169+
const {fn, asyncError} = testOrHook;
170+
170171
return new Promise((resolve, reject) => {
171172
timeoutID = setTimeout(
172-
() => reject(_makeTimeoutMessage(timeout, !!isHook)),
173+
() => reject(_makeTimeoutMessage(timeout, isHook)),
173174
timeout,
174175
);
175176

@@ -179,7 +180,7 @@ export const callAsyncCircusFn = (
179180
let returnedValue: unknown = undefined;
180181
const done = (reason?: Error | string): void => {
181182
// We need to keep a stack here before the promise tick
182-
const errorAtDone = new Error();
183+
const errorAtDone = new ErrorWithStack(undefined, done);
183184
// Use `Promise.resolve` to allow the event loop to go a single tick in case `done` is called synchronously
184185
Promise.resolve().then(() => {
185186
if (returnedValue !== undefined) {

packages/jest-types/src/Circus.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export type SyncEvent =
6666
asyncError: Error;
6767
name: 'add_test';
6868
testName: TestName;
69-
fn?: TestFn;
69+
fn: TestFn;
7070
mode?: TestMode;
7171
timeout: number | undefined;
7272
}
@@ -231,7 +231,7 @@ export type TestEntry = {
231231
type: 'test';
232232
asyncError: Exception; // Used if the test failure contains no usable stack trace
233233
errors: Array<TestError>;
234-
fn?: TestFn;
234+
fn: TestFn;
235235
invocations: number;
236236
mode: TestMode;
237237
name: TestName;

0 commit comments

Comments
 (0)