Skip to content

Commit 0ef9797

Browse files
test: use runAllTimers when running fakeTimers
1 parent 3707c1a commit 0ef9797

File tree

4 files changed

+69
-15
lines changed

4 files changed

+69
-15
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,41 @@ exports[`Test Retries logs error(s) before retry 1`] = `
3737
PASS __tests__/logErrorsBeforeRetries.test.js
3838
✓ retryTimes set"
3939
`;
40+
41+
exports[`Test Retries logs error(s) before retry 2`] = `
42+
"LOGGING RETRY ERRORS retryTimes set with fake timers
43+
RETRY 1
44+
45+
expect(received).toBeFalsy()
46+
47+
Received: true
48+
49+
18 | );
50+
19 | } else {
51+
> 20 | expect(true).toBeFalsy();
52+
| ^
53+
21 | jest.runAllTimers();
54+
22 | }
55+
23 | });
56+
57+
at Object.toBeFalsy (__tests__/logErrorsBeforeRetriesFakeTimers.test.js:20:18)
58+
59+
RETRY 2
60+
61+
expect(received).toBeFalsy()
62+
63+
Received: true
64+
65+
18 | );
66+
19 | } else {
67+
> 20 | expect(true).toBeFalsy();
68+
| ^
69+
21 | jest.runAllTimers();
70+
22 | }
71+
23 | });
72+
73+
at Object.toBeFalsy (__tests__/logErrorsBeforeRetriesFakeTimers.test.js:20:18)
74+
75+
PASS __tests__/logErrorsBeforeRetriesFakeTimers.test.js
76+
✓ retryTimes set with fake timers"
77+
`;

e2e/__tests__/testRetries.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ describe('Test Retries', () => {
4242
expect(extractSummary(result.stderr).rest).toMatchSnapshot();
4343
});
4444

45+
it('logs error(s) before retry', () => {
46+
const result = runJest('test-retries', ['logErrorsBeforeRetriesFakeTimers.test.js']);
47+
expect(result.exitCode).toBe(0);
48+
expect(result.failed).toBe(false);
49+
expect(result.stderr).toContain(logErrorsBeforeRetryErrorMessage);
50+
expect(extractSummary(result.stderr).rest).toMatchSnapshot();
51+
});
52+
4553
it('reporter shows more than 1 invocation if test is retried', () => {
4654
let jsonResult;
4755

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,3 @@ it('retryTimes set', () => {
1717
expect(true).toBeFalsy();
1818
}
1919
});
20-
21-
let iFakeTimers = 0;
22-
const startTimeInSecondsFakeTimers = new Date().getTime();
23-
jest.retryTimes(3, {logErrorsBeforeRetry: true, waitBeforeRetry: 100});
24-
it('retryTimes set with fake timers', () => {
25-
jest.useFakeTimers();
26-
iFakeTimers++;
27-
if (iFakeTimers === 3) {
28-
expect(new Date().getTime() - startTimeInSecondsFakeTimers).toBeGreaterThan(
29-
200,
30-
);
31-
} else {
32-
expect(true).toBeFalsy();
33-
}
34-
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
let i = 0;
10+
const startTimeInSeconds = new Date().getTime();
11+
jest.retryTimes(3, {logErrorsBeforeRetry: true, waitBeforeRetry: 100});
12+
it('retryTimes set with fake timers', () => {
13+
jest.useFakeTimers();
14+
i++;
15+
if (i === 3) {
16+
expect(new Date().getTime() - startTimeInSeconds).toBeGreaterThan(
17+
200,
18+
);
19+
} else {
20+
expect(true).toBeFalsy();
21+
jest.runAllTimers();
22+
}
23+
});

0 commit comments

Comments
 (0)