Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor callAsyncCircusFn parameters #10629

Merged
merged 10 commits into from
Oct 14, 2020
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Features

- `[docs]` Add step for fetching `backers.json` file in website setup docs ([#10631](https://github.com/facebook/jest/pull/10631))
flozender marked this conversation as resolved.
Show resolved Hide resolved
- `[jest-circus]` Refactor `callAsyncCircusFn` parameters ([#10629](https://github.com/facebook/jest/pull/10629))
flozender marked this conversation as resolved.
Show resolved Hide resolved
- `[jest-cli, jest-config]` Add support for the `jest.config.ts` configuration file ([#10564](https://github.com/facebook/jest/pull/10564))

### Fixes
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-circus/src/run.ts
Original file line number Diff line number Diff line change
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(testContext, hook, {
isHook: true,
timeout,
});
Expand All @@ -166,7 +166,7 @@ const _callCircusTest = async (
}

try {
await callAsyncCircusFn(test.fn, testContext, test.asyncError, {
await callAsyncCircusFn(testContext, test, {
isHook: false,
timeout,
});
Expand Down
11 changes: 6 additions & 5 deletions packages/jest-circus/src/utils.ts
Original file line number Diff line number Diff line change
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,
SimenB marked this conversation as resolved.
Show resolved Hide resolved
fn: Circus.TestFn,
mode: Circus.TestMode,
name: Circus.TestName,
parent: Circus.DescribeBlock,
Expand Down Expand Up @@ -159,14 +159,15 @@ function checkIsError(error: unknown): error is Error {
}

export const callAsyncCircusFn = (
fn: Circus.AsyncFn,
testContext: Circus.TestContext | undefined,
asyncError: Circus.Exception,
testOrHook: Circus.TestEntry | Circus.Hook,
flozender marked this conversation as resolved.
Show resolved Hide resolved
{isHook, timeout}: {isHook?: boolean | null; timeout: number},
flozender marked this conversation as resolved.
Show resolved Hide resolved
): Promise<unknown> => {
let timeoutID: NodeJS.Timeout;
let completed = false;

const {fn, asyncError} = testOrHook;

return new Promise((resolve, reject) => {
timeoutID = setTimeout(
() => reject(_makeTimeoutMessage(timeout, !!isHook)),
flozender marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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