Skip to content

Commit

Permalink
test: use runAllTimers when running fakeTimers
Browse files Browse the repository at this point in the history
  • Loading branch information
WillianAgostini committed Dec 24, 2023
1 parent 3707c1a commit 0ef9797
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
38 changes: 38 additions & 0 deletions e2e/__tests__/__snapshots__/testRetries.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,41 @@ exports[`Test Retries logs error(s) before retry 1`] = `
PASS __tests__/logErrorsBeforeRetries.test.js
✓ retryTimes set"
`;

exports[`Test Retries logs error(s) before retry 2`] = `
"LOGGING RETRY ERRORS retryTimes set with fake timers
RETRY 1
expect(received).toBeFalsy()
Received: true
18 | );
19 | } else {
> 20 | expect(true).toBeFalsy();
| ^
21 | jest.runAllTimers();
22 | }
23 | });
at Object.toBeFalsy (__tests__/logErrorsBeforeRetriesFakeTimers.test.js:20:18)
RETRY 2
expect(received).toBeFalsy()
Received: true
18 | );
19 | } else {
> 20 | expect(true).toBeFalsy();
| ^
21 | jest.runAllTimers();
22 | }
23 | });
at Object.toBeFalsy (__tests__/logErrorsBeforeRetriesFakeTimers.test.js:20:18)
PASS __tests__/logErrorsBeforeRetriesFakeTimers.test.js
✓ retryTimes set with fake timers"
`;
8 changes: 8 additions & 0 deletions e2e/__tests__/testRetries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ describe('Test Retries', () => {
expect(extractSummary(result.stderr).rest).toMatchSnapshot();
});

it('logs error(s) before retry', () => {

Check failure on line 45 in e2e/__tests__/testRetries.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Test title is used multiple times in the same describe block
const result = runJest('test-retries', ['logErrorsBeforeRetriesFakeTimers.test.js']);

Check failure on line 46 in e2e/__tests__/testRetries.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `'logErrorsBeforeRetriesFakeTimers.test.js'` with `⏎······'logErrorsBeforeRetriesFakeTimers.test.js',⏎····`
expect(result.exitCode).toBe(0);
expect(result.failed).toBe(false);
expect(result.stderr).toContain(logErrorsBeforeRetryErrorMessage);
expect(extractSummary(result.stderr).rest).toMatchSnapshot();
});

it('reporter shows more than 1 invocation if test is retried', () => {
let jsonResult;

Expand Down
15 changes: 0 additions & 15 deletions e2e/test-retries/__tests__/logErrorsBeforeRetries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,3 @@ it('retryTimes set', () => {
expect(true).toBeFalsy();
}
});

let iFakeTimers = 0;
const startTimeInSecondsFakeTimers = new Date().getTime();
jest.retryTimes(3, {logErrorsBeforeRetry: true, waitBeforeRetry: 100});
it('retryTimes set with fake timers', () => {
jest.useFakeTimers();
iFakeTimers++;
if (iFakeTimers === 3) {
expect(new Date().getTime() - startTimeInSecondsFakeTimers).toBeGreaterThan(
200,
);
} else {
expect(true).toBeFalsy();
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

let i = 0;
const startTimeInSeconds = new Date().getTime();
jest.retryTimes(3, {logErrorsBeforeRetry: true, waitBeforeRetry: 100});
it('retryTimes set with fake timers', () => {
jest.useFakeTimers();
i++;
if (i === 3) {
expect(new Date().getTime() - startTimeInSeconds).toBeGreaterThan(

Check failure on line 16 in e2e/test-retries/__tests__/logErrorsBeforeRetriesFakeTimers.test.js

View workflow job for this annotation

GitHub Actions / Lint

Replace `⏎······200,⏎····` with `200`
200,
);
} else {
expect(true).toBeFalsy();
jest.runAllTimers();
}
});

0 comments on commit 0ef9797

Please sign in to comment.