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

feat: Add Console to custom console object #10502

Merged
merged 6 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

### Fixes

- Fix lifecycle hook function types ([#10480](https://github.com/facebook/jest/pull/10480))
- `[jest-console]` Add `Console` constructor to `console` object ([#10502](https://github.com/facebook/jest/pull/10502))
- `[jest-globals]` Fix lifecycle hook function types ([#10480](https://github.com/facebook/jest/pull/10480))

### Chore & Maintenance

Expand Down
17 changes: 7 additions & 10 deletions packages/jest-console/src/BufferedConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,21 @@ import type {
} from './types';

export default class BufferedConsole extends Console {
private _buffer: ConsoleBuffer;
private _counters: LogCounters;
private _timers: LogTimers;
private _groupDepth: number;
private _buffer: ConsoleBuffer = [];
private _counters: LogCounters = {};
private _timers: LogTimers = {};
private _groupDepth = 0;

Console: NodeJS.ConsoleConstructor = Console;

constructor() {
const buffer: ConsoleBuffer = [];
super({
write: (message: string) => {
BufferedConsole.write(buffer, 'log', message, null);
BufferedConsole.write(this._buffer, 'log', message, null);

return true;
},
} as NodeJS.WritableStream);
this._buffer = buffer;
this._counters = {};
this._timers = {};
this._groupDepth = 0;
}

static write(
Expand Down
14 changes: 6 additions & 8 deletions packages/jest-console/src/CustomConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,21 @@ export default class CustomConsole extends Console {
private _stdout: NodeJS.WriteStream;
private _stderr: NodeJS.WriteStream;
private _formatBuffer: Formatter;
private _counters: LogCounters;
private _timers: LogTimers;
private _groupDepth: number;
private _counters: LogCounters = {};
private _timers: LogTimers = {};
private _groupDepth = 0;

Console: NodeJS.ConsoleConstructor = Console;

constructor(
stdout: NodeJS.WriteStream,
stderr: NodeJS.WriteStream,
formatBuffer: Formatter = (_type: LogType, message: string): string =>
message,
formatBuffer: Formatter = (_type, message) => message,
) {
super(stdout, stderr);
this._stdout = stdout;
this._stderr = stderr;
this._formatBuffer = formatBuffer;
this._counters = {};
this._timers = {};
this._groupDepth = 0;
}

private _log(type: LogType, message: string) {
Expand Down
6 changes: 6 additions & 0 deletions packages/jest-console/src/__tests__/CustomConsole.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,10 @@ describe('CustomConsole', () => {
_console.timeEnd('custom');
});
});

describe('console', () => {
test('should be able to initialize console instance', () => {
expect(_console.Console).toBeDefined();
});
});
});
6 changes: 6 additions & 0 deletions packages/jest-console/src/__tests__/bufferedConsole.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,10 @@ describe('CustomConsole', () => {
_console.timeEnd('custom');
});
});

describe('console', () => {
test('should be able to initialize console instance', () => {
expect(_console.Console).toBeDefined();
});
});
});