Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add console.timeLog function #10209

Merged
merged 2 commits into from Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');
});
});
});