Skip to content

Commit

Permalink
Add console.timeLog function
Browse files Browse the repository at this point in the history
Provide timeLog to be compatible with node.

Signed-off-by: Chris. Webster <[email protected]>
  • Loading branch information
webstech committed Jun 29, 2020
1 parent a8129a7 commit d5716a2
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-console]` Add missing console.timeLog for compatability with Node ([#10209](https://github.com/facebook/jest/pull/10209))

### Chore & Maintenance

### Performance
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-console/src/BufferedConsole.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 d5716a2

Please sign in to comment.