From b1c20ce7def0fbd4070f18b56c95046fb1a0fb6f Mon Sep 17 00:00:00 2001 From: Dmitri Gabbasov Date: Mon, 17 Jul 2023 01:39:05 +0300 Subject: [PATCH 1/2] fix: incorrectly setting the name of currently running concurrent test Test functions lose their async context when throttled by p-limit. This caused snapshot matchers not to work properly when the number of concurrent tests in a file was greater than the maxConcurrency config option. The fix is to set AsyncLocalStorage inside the throttled function. --- packages/jest-circus/src/run.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/jest-circus/src/run.ts b/packages/jest-circus/src/run.ts index ea6d5eb3076f..6564b92e8c45 100644 --- a/packages/jest-circus/src/run.ts +++ b/packages/jest-circus/src/run.ts @@ -140,9 +140,8 @@ function startTestsConcurrently(concurrentTests: Array) { jestExpect.setState({currentConcurrentTestName: testNameStorage}); for (const test of concurrentTests) { try { - const promise = testNameStorage.run(getTestID(test), () => - mutex(test.fn), - ); + const testFn = test.fn; + const promise = mutex(() => testNameStorage.run(getTestID(test), testFn)); // 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 From 7a174cdbb4a63496f737d4fa24d93a431cacbd51 Mon Sep 17 00:00:00 2001 From: Dmitri Gabbasov Date: Mon, 17 Jul 2023 01:47:43 +0300 Subject: [PATCH 2/2] add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e7e2690d1c7..461f20949ee2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Fixes +- `[jest-circus]` Fix snapshot matchers in concurrent tests when nr of tests exceeds `maxConcurrency` ([#14335](https://github.com/jestjs/jest/pull/14335)) - `[jest-snapshot]` Move `@types/prettier` from `dependencies` to `devDependencies` ([#14328](https://github.com/jestjs/jest/pull/14328)) ### Chore & Maintenance