Skip to content

Commit f5507db

Browse files
committed
Fix time duration formatting as per SI
This commit fixes the formatting of time durations to have a spcae between the quantity and the unit symbol which is compliant with the SI. It also consolidates all of the time duration formatting code into formatTime() in jest-util to keep the code DRY and so that future updates are easy.
1 parent 7e5e422 commit f5507db

File tree

19 files changed

+111
-27
lines changed

19 files changed

+111
-27
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Fixes
66

7+
- `[jest-circus, jest-console, jest-jasmine2, jest-reporters, jest-util, pretty-format]` Fix time durating formatting and consolidate time formatting code ([#9765](https://github.com/facebook/jest/pull/9765))
8+
79
### Chore & Maintenance
810

911
- `[@jest/transform]` Expose type `CacheKeyOptions` for `getCacheKey` ([#9762](https://github.com/facebook/jest/pull/9762))

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ PASS __tests__/clear_cache.test.js
117117
Test Suites: 1 passed, 1 total
118118
Tests: 1 passed, 1 total
119119
Snapshots: 0 total
120-
Time: 0.232s, estimated 1s
120+
Time: 0.232 s, estimated 1 s
121121
Ran all test suites.
122122
```
123123

e2e/Utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export const copyDir = (src: string, dest: string) => {
140140

141141
export const replaceTime = (str: string) =>
142142
str
143-
.replace(/\d*\.?\d+m?s/g, '<<REPLACED>>')
143+
.replace(/\d*\.?\d+ m?s\b/g, '<<REPLACED>>')
144144
.replace(/, estimated <<REPLACED>>/g, '');
145145

146146
// Since Jest does not guarantee the order of tests we'll sort the output.
@@ -192,7 +192,7 @@ export const extractSummary = (stdout: string) => {
192192
const rest = stdout
193193
.replace(match[0], '')
194194
// remove all timestamps
195-
.replace(/\s*\(\d*\.?\d+m?s\)$/gm, '');
195+
.replace(/\s*\(\d*\.?\d+ m?s\b\)$/gm, '');
196196

197197
return {
198198
rest: rest.trim(),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ PASS __tests__/console.test.js
1515
14 | });
1616
15 |
1717
18-
at BufferedConsole.log (../../packages/jest-console/build/BufferedConsole.js:199:10)
18+
at BufferedConsole.log (../../packages/jest-console/build/BufferedConsole.js:209:10)
1919
at log (__tests__/console.test.js:12:13)
2020
`;

e2e/__tests__/overrideGlobals.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test('overriding native promise does not freeze Jest', () => {
1313
});
1414

1515
test('has a duration even if time is faked', () => {
16-
const regex = /works well \((\d+)ms\)/;
16+
const regex = /works well \((\d+) ms\)/;
1717
const {stderr} = runJest('override-globals', ['--verbose']);
1818

1919
expect(stderr).toMatch(regex);

packages/jest-circus/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import type {Circus} from '@jest/types';
9-
import {convertDescriptorToString} from 'jest-util';
9+
import {convertDescriptorToString, formatTime} from 'jest-util';
1010
import isGeneratorFn from 'is-generator-fn';
1111
import co from 'co';
1212
import StackUtils = require('stack-utils');
@@ -138,7 +138,7 @@ export const describeBlockHasTests = (
138138
describe.tests.length > 0 || describe.children.some(describeBlockHasTests);
139139

140140
const _makeTimeoutMessage = (timeout: number, isHook: boolean) =>
141-
`Exceeded timeout of ${timeout}ms for a ${
141+
`Exceeded timeout of ${formatTime(timeout)} for a ${
142142
isHook ? 'hook' : 'test'
143143
}.\nUse jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test.`;
144144

packages/jest-console/src/BufferedConsole.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import assert = require('assert');
99
import {Console} from 'console';
1010
import {format} from 'util';
11+
import {formatTime} from 'jest-util';
1112
import chalk = require('chalk');
1213
import {SourceMapRegistry, getCallsite} from '@jest/source-map';
1314
import type {
@@ -150,7 +151,7 @@ export default class BufferedConsole extends Console {
150151
if (startTime) {
151152
const endTime = new Date();
152153
const time = endTime.getTime() - startTime.getTime();
153-
this._log('time', format(`${label}: ${time}ms`));
154+
this._log('time', format(`${label}: ${formatTime(time)}`));
154155
delete this._timers[label];
155156
}
156157
}

packages/jest-console/src/CustomConsole.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import assert = require('assert');
99
import {format} from 'util';
1010
import {Console} from 'console';
1111
import chalk = require('chalk');
12-
import {clearLine} from 'jest-util';
12+
import {clearLine, formatTime} from 'jest-util';
1313
import type {LogCounters, LogMessage, LogTimers, LogType} from './types';
1414

1515
type Formatter = (type: LogType, message: LogMessage) => string;
@@ -132,7 +132,7 @@ export default class CustomConsole extends Console {
132132
if (startTime) {
133133
const endTime = new Date().getTime();
134134
const time = endTime - startTime.getTime();
135-
this._log('time', format(`${label}: ${time}ms`));
135+
this._log('time', format(`${label}: ${formatTime(time)}`));
136136
delete this._timers[label];
137137
}
138138
}

packages/jest-jasmine2/src/__tests__/queueRunner.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('queueRunner', () => {
113113
expect(onException).toHaveBeenCalled();
114114
// i.e. the `message` of the error passed to `onException`.
115115
expect(onException.mock.calls[0][0].message).toEqual(
116-
'Timeout - Async callback was not invoked within the 0ms timeout ' +
116+
'Timeout - Async callback was not invoked within the 0 ms timeout ' +
117117
'specified by jest.setTimeout.',
118118
);
119119
expect(fnTwo).toHaveBeenCalled();

packages/jest-jasmine2/src/queueRunner.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
// @ts-ignore ignore vendor file
9+
import {formatTime} from 'jest-util';
910
import PCancelable from './PCancelable';
1011
import pTimeout from './pTimeout';
1112

@@ -71,8 +72,8 @@ export default function queueRunner(options: Options) {
7172
() => {
7273
initError.message =
7374
'Timeout - Async callback was not invoked within the ' +
74-
timeoutMs +
75-
'ms timeout specified by jest.setTimeout.';
75+
formatTime(timeoutMs) +
76+
' timeout specified by jest.setTimeout.';
7677
initError.stack = initError.message + initError.stack;
7778
options.onException(initError);
7879
},

0 commit comments

Comments
 (0)