0
# BufferedConsole
1
2
BufferedConsole is a console implementation that captures all output in memory as structured log entries, making it ideal for testing scenarios where you need to verify console output programmatically.
3
4
## Capabilities
5
6
### BufferedConsole Class
7
8
Console implementation that extends Node.js Console to buffer all output in memory for later retrieval.
9
10
```typescript { .api }
11
/**
12
* Console implementation that buffers all output in memory
13
* Extends Node.js Console with memory buffering capabilities
14
*/
15
class BufferedConsole extends Console {
16
/** Creates a new BufferedConsole instance with no configuration required */
17
constructor();
18
19
/** Reference to the Node.js Console constructor */
20
Console: typeof Console;
21
22
/** Returns the current buffer contents or undefined if empty */
23
getBuffer(): ConsoleBuffer | undefined;
24
25
/** Static method to write entries to a console buffer */
26
static write(
27
buffer: ConsoleBuffer,
28
type: LogType,
29
message: LogMessage,
30
stackLevel?: number
31
): ConsoleBuffer;
32
}
33
```
34
35
**Usage Examples:**
36
37
```typescript
38
import { BufferedConsole } from "@jest/console";
39
40
// Create a buffered console
41
const console = new BufferedConsole();
42
43
// Use like a normal console
44
console.log("Hello, World!");
45
console.error("Something went wrong");
46
console.warn("Warning message");
47
48
// Retrieve the buffer
49
const buffer = console.getBuffer();
50
if (buffer) {
51
buffer.forEach(entry => {
52
console.log(`Type: ${entry.type}, Message: ${entry.message}`);
53
});
54
}
55
56
// Static write method for custom buffering
57
import type { ConsoleBuffer } from "@jest/console";
58
const customBuffer: ConsoleBuffer = [];
59
BufferedConsole.write(customBuffer, 'log', 'Custom message');
60
```
61
62
### Console Methods
63
64
All standard console methods are supported with proper buffering and stack trace preservation.
65
66
#### Logging Methods
67
68
```typescript { .api }
69
/**
70
* Log a message to the buffer
71
* @param firstArg - Primary message or data to log
72
* @param rest - Additional arguments to format and log
73
*/
74
log(firstArg: unknown, ...rest: Array<unknown>): void;
75
76
/**
77
* Log an informational message to the buffer
78
* @param firstArg - Primary message or data to log
79
* @param rest - Additional arguments to format and log
80
*/
81
info(firstArg: unknown, ...rest: Array<unknown>): void;
82
83
/**
84
* Log a debug message to the buffer
85
* @param firstArg - Primary message or data to log
86
* @param rest - Additional arguments to format and log
87
*/
88
debug(firstArg: unknown, ...rest: Array<unknown>): void;
89
90
/**
91
* Log a warning message to the buffer
92
* @param firstArg - Primary message or data to log
93
* @param rest - Additional arguments to format and log
94
*/
95
warn(firstArg: unknown, ...rest: Array<unknown>): void;
96
97
/**
98
* Log an error message to the buffer
99
* @param firstArg - Primary message or data to log
100
* @param rest - Additional arguments to format and log
101
*/
102
error(firstArg: unknown, ...rest: Array<unknown>): void;
103
```
104
105
#### Assertion Method
106
107
```typescript { .api }
108
/**
109
* Assert that a value is truthy, logging assertion failures to the buffer
110
* @param value - Value to test for truthiness
111
* @param message - Optional message or Error object for assertion failure
112
*/
113
assert(value: unknown, message?: string | Error): void;
114
```
115
116
#### Object Inspection Methods
117
118
```typescript { .api }
119
/**
120
* Log an object inspection to the buffer
121
* @param firstArg - Object to inspect
122
* @param options - Optional inspection options from Node.js util.inspect
123
*/
124
dir(firstArg: unknown, options?: InspectOptions): void;
125
126
/**
127
* Log XML-style object representation to the buffer
128
* @param firstArg - Primary object to log
129
* @param rest - Additional arguments to format and log
130
*/
131
dirxml(firstArg: unknown, ...rest: Array<unknown>): void;
132
```
133
134
#### Grouping Methods
135
136
```typescript { .api }
137
/**
138
* Start a new console group with optional title, increasing indentation depth
139
* @param title - Optional group title
140
* @param rest - Additional arguments to format in the title
141
*/
142
group(title?: string, ...rest: Array<unknown>): void;
143
144
/**
145
* Start a new collapsed console group with optional title
146
* @param title - Optional group title
147
* @param rest - Additional arguments to format in the title
148
*/
149
groupCollapsed(title?: string, ...rest: Array<unknown>): void;
150
151
/**
152
* End the current console group, decreasing indentation depth
153
*/
154
groupEnd(): void;
155
```
156
157
#### Counting Methods
158
159
```typescript { .api }
160
/**
161
* Increment and log a counter for the given label
162
* @param label - Counter label, defaults to 'default'
163
*/
164
count(label?: string): void;
165
166
/**
167
* Reset a counter for the given label to zero
168
* @param label - Counter label to reset, defaults to 'default'
169
*/
170
countReset(label?: string): void;
171
```
172
173
#### Timing Methods
174
175
```typescript { .api }
176
/**
177
* Start a timer with the given label
178
* @param label - Timer label, defaults to 'default'
179
*/
180
time(label?: string): void;
181
182
/**
183
* End a timer and log the elapsed time
184
* @param label - Timer label to end, defaults to 'default'
185
*/
186
timeEnd(label?: string): void;
187
188
/**
189
* Log the current elapsed time for a running timer
190
* @param label - Timer label to check, defaults to 'default'
191
* @param data - Additional data to log with the time
192
*/
193
timeLog(label?: string, ...data: Array<unknown>): void;
194
```