Skip to content

Commit cb2630b

Browse files
authored
chore: fix broken test in circus (#8009)
1 parent 591eb99 commit cb2630b

File tree

2 files changed

+69
-66
lines changed

2 files changed

+69
-66
lines changed

e2e/__tests__/detectOpenHandles.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ try {
2222
}
2323
}
2424

25-
function getTextAfterTest(stderr) {
25+
function getTextAfterTest(stderr: string) {
2626
return (stderr.split(/Ran all test suites(.*)\n/)[2] || '').trim();
2727
}
2828

packages/jest-circus/src/utils.ts

+68-65
Original file line numberDiff line numberDiff line change
@@ -157,89 +157,92 @@ function checkIsError(error: any): error is Error {
157157
return !!(error && (error as Error).message && (error as Error).stack);
158158
}
159159

160-
export const callAsyncCircusFn = async (
160+
export const callAsyncCircusFn = (
161161
fn: AsyncFn,
162162
testContext: TestContext | undefined,
163163
{isHook, timeout}: {isHook?: boolean | null; timeout: number},
164164
): Promise<any> => {
165-
let timeoutID: NodeJS.Timeout | undefined;
165+
let timeoutID: NodeJS.Timeout;
166166
let completed = false;
167167

168-
try {
169-
return await new Promise((resolve, reject) => {
170-
timeoutID = setTimeout(
171-
() => reject(_makeTimeoutMessage(timeout, !!isHook)),
172-
timeout,
173-
);
174-
175-
// If this fn accepts `done` callback we return a promise that fulfills as
176-
// soon as `done` called.
177-
if (fn.length) {
178-
const done = (reason?: Error | string): void => {
179-
const errorAsErrorObject = checkIsError(reason)
180-
? reason
181-
: new Error(`Failed: ${prettyFormat(reason, {maxDepth: 3})}`);
182-
183-
// Consider always throwing, regardless if `reason` is set or not
184-
if (completed && reason) {
185-
errorAsErrorObject.message =
186-
'Caught error after test environment was torn down\n\n' +
187-
errorAsErrorObject.message;
188-
189-
throw errorAsErrorObject;
190-
}
168+
return new Promise((resolve, reject) => {
169+
timeoutID = setTimeout(
170+
() => reject(_makeTimeoutMessage(timeout, !!isHook)),
171+
timeout,
172+
);
173+
174+
// If this fn accepts `done` callback we return a promise that fulfills as
175+
// soon as `done` called.
176+
if (fn.length) {
177+
const done = (reason?: Error | string): void => {
178+
const errorAsErrorObject = checkIsError(reason)
179+
? reason
180+
: new Error(`Failed: ${prettyFormat(reason, {maxDepth: 3})}`);
181+
182+
// Consider always throwing, regardless if `reason` is set or not
183+
if (completed && reason) {
184+
errorAsErrorObject.message =
185+
'Caught error after test environment was torn down\n\n' +
186+
errorAsErrorObject.message;
187+
188+
throw errorAsErrorObject;
189+
}
191190

192-
return reason ? reject(errorAsErrorObject) : resolve();
193-
};
191+
return reason ? reject(errorAsErrorObject) : resolve();
192+
};
194193

195-
return fn.call(testContext, done);
196-
}
194+
return fn.call(testContext, done);
195+
}
197196

198-
let returnedValue;
199-
if (isGeneratorFn(fn)) {
200-
returnedValue = co.wrap(fn).call({});
201-
} else {
202-
try {
203-
returnedValue = fn.call(testContext);
204-
} catch (error) {
205-
return reject(error);
206-
}
197+
let returnedValue;
198+
if (isGeneratorFn(fn)) {
199+
returnedValue = co.wrap(fn).call({});
200+
} else {
201+
try {
202+
returnedValue = fn.call(testContext);
203+
} catch (error) {
204+
return reject(error);
207205
}
206+
}
208207

209-
// If it's a Promise, return it. Test for an object with a `then` function
210-
// to support custom Promise implementations.
211-
if (
212-
typeof returnedValue === 'object' &&
213-
returnedValue !== null &&
214-
typeof returnedValue.then === 'function'
215-
) {
216-
return returnedValue.then(resolve, reject);
217-
}
208+
// If it's a Promise, return it. Test for an object with a `then` function
209+
// to support custom Promise implementations.
210+
if (
211+
typeof returnedValue === 'object' &&
212+
returnedValue !== null &&
213+
typeof returnedValue.then === 'function'
214+
) {
215+
return returnedValue.then(resolve, reject);
216+
}
218217

219-
if (!isHook && returnedValue !== void 0) {
220-
return reject(
221-
new Error(
222-
`
218+
if (!isHook && returnedValue !== void 0) {
219+
return reject(
220+
new Error(
221+
`
223222
test functions can only return Promise or undefined.
224223
Returned value: ${String(returnedValue)}
225224
`,
226-
),
227-
);
228-
}
225+
),
226+
);
227+
}
229228

230-
// Otherwise this test is synchronous, and if it didn't throw it means
231-
// it passed.
232-
return resolve();
233-
});
234-
} finally {
235-
completed = true;
236-
// If timeout is not cleared/unrefed the node process won't exit until
237-
// it's resolved.
238-
if (timeoutID) {
229+
// Otherwise this test is synchronous, and if it didn't throw it means
230+
// it passed.
231+
return resolve();
232+
})
233+
.then(() => {
234+
completed = true;
235+
// If timeout is not cleared/unrefed the node process won't exit until
236+
// it's resolved.
239237
timeoutID.unref && timeoutID.unref();
240238
clearTimeout(timeoutID);
241-
}
242-
}
239+
})
240+
.catch(error => {
241+
completed = true;
242+
timeoutID.unref && timeoutID.unref();
243+
clearTimeout(timeoutID);
244+
throw error;
245+
});
243246
};
244247

245248
export const getTestDuration = (test: TestEntry): number | null => {

0 commit comments

Comments
 (0)