From 19886b5a93a351eb77b570721eba1eecaa86f888 Mon Sep 17 00:00:00 2001 From: Tayeeb Hasan <40818234+flozender@users.noreply.github.com> Date: Wed, 14 Oct 2020 21:27:09 +0530 Subject: [PATCH] chore: refactor `callAsyncCircusFn` parameters (#10629) --- CHANGELOG.md | 1 + packages/jest-circus/src/run.ts | 4 ++-- packages/jest-circus/src/utils.ts | 15 ++++++++------- packages/jest-types/src/Circus.ts | 4 ++-- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c8dad693ee8..a5874ce06b2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - `[docs]` Add step for fetching `backers.json` file in website setup docs ([#10631](https://github.com/facebook/jest/pull/10631)) - `[docs]` Add page detailing environment variables set by Jest ([#10630](https://github.com/facebook/jest/pull/10630)) +- `[jest-circus]` Refactor `callAsyncCircusFn` parameters ([#10629](https://github.com/facebook/jest/pull/10629)) ### Performance diff --git a/packages/jest-circus/src/run.ts b/packages/jest-circus/src/run.ts index f9a9969c7b3b..c3509943c4c4 100644 --- a/packages/jest-circus/src/run.ts +++ b/packages/jest-circus/src/run.ts @@ -143,7 +143,7 @@ const _callCircusHook = async ({ const timeout = hook.timeout || getState().testTimeout; try { - await callAsyncCircusFn(hook.fn, testContext, hook.asyncError, { + await callAsyncCircusFn(hook, testContext, { isHook: true, timeout, }); @@ -166,7 +166,7 @@ const _callCircusTest = async ( } try { - await callAsyncCircusFn(test.fn, testContext, test.asyncError, { + await callAsyncCircusFn(test, testContext, { isHook: false, timeout, }); diff --git a/packages/jest-circus/src/utils.ts b/packages/jest-circus/src/utils.ts index b1e9f8aeb95d..333faf037691 100644 --- a/packages/jest-circus/src/utils.ts +++ b/packages/jest-circus/src/utils.ts @@ -7,7 +7,7 @@ import * as path from 'path'; import type {Circus} from '@jest/types'; -import {convertDescriptorToString, formatTime} from 'jest-util'; +import {ErrorWithStack, convertDescriptorToString, formatTime} from 'jest-util'; import isGeneratorFn from 'is-generator-fn'; import co from 'co'; import dedent = require('dedent'); @@ -43,7 +43,7 @@ export const makeDescribe = ( }; export const makeTest = ( - fn: Circus.TestFn | undefined, + fn: Circus.TestFn, mode: Circus.TestMode, name: Circus.TestName, parent: Circus.DescribeBlock, @@ -159,17 +159,18 @@ function checkIsError(error: unknown): error is Error { } export const callAsyncCircusFn = ( - fn: Circus.AsyncFn, + testOrHook: Circus.TestEntry | Circus.Hook, testContext: Circus.TestContext | undefined, - asyncError: Circus.Exception, - {isHook, timeout}: {isHook?: boolean | null; timeout: number}, + {isHook, timeout}: {isHook: boolean; timeout: number}, ): Promise => { let timeoutID: NodeJS.Timeout; let completed = false; + const {fn, asyncError} = testOrHook; + return new Promise((resolve, reject) => { timeoutID = setTimeout( - () => reject(_makeTimeoutMessage(timeout, !!isHook)), + () => reject(_makeTimeoutMessage(timeout, isHook)), timeout, ); @@ -179,7 +180,7 @@ export const callAsyncCircusFn = ( let returnedValue: unknown = undefined; const done = (reason?: Error | string): void => { // We need to keep a stack here before the promise tick - const errorAtDone = new Error(); + const errorAtDone = new ErrorWithStack(undefined, done); // Use `Promise.resolve` to allow the event loop to go a single tick in case `done` is called synchronously Promise.resolve().then(() => { if (returnedValue !== undefined) { diff --git a/packages/jest-types/src/Circus.ts b/packages/jest-types/src/Circus.ts index b4c94d57c3b6..c5f941efc11c 100644 --- a/packages/jest-types/src/Circus.ts +++ b/packages/jest-types/src/Circus.ts @@ -66,7 +66,7 @@ export type SyncEvent = asyncError: Error; name: 'add_test'; testName: TestName; - fn?: TestFn; + fn: TestFn; mode?: TestMode; timeout: number | undefined; } @@ -231,7 +231,7 @@ export type TestEntry = { type: 'test'; asyncError: Exception; // Used if the test failure contains no usable stack trace errors: Array; - fn?: TestFn; + fn: TestFn; invocations: number; mode: TestMode; name: TestName;