Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
call _runTest concurrently instead of just the test.fn
  • Loading branch information
madcapnmckay committed Jan 6, 2025
commit 2f38ecd957c5b8b97c7ea18b57e3c8091b4b2ecd
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ add_test: three
finish_describe_definition: describe
run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
hello one
hello two
hello three
run_describe_start: describe
test_start: one
test_started: one
test_fn_start: one
test_fn_failure: one
test_done: one
test_start: two
test_started: two
test_fn_start: two
test_fn_success: two
test_done: two
test_start: three
test_started: one
test_started: two
test_started: three
test_fn_start: one
test_fn_start: two
test_fn_start: three
hello one
hello two
hello three
test_fn_failure: one
test_fn_success: two
test_fn_success: three
test_done: one
test_done: two
test_done: three
run_describe_finish: describe
run_describe_finish: ROOT_DESCRIBE_BLOCK
Expand Down
23 changes: 17 additions & 6 deletions packages/jest-circus/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const {setTimeout} = globalThis;

type ConcurrentTestEntry = Omit<Circus.TestEntry, 'fn'> & {
fn: Circus.ConcurrentTestFn;
done: Promise<void>;
};

const run = async (): Promise<Circus.RunResult> => {
Expand Down Expand Up @@ -63,7 +64,7 @@ const _runTestsForDescribeBlock = async (
if (isRootBlock) {
const concurrentTests = collectConcurrentTests(describeBlock);
if (concurrentTests.length > 0) {
startTestsConcurrently(concurrentTests);
startTestsConcurrently(concurrentTests, isSkipped);
}
}

Expand Down Expand Up @@ -112,7 +113,11 @@ const _runTestsForDescribeBlock = async (
case 'test': {
const hasErrorsBeforeTestRun = child.errors.length > 0;
const hasRetryTimes = retryTimes > 0;
await _runTest(child, isSkipped);
if (child.concurrent) {
await (child as ConcurrentTestEntry).done;
} else {
await _runTest(child, isSkipped);
}

// If immediate retry is set, we retry the test immediately after the first run
if (
Expand Down Expand Up @@ -171,21 +176,27 @@ function collectConcurrentTests(
});
}

function startTestsConcurrently(concurrentTests: Array<ConcurrentTestEntry>) {
function startTestsConcurrently(
concurrentTests: Array<ConcurrentTestEntry>,
parentSkipped: boolean,
) {
const mutex = pLimit(getState().maxConcurrency);
const testNameStorage = new AsyncLocalStorage<string>();
jestExpect.setState({
currentConcurrentTestName: () => testNameStorage.getStore(),
});
for (const test of concurrentTests) {
try {
const testFn = test.fn;
const promise = mutex(() => testNameStorage.run(getTestID(test), testFn));
const promise = mutex(() =>
testNameStorage.run(getTestID(test), () =>
_runTest(test, parentSkipped),
),
);
// Avoid triggering the uncaught promise rejection handler in case the
// test fails before being awaited on.
// eslint-disable-next-line @typescript-eslint/no-empty-function
promise.catch(() => {});
test.fn = () => promise;
test.done = promise;
} catch (error) {
test.fn = () => {
throw error;
Expand Down