Skip to content

Commit

Permalink
chore: refactor callAsyncCircusFn parameters (#10629)
Browse files Browse the repository at this point in the history
  • Loading branch information
flozender committed Oct 14, 2020
1 parent d077f2e commit 19886b5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions packages/jest-circus/src/run.ts
Expand Up @@ -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,
});
Expand All @@ -166,7 +166,7 @@ const _callCircusTest = async (
}

try {
await callAsyncCircusFn(test.fn, testContext, test.asyncError, {
await callAsyncCircusFn(test, testContext, {
isHook: false,
timeout,
});
Expand Down
15 changes: 8 additions & 7 deletions packages/jest-circus/src/utils.ts
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<unknown> => {
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,
);

Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-types/src/Circus.ts
Expand Up @@ -66,7 +66,7 @@ export type SyncEvent =
asyncError: Error;
name: 'add_test';
testName: TestName;
fn?: TestFn;
fn: TestFn;
mode?: TestMode;
timeout: number | undefined;
}
Expand Down Expand Up @@ -231,7 +231,7 @@ export type TestEntry = {
type: 'test';
asyncError: Exception; // Used if the test failure contains no usable stack trace
errors: Array<TestError>;
fn?: TestFn;
fn: TestFn;
invocations: number;
mode: TestMode;
name: TestName;
Expand Down

0 comments on commit 19886b5

Please sign in to comment.