Skip to content

Commit

Permalink
Add console.timeLog function (#10209)
Browse files Browse the repository at this point in the history
  • Loading branch information
webstech committed Jul 3, 2020
1 parent 6b234f3 commit 6005d7e
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Fixes

- `[expect]` Match symbols and bigints in `any()` ([#10223](https://github.com/facebook/jest/pull/10223))
- `[jest-console]` Add missing console.timeLog for compatability with Node ([#10209](https://github.com/facebook/jest/pull/10209))
- `[jest-snapshot]` Strip added indentation for inline error snapshots ([#10217](https://github.com/facebook/jest/pull/10217))

### Chore & Maintenance
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-console/src/BufferedConsole.ts
Expand Up @@ -159,6 +159,16 @@ export default class BufferedConsole extends Console {
}
}

timeLog(label = 'default', ...data: Array<unknown>): void {
const startTime = this._timers[label];

if (startTime) {
const endTime = new Date();
const time = endTime.getTime() - startTime.getTime();
this._log('time', format(`${label}: ${formatTime(time)}`, ...data));
}
}

warn(firstArg: unknown, ...rest: Array<unknown>): void {
this._log('warn', format(firstArg, ...rest));
}
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-console/src/CustomConsole.ts
Expand Up @@ -136,6 +136,16 @@ export default class CustomConsole extends Console {
}
}

timeLog(label = 'default', ...data: Array<unknown>): void {
const startTime = this._timers[label];

if (startTime) {
const endTime = new Date();
const time = endTime.getTime() - startTime.getTime();
this._log('time', format(`${label}: ${formatTime(time)}`, ...data));
}
}

warn(firstArg: unknown, ...args: Array<unknown>): void {
this._logError('warn', format(firstArg, ...args));
}
Expand Down
1 change: 1 addition & 0 deletions packages/jest-console/src/NullConsole.ts
Expand Up @@ -16,6 +16,7 @@ export default class NullConsole extends CustomConsole {
log(): void {}
time(): void {}
timeEnd(): void {}
timeLog(): void {}
trace(): void {}
warn(): void {}
group(): void {}
Expand Down
38 changes: 38 additions & 0 deletions packages/jest-console/src/__tests__/CustomConsole.test.ts
Expand Up @@ -186,4 +186,42 @@ describe('CustomConsole', () => {
expect(_stdout).toMatch('ms');
});
});

describe('timeLog', () => {
test('should return the time between time() and timeEnd() on default timer', () => {
_console.time();
_console.timeLog();

expect(_stdout).toMatch('default: ');
expect(_stdout).toMatch('ms');
_console.timeEnd();
});

test('should return the time between time() and timeEnd() on custom timer', () => {
_console.time('custom');
_console.timeLog('custom');

expect(_stdout).toMatch('custom: ');
expect(_stdout).toMatch('ms');
_console.timeEnd('custom');
});

test('default timer with data', () => {
_console.time();
_console.timeLog(undefined, 'foo', 5);

expect(_stdout).toMatch('default: ');
expect(_stdout).toMatch('ms foo 5');
_console.timeEnd();
});

test('custom timer with data', () => {
_console.time('custom');
_console.timeLog('custom', 'foo', 5);

expect(_stdout).toMatch('custom: ');
expect(_stdout).toMatch('ms foo 5');
_console.timeEnd('custom');
});
});
});
38 changes: 38 additions & 0 deletions packages/jest-console/src/__tests__/bufferedConsole.test.ts
Expand Up @@ -146,4 +146,42 @@ describe('CustomConsole', () => {
expect(stdout()).toMatch('ms');
});
});

describe('timeLog', () => {
test('should return the time between time() and timeEnd() on default timer', () => {
_console.time();
_console.timeLog();

expect(stdout()).toMatch('default: ');
expect(stdout()).toMatch('ms');
_console.timeEnd();
});

test('should return the time between time() and timeEnd() on custom timer', () => {
_console.time('custom');
_console.timeLog('custom');

expect(stdout()).toMatch('custom: ');
expect(stdout()).toMatch('ms');
_console.timeEnd('custom');
});

test('default timer with data', () => {
_console.time();
_console.timeLog(undefined, 'foo', 5);

expect(stdout()).toMatch('default: ');
expect(stdout()).toMatch('ms foo 5');
_console.timeEnd();
});

test('custom timer with data', () => {
_console.time('custom');
_console.timeLog('custom', 'foo', 5);

expect(stdout()).toMatch('custom: ');
expect(stdout()).toMatch('ms foo 5');
_console.timeEnd('custom');
});
});
});

0 comments on commit 6005d7e

Please sign in to comment.