Console utilities for Jest testing framework that provide custom console implementations including BufferedConsole for capturing output, CustomConsole for formatted logging, and NullConsole for suppressing output during tests
npx @tessl/cli install tessl/npm-jest--console@30.1.00
# Jest Console
1
2
Jest Console provides essential console utilities for the Jest testing framework, enabling developers to control and capture console output during test execution. It offers three main console implementations: BufferedConsole for collecting console output in memory, CustomConsole for providing formatted console output with custom styling, and NullConsole for completely suppressing console output during tests.
3
4
## Package Information
5
6
- **Package Name**: @jest/console
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install @jest/console`
10
11
## Core Imports
12
13
```typescript
14
import { BufferedConsole, CustomConsole, NullConsole, getConsoleOutput } from "@jest/console";
15
import type { ConsoleBuffer, LogMessage, LogType, LogEntry } from "@jest/console";
16
```
17
18
For CommonJS:
19
20
```javascript
21
const { BufferedConsole, CustomConsole, NullConsole, getConsoleOutput } = require("@jest/console");
22
```
23
24
## Basic Usage
25
26
```typescript
27
import { BufferedConsole, CustomConsole, NullConsole } from "@jest/console";
28
29
// BufferedConsole - capture output in memory
30
const bufferedConsole = new BufferedConsole();
31
bufferedConsole.log("Hello, World!");
32
bufferedConsole.error("Something went wrong");
33
const buffer = bufferedConsole.getBuffer();
34
// buffer contains array of log entries with messages, types, and stack traces
35
36
// CustomConsole - formatted real-time output
37
import { WriteStream } from "tty";
38
const customConsole = new CustomConsole(process.stdout, process.stderr);
39
customConsole.log("Formatted output");
40
41
// NullConsole - suppress all output
42
const nullConsole = new NullConsole();
43
nullConsole.log("This will not appear anywhere");
44
```
45
46
## Architecture
47
48
Jest Console is built around several key components:
49
50
- **BufferedConsole**: Extends Node.js Console to capture all output in memory as structured log entries
51
- **CustomConsole**: Extends Node.js Console to provide formatted output to custom streams with optional message formatting
52
- **NullConsole**: Extends CustomConsole to suppress all console output completely
53
- **Output Formatting**: Utilities for converting captured console logs into formatted, styled output strings
54
55
## Capabilities
56
57
### Buffered Console Implementation
58
59
Console implementation that captures all output in memory for later retrieval and analysis, ideal for testing scenarios where you need to verify console output.
60
61
```typescript { .api }
62
class BufferedConsole extends Console {
63
constructor();
64
getBuffer(): ConsoleBuffer | undefined;
65
static write(
66
buffer: ConsoleBuffer,
67
type: LogType,
68
message: LogMessage,
69
stackLevel?: number
70
): ConsoleBuffer;
71
}
72
```
73
74
[BufferedConsole](./buffered-console.md)
75
76
### Custom Console Implementation
77
78
Console implementation that outputs to custom streams with optional formatting, perfect for providing styled console output in custom environments.
79
80
```typescript { .api }
81
type Formatter = (type: LogType, message: LogMessage) => string;
82
83
class CustomConsole extends Console {
84
constructor(
85
stdout: WriteStream,
86
stderr: WriteStream,
87
formatBuffer?: Formatter
88
);
89
getBuffer(): undefined;
90
}
91
```
92
93
[CustomConsole](./custom-console.md)
94
95
### Null Console Implementation
96
97
Console implementation that suppresses all output, useful for completely silencing console output during test execution.
98
99
```typescript { .api }
100
class NullConsole extends CustomConsole {
101
// All console methods are no-ops
102
}
103
```
104
105
[NullConsole](./null-console.md)
106
107
### Console Output Formatting
108
109
Utility function for formatting console buffer entries into readable string output with proper styling and stack traces.
110
111
```typescript { .api }
112
function getConsoleOutput(
113
buffer: ConsoleBuffer,
114
config: StackTraceConfig,
115
globalConfig: Config.GlobalConfig
116
): string;
117
```
118
119
[Output Formatting](./output-formatting.md)
120
121
## Types
122
123
```typescript { .api }
124
type LogMessage = string;
125
126
type LogType =
127
| 'assert'
128
| 'count'
129
| 'debug'
130
| 'dir'
131
| 'dirxml'
132
| 'error'
133
| 'group'
134
| 'groupCollapsed'
135
| 'info'
136
| 'log'
137
| 'time'
138
| 'warn';
139
140
interface LogEntry {
141
message: LogMessage;
142
origin: string;
143
type: LogType;
144
}
145
146
type ConsoleBuffer = Array<LogEntry>;
147
148
// From Node.js util module
149
interface InspectOptions {
150
showHidden?: boolean;
151
depth?: number | null;
152
colors?: boolean;
153
customInspect?: boolean;
154
showProxy?: boolean;
155
maxArrayLength?: number | null;
156
maxStringLength?: number | null;
157
breakLength?: number;
158
compact?: boolean | number;
159
sorted?: boolean | ((a: string, b: string) => number);
160
getters?: boolean | 'get' | 'set';
161
}
162
163
// From Node.js tty module
164
interface WriteStream extends NodeJS.WriteStream {
165
isTTY: boolean;
166
columns: number;
167
rows: number;
168
}
169
```