Skip to content

Commit 8c6a5ff

Browse files
feat: add a waitBeforeRetry option to jest.retryTimes (#14737)
1 parent 8725326 commit 8c6a5ff

File tree

7 files changed

+43
-18
lines changed

7 files changed

+43
-18
lines changed

docs/JestObjectAPI.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,16 @@ test('will fail', () => {
10711071
});
10721072
```
10731073

1074+
`waitBeforeRetry` is the number of milliseconds to wait before retrying.
1075+
1076+
```js
1077+
jest.retryTimes(3, {waitBeforeRetry: 1000});
1078+
1079+
test('will fail', () => {
1080+
expect(true).toBe(false);
1081+
});
1082+
```
1083+
10741084
Returns the `jest` object for chaining.
10751085

10761086
:::caution

e2e/__tests__/__snapshots__/testRetries.test.ts.snap

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@ exports[`Test Retries logs error(s) before retry 1`] = `
88
99
Received: true
1010
11-
14 | expect(true).toBeTruthy();
12-
15 | } else {
13-
> 16 | expect(true).toBeFalsy();
11+
15 | expect(new Date().getTime() - startTimeInSeconds).toBeGreaterThan(200);
12+
16 | } else {
13+
> 17 | expect(true).toBeFalsy();
1414
| ^
15-
17 | }
16-
18 | });
17-
19 |
15+
18 | }
16+
19 | });
17+
20 |
1818
19-
at Object.toBeFalsy (__tests__/logErrorsBeforeRetries.test.js:16:18)
19+
at Object.toBeFalsy (__tests__/logErrorsBeforeRetries.test.js:17:18)
2020
2121
RETRY 2
2222
2323
expect(received).toBeFalsy()
2424
2525
Received: true
2626
27-
14 | expect(true).toBeTruthy();
28-
15 | } else {
29-
> 16 | expect(true).toBeFalsy();
27+
15 | expect(new Date().getTime() - startTimeInSeconds).toBeGreaterThan(200);
28+
16 | } else {
29+
> 17 | expect(true).toBeFalsy();
3030
| ^
31-
17 | }
32-
18 | });
33-
19 |
31+
18 | }
32+
19 | });
33+
20 |
3434
35-
at Object.toBeFalsy (__tests__/logErrorsBeforeRetries.test.js:16:18)
35+
at Object.toBeFalsy (__tests__/logErrorsBeforeRetries.test.js:17:18)
3636
3737
PASS __tests__/logErrorsBeforeRetries.test.js
3838
✓ retryTimes set"

e2e/test-retries/__tests__/logErrorsBeforeRetries.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
'use strict';
88

99
let i = 0;
10-
jest.retryTimes(3, {logErrorsBeforeRetry: true});
10+
const startTimeInSeconds = new Date().getTime();
11+
jest.retryTimes(3, {logErrorsBeforeRetry: true, waitBeforeRetry: 100});
1112
it('retryTimes set', () => {
1213
i++;
1314
if (i === 3) {
14-
expect(true).toBeTruthy();
15+
expect(new Date().getTime() - startTimeInSeconds).toBeGreaterThan(200);
1516
} else {
1617
expect(true).toBeFalsy();
1718
}

packages/jest-circus/src/run.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import shuffleArray, {
1515
rngBuilder,
1616
} from './shuffleArray';
1717
import {dispatch, getState} from './state';
18-
import {RETRY_TIMES} from './types';
18+
import {RETRY_TIMES, WAIT_BEFORE_RETRY} from './types';
1919
import {
2020
callAsyncCircusFn,
2121
getAllHooksForDescribe,
@@ -67,6 +67,10 @@ const _runTestsForDescribeBlock = async (
6767
const retryTimes =
6868
// eslint-disable-next-line no-restricted-globals
6969
parseInt((global as Global.Global)[RETRY_TIMES] as string, 10) || 0;
70+
71+
const waitBeforeRetry =
72+
// eslint-disable-next-line no-restricted-globals
73+
parseInt((global as Global.Global)[WAIT_BEFORE_RETRY] as string, 10) || 0;
7074
const deferredRetryTests = [];
7175

7276
if (rng) {
@@ -102,6 +106,10 @@ const _runTestsForDescribeBlock = async (
102106
// Clear errors so retries occur
103107
await dispatch({name: 'test_retry', test});
104108

109+
if (waitBeforeRetry > 0) {
110+
await new Promise(resolve => setTimeout(resolve, waitBeforeRetry));
111+
}
112+
105113
await _runTest(test, isSkipped);
106114
numRetriesAvailable--;
107115
}

packages/jest-circus/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
export const STATE_SYM = Symbol('JEST_STATE_SYMBOL');
99
export const RETRY_TIMES = Symbol.for('RETRY_TIMES');
10+
export const WAIT_BEFORE_RETRY = Symbol.for('WAIT_BEFORE_RETRY');
1011
// To pass this value from Runtime object to state we need to use global[sym]
1112
export const TEST_TIMEOUT_SYMBOL = Symbol.for('TEST_TIMEOUT_SYMBOL');
1213
export const LOG_ERRORS_BEFORE_RETRY = Symbol.for('LOG_ERRORS_BEFORE_RETRY');

packages/jest-environment/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,14 @@ export interface Jest {
298298
* the test to fail to the console, providing visibility on why a retry occurred.
299299
* retries is exhausted.
300300
*
301+
* `waitBeforeRetry` is the number of milliseconds to wait before retrying
302+
*
301303
* @remarks
302304
* Only available with `jest-circus` runner.
303305
*/
304306
retryTimes(
305307
numRetries: number,
306-
options?: {logErrorsBeforeRetry?: boolean},
308+
options?: {logErrorsBeforeRetry?: boolean; waitBeforeRetry?: number},
307309
): Jest;
308310
/**
309311
* Exhausts tasks queued by `setImmediate()`.

packages/jest-runtime/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ type ResolveOptions = Parameters<typeof require.resolve>[1] & {
122122

123123
const testTimeoutSymbol = Symbol.for('TEST_TIMEOUT_SYMBOL');
124124
const retryTimesSymbol = Symbol.for('RETRY_TIMES');
125+
const waitBeforeRetrySymbol = Symbol.for('WAIT_BEFORE_RETRY');
125126
const logErrorsBeforeRetrySymbol = Symbol.for('LOG_ERRORS_BEFORE_RETRY');
126127

127128
const NODE_MODULES = `${path.sep}node_modules${path.sep}`;
@@ -2265,6 +2266,8 @@ export default class Runtime {
22652266
this._environment.global[retryTimesSymbol] = numTestRetries;
22662267
this._environment.global[logErrorsBeforeRetrySymbol] =
22672268
options?.logErrorsBeforeRetry;
2269+
this._environment.global[waitBeforeRetrySymbol] =
2270+
options?.waitBeforeRetry;
22682271

22692272
return jestObject;
22702273
};

0 commit comments

Comments
 (0)