From ddb620327481a5b2983b4bce256acd6053eb6ebf Mon Sep 17 00:00:00 2001 From: "Chris. Webster" Date: Fri, 26 Jun 2020 21:59:07 -0700 Subject: [PATCH] Add console.timeLog function Provide timeLog to be compatible with node. Signed-off-by: Chris. Webster --- CHANGELOG.md | 1 + packages/jest-console/src/BufferedConsole.ts | 10 +++++ packages/jest-console/src/CustomConsole.ts | 10 +++++ packages/jest-console/src/NullConsole.ts | 1 + .../src/__tests__/CustomConsole.test.ts | 38 +++++++++++++++++++ .../src/__tests__/bufferedConsole.test.ts | 38 +++++++++++++++++++ 6 files changed, 98 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0528254517c8..3cb5795a7334 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features ### Fixes +- `[jest-console]` Add missing console.timeLog for compatability with Node ([#10209](https://github.com/facebook/jest/pull/10209)) ### Chore & Maintenance diff --git a/packages/jest-console/src/BufferedConsole.ts b/packages/jest-console/src/BufferedConsole.ts index 6cf73e4d01a9..7ed54795e030 100644 --- a/packages/jest-console/src/BufferedConsole.ts +++ b/packages/jest-console/src/BufferedConsole.ts @@ -159,6 +159,16 @@ export default class BufferedConsole extends Console { } } + timeLog(label = 'default', ...data: Array): 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): void { this._log('warn', format(firstArg, ...rest)); } diff --git a/packages/jest-console/src/CustomConsole.ts b/packages/jest-console/src/CustomConsole.ts index eecaa706720a..de38ab55d4a0 100644 --- a/packages/jest-console/src/CustomConsole.ts +++ b/packages/jest-console/src/CustomConsole.ts @@ -136,6 +136,16 @@ export default class CustomConsole extends Console { } } + timeLog(label = 'default', ...data: Array): 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): void { this._logError('warn', format(firstArg, ...args)); } diff --git a/packages/jest-console/src/NullConsole.ts b/packages/jest-console/src/NullConsole.ts index 1f3530d977a4..002e3a9738ff 100644 --- a/packages/jest-console/src/NullConsole.ts +++ b/packages/jest-console/src/NullConsole.ts @@ -16,6 +16,7 @@ export default class NullConsole extends CustomConsole { log(): void {} time(): void {} timeEnd(): void {} + timeLog(): void {} trace(): void {} warn(): void {} group(): void {} diff --git a/packages/jest-console/src/__tests__/CustomConsole.test.ts b/packages/jest-console/src/__tests__/CustomConsole.test.ts index 13e578db0680..6751a0f63fb3 100644 --- a/packages/jest-console/src/__tests__/CustomConsole.test.ts +++ b/packages/jest-console/src/__tests__/CustomConsole.test.ts @@ -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'); + }); + }); }); diff --git a/packages/jest-console/src/__tests__/bufferedConsole.test.ts b/packages/jest-console/src/__tests__/bufferedConsole.test.ts index 23411b6a9523..aa9fb36e34f1 100644 --- a/packages/jest-console/src/__tests__/bufferedConsole.test.ts +++ b/packages/jest-console/src/__tests__/bufferedConsole.test.ts @@ -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'); + }); + }); });