Skip to content

Commit

Permalink
chore: prefer Date.now (#14811)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Dec 29, 2023
1 parent da0701b commit fb647d5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
15 changes: 13 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,19 @@ module.exports = {
'unicorn/prefer-top-level-await': 'error',
},
},
{
files: [
'e2e/coverage-report/__mocks__/sumDependency.js',
'e2e/require-main-after-create-require/empty.js',
'packages/create-jest/src/__tests__/__fixtures__/**/*',
'packages/jest-core/src/__tests__/**/*',
'packages/jest-haste-map/src/__tests__/test_dotfiles_root/**/*',
'packages/jest-resolve/src/__mocks__/**/*',
],
rules: {
'unicorn/no-empty-file': 'off',
},
},
],
parser: '@typescript-eslint/parser',
parserOptions: {
Expand Down Expand Up @@ -690,13 +703,11 @@ module.exports = {
'unicorn/error-message': 'off',
'unicorn/no-await-expression-member': 'off',
'unicorn/no-console-spaces': 'off',
'unicorn/no-empty-file': 'off',
'unicorn/no-object-as-default-parameter': 'off',
'unicorn/no-thenable': 'off',
'unicorn/no-typeof-undefined': 'off',
'unicorn/no-useless-promise-resolve-reject': 'off',
'unicorn/no-useless-undefined': 'off',
'unicorn/prefer-date-now': 'off',
'unicorn/prefer-logical-operator-over-ternary': 'off',
'unicorn/prefer-math-trunc': 'off',
'unicorn/prefer-native-coercion-functions': 'off',
Expand Down
8 changes: 4 additions & 4 deletions e2e/__tests__/__snapshots__/testRetries.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ exports[`Test Retries wait before retry 1`] = `
Received: true
15 | expect(new Date().getTime() - startTimeInSeconds).toBeGreaterThan(200);
15 | expect(Date.now() - startTimeInSeconds).toBeGreaterThan(200);
16 | } else {
> 17 | expect(true).toBeFalsy();
| ^
Expand All @@ -62,7 +62,7 @@ exports[`Test Retries wait before retry 1`] = `
Received: true
15 | expect(new Date().getTime() - startTimeInSeconds).toBeGreaterThan(200);
15 | expect(Date.now() - startTimeInSeconds).toBeGreaterThan(200);
16 | } else {
> 17 | expect(true).toBeFalsy();
| ^
Expand All @@ -84,7 +84,7 @@ exports[`Test Retries wait before retry with fake timers 1`] = `
Received: true
16 | expect(new Date().getTime() - startTimeInSeconds).toBeGreaterThan(200);
16 | expect(Date.now() - startTimeInSeconds).toBeGreaterThan(200);
17 | } else {
> 18 | expect(true).toBeFalsy();
| ^
Expand All @@ -100,7 +100,7 @@ exports[`Test Retries wait before retry with fake timers 1`] = `
Received: true
16 | expect(new Date().getTime() - startTimeInSeconds).toBeGreaterThan(200);
16 | expect(Date.now() - startTimeInSeconds).toBeGreaterThan(200);
17 | } else {
> 18 | expect(true).toBeFalsy();
| ^
Expand Down
4 changes: 2 additions & 2 deletions e2e/test-retries/__tests__/waitBeforeRetry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
'use strict';

let i = 0;
const startTimeInSeconds = new Date().getTime();
const startTimeInSeconds = Date.now();
jest.retryTimes(3, {logErrorsBeforeRetry: true, waitBeforeRetry: 100});
it('retryTimes set', () => {
i++;
if (i === 3) {
expect(new Date().getTime() - startTimeInSeconds).toBeGreaterThan(200);
expect(Date.now() - startTimeInSeconds).toBeGreaterThan(200);
} else {
expect(true).toBeFalsy();
}
Expand Down
4 changes: 2 additions & 2 deletions e2e/test-retries/__tests__/waitBeforeRetryFakeTimers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
'use strict';

let i = 0;
const startTimeInSeconds = new Date().getTime();
const startTimeInSeconds = Date.now();
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(200);
expect(Date.now() - startTimeInSeconds).toBeGreaterThan(200);
} else {
expect(true).toBeFalsy();
jest.runAllTimers();
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-console/src/CustomConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export default class CustomConsole extends Console {
const startTime = this._timers[label];

if (startTime != null) {
const endTime = new Date().getTime();
const endTime = Date.now();
const time = endTime - startTime.getTime();
this._log('time', format(`${label}: ${formatTime(time)}`));
delete this._timers[label];
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/jasmine/Timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

const defaultNow = (function (Date) {
return function () {
return new Date().getTime();
return Date.now();
};
})(Date);

Expand Down

0 comments on commit fb647d5

Please sign in to comment.