MCPcopy
hub / github.com/jestjs/jest / done

Function done

packages/jest-circus/src/utils.ts:218–266  ·  view source on GitHub ↗
(reason?: Error | string)

Source from the content-addressed store, hash-verified

216 let returnedValue: unknown = undefined;
217
218 const done = (reason?: Error | string): void => {
219 // We need to keep a stack here before the promise tick
220 const errorAtDone = new ErrorWithStack(undefined, done);
221
222 if (!completed && testOrHook.seenDone) {
223 errorAtDone.message =
224 'Expected done to be called once, but it was called multiple times.';
225
226 if (reason) {
227 errorAtDone.message += ` Reason: ${prettyFormat(reason, {
228 maxDepth: 3,
229 })}`;
230 }
231 reject(errorAtDone);
232 throw errorAtDone;
233 } else {
234 testOrHook.seenDone = true;
235 }
236
237 // Use `Promise.resolve` to allow the event loop to go a single tick in case `done` is called synchronously
238 Promise.resolve().then(() => {
239 if (returnedValue !== undefined) {
240 asyncError.message = dedent`
241 Test functions cannot both take a 'done' callback and return something. Either use a 'done' callback, or return a promise.
242 Returned value: ${prettyFormat(returnedValue, {maxDepth: 3})}
243 `;
244 return reject(asyncError);
245 }
246
247 let errorAsErrorObject: Error;
248 if (checkIsError(reason)) {
249 errorAsErrorObject = reason;
250 } else {
251 errorAsErrorObject = errorAtDone;
252 errorAtDone.message = `Failed: ${prettyFormat(reason, {
253 maxDepth: 3,
254 })}`;
255 }
256
257 // Consider always throwing, regardless if `reason` is set or not
258 if (completed && reason) {
259 errorAsErrorObject.message = `Caught error after test environment was torn down\n\n${errorAsErrorObject.message}`;
260
261 throw errorAsErrorObject;
262 }
263
264 return reason ? reject(errorAsErrorObject) : resolve();
265 });
266 };
267
268 returnedValue = fn.call(testContext, done);
269

Callers 15

asyncError.test.tsFile · 0.85
asyncJestLifecycleFunction · 0.85
asyncJestTestFunction · 0.85
globals.test.tsFile · 0.85
promiseIt.test.jsFile · 0.85
index.test.jsFile · 0.85

Calls 4

checkIsErrorFunction · 0.85
thenMethod · 0.80
resolveFunction · 0.50
resolveMethod · 0.45

Tested by

no test coverage detected