diff --git a/CHANGELOG.md b/CHANGELOG.md index b9aaf5641d85..d6702543432b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/e2e/__tests__/__snapshots__/consoleAfterTeardown.test.ts.snap b/e2e/__tests__/__snapshots__/consoleAfterTeardown.test.ts.snap index c75128492c2e..0057efd49acc 100644 --- a/e2e/__tests__/__snapshots__/consoleAfterTeardown.test.ts.snap +++ b/e2e/__tests__/__snapshots__/consoleAfterTeardown.test.ts.snap @@ -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) `; diff --git a/packages/jest-console/src/BufferedConsole.ts b/packages/jest-console/src/BufferedConsole.ts index 7ed54795e030..55c4e20ffc95 100644 --- a/packages/jest-console/src/BufferedConsole.ts +++ b/packages/jest-console/src/BufferedConsole.ts @@ -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( diff --git a/packages/jest-console/src/CustomConsole.ts b/packages/jest-console/src/CustomConsole.ts index de38ab55d4a0..0bbb8561db2d 100644 --- a/packages/jest-console/src/CustomConsole.ts +++ b/packages/jest-console/src/CustomConsole.ts @@ -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) { diff --git a/packages/jest-console/src/__tests__/CustomConsole.test.ts b/packages/jest-console/src/__tests__/CustomConsole.test.ts index 6751a0f63fb3..07bb5cf4f888 100644 --- a/packages/jest-console/src/__tests__/CustomConsole.test.ts +++ b/packages/jest-console/src/__tests__/CustomConsole.test.ts @@ -224,4 +224,10 @@ describe('CustomConsole', () => { _console.timeEnd('custom'); }); }); + + describe('console', () => { + test('should be able to initialize console instance', () => { + expect(_console.Console).toBeDefined(); + }); + }); }); diff --git a/packages/jest-console/src/__tests__/bufferedConsole.test.ts b/packages/jest-console/src/__tests__/bufferedConsole.test.ts index aa9fb36e34f1..8b0ee9f98439 100644 --- a/packages/jest-console/src/__tests__/bufferedConsole.test.ts +++ b/packages/jest-console/src/__tests__/bufferedConsole.test.ts @@ -184,4 +184,10 @@ describe('CustomConsole', () => { _console.timeEnd('custom'); }); }); + + describe('console', () => { + test('should be able to initialize console instance', () => { + expect(_console.Console).toBeDefined(); + }); + }); });