Skip to content

Commit

Permalink
feat: Add Console to custom console object (#10502)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark1626 committed Sep 14, 2020
1 parent 1969fe0 commit db0b335
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -7,7 +7,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
Expand Up @@ -15,6 +15,6 @@ PASS __tests__/console.test.js
14 | });
15 |
at BufferedConsole.log (../../packages/jest-console/build/BufferedConsole.js:201:10)
at BufferedConsole.log (../../packages/jest-console/build/BufferedConsole.js:197:10)
at log (__tests__/console.test.js:12:13)
`;
17 changes: 7 additions & 10 deletions packages/jest-console/src/BufferedConsole.ts
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
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
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
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();
});
});
});

0 comments on commit db0b335

Please sign in to comment.