0
# Output Formatting
1
2
Output formatting utilities for converting console buffer entries into readable, styled string output with proper indentation and stack traces.
3
4
## Capabilities
5
6
### getConsoleOutput Function
7
8
Formats console buffer entries into a readable string output with proper styling, indentation, and stack traces for Jest test results.
9
10
```typescript { .api }
11
/**
12
* Formats console buffer entries into readable string output
13
* Converts an array of log entries into formatted output with styling and stack traces
14
* @param buffer - Array of log entries to format
15
* @param config - Stack trace configuration for formatting
16
* @param globalConfig - Jest global configuration for output styling
17
* @returns Formatted string with styled console output
18
*/
19
function getConsoleOutput(
20
buffer: ConsoleBuffer,
21
config: StackTraceConfig,
22
globalConfig: Config.GlobalConfig
23
): string;
24
```
25
26
**Usage Examples:**
27
28
```typescript
29
import { BufferedConsole, getConsoleOutput } from "@jest/console";
30
import type { Config } from "@jest/types";
31
32
// Create and use a buffered console
33
const console = new BufferedConsole();
34
console.log("Hello, World!");
35
console.warn("This is a warning");
36
console.error("This is an error");
37
38
// Get the buffer
39
const buffer = console.getBuffer();
40
41
if (buffer) {
42
// Mock configuration objects (in real Jest these come from the test framework)
43
const stackTraceConfig = {
44
rootDir: process.cwd(),
45
testMatch: ["**/*.test.js"]
46
};
47
48
const globalConfig: Config.GlobalConfig = {
49
verbose: true,
50
noStackTrace: false,
51
// ... other Jest config properties
52
} as Config.GlobalConfig;
53
54
// Format the output
55
const formattedOutput = getConsoleOutput(buffer, stackTraceConfig, globalConfig);
56
console.log(formattedOutput);
57
}
58
59
// Example of formatted output:
60
// console.log
61
// Hello, World!
62
// at Object.<anonymous> (/path/to/file.js:2:9)
63
//
64
// console.warn
65
// This is a warning
66
// at Object.<anonymous> (/path/to/file.js:3:9)
67
//
68
// console.error
69
// This is an error
70
// at Object.<anonymous> (/path/to/file.js:4:9)
71
```
72
73
## Dependencies
74
75
The output formatting functionality relies on external Jest utilities:
76
77
### Required Types
78
79
```typescript { .api }
80
// From @jest/types
81
interface Config {
82
GlobalConfig: {
83
verbose?: boolean;
84
noStackTrace?: boolean;
85
// Other Jest global configuration properties
86
};
87
}
88
89
// From jest-message-util
90
interface StackTraceConfig {
91
rootDir: string;
92
testMatch?: string[];
93
// Other stack trace configuration properties
94
}
95
96
interface StackTraceOptions {
97
noCodeFrame: boolean;
98
noStackTrace: boolean;
99
}
100
```
101
102
## Output Styling
103
104
The formatting applies different styles based on log types:
105
106
- **Standard logs** (`log`, `info`, `debug`): Plain text with indentation
107
- **Warnings** (`warn`): Yellow styling with stack traces when enabled
108
- **Errors** (`error`): Red styling with stack traces when enabled
109
- **All types**: Proper indentation based on Jest's verbose mode setting
110
111
The output includes:
112
- Type indicators (e.g., "console.log", "console.warn")
113
- Indented message content
114
- Stack trace information (when not suppressed)
115
- Color coding for warnings and errors using chalk styling