Skip to content

Commit 0b42f0e

Browse files
authored
[jest-circus] Isolate done callback state between invocations (#16343)
1 parent 5a7a6c4 commit 0b42f0e

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- `[expect, jest-message-util, jest-pattern, jest-regex-util, jest-util]` Revert `node:` protocol imports to restore webpack/browser-bundle compatibility ([#16167](https://github.com/jestjs/jest/pull/16167))
1818
- `[expect]` Widen `toMatchObject` and `objectContaining` parameter type from `Record<string, unknown>` to `object` so class instances are accepted ([#16196](https://github.com/jestjs/jest/pull/16196))
1919
- `[jest-circus]` Clear `currentlyRunningTest` after skipped and todo tests ([#16342](https://github.com/jestjs/jest/pull/16342))
20+
- `[jest-circus]` Prevent late `done()` callbacks from affecting later test or hook invocations ([#16343](https://github.com/jestjs/jest/pull/16343))
2021
- `[jest-circus, jest-jasmine2, jest-message-util]` Serialize the inner errors of an `AggregateError` into `failureMessages`, `retryReasons` and `unhandledErrors`, so `--json` output and reporter annotations include them ([#16316](https://github.com/jestjs/jest/pull/16316))
2122
- `[@jest/create-cache-key-function]` Include the caller support flags in the generated key, so a transformer that emits ESM or CJS based on them no longer shares one cache entry between the two ([#16331](https://github.com/jestjs/jest/pull/16331))
2223
- `[@jest/create-cache-key-function]` Include the stringified project config in the generated key, so editing a transformer's own settings invalidates what it cached ([#16331](https://github.com/jestjs/jest/pull/16331))

‎packages/jest-circus/src/__tests__/utils.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import type {Circus} from '@jest/types';
89
import {runTest} from '../__mocks__/testUtils';
910
import {ROOT_DESCRIBE_BLOCK_NAME} from '../state';
1011
import {
12+
callAsyncCircusFn,
1113
makeDescribe,
1214
makeRunResult,
1315
makeSingleTestResult,
@@ -139,3 +141,37 @@ test('makeRunResult keeps the unserialized unhandled errors', () => {
139141
expect(result.unhandledErrorsDetailed[0]).toBe(error);
140142
expect(result.unhandledErrors[0]).toBe(error.stack);
141143
});
144+
145+
test('a late done callback does not affect a later invocation', async () => {
146+
const rootDescribe = makeDescribe(ROOT_DESCRIBE_BLOCK_NAME);
147+
let firstDone: Circus.DoneFn = () => {};
148+
const circusTest = makeTest(
149+
done => {
150+
firstDone = done;
151+
},
152+
undefined,
153+
false,
154+
'done callback test',
155+
rootDescribe,
156+
undefined,
157+
new Error('async error'),
158+
false,
159+
);
160+
const options = {isHook: false, timeout: 1000};
161+
162+
const firstInvocation = callAsyncCircusFn(circusTest, {}, options);
163+
firstDone(new Error('first failure'));
164+
await expect(firstInvocation).rejects.toThrow('first failure');
165+
166+
circusTest.seenDone = false;
167+
let secondDone: Circus.DoneFn = () => {};
168+
circusTest.fn = done => {
169+
secondDone = done;
170+
};
171+
172+
const secondInvocation = callAsyncCircusFn(circusTest, {}, options);
173+
firstDone();
174+
secondDone();
175+
176+
await expect(secondInvocation).resolves.toBeUndefined();
177+
});

‎packages/jest-circus/src/utils.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export const callAsyncCircusFn = (
230230
}
231231
reject(errorAtDone);
232232
throw errorAtDone;
233-
} else {
233+
} else if (!completed) {
234234
testOrHook.seenDone = true;
235235
}
236236

0 commit comments

Comments
 (0)