0
# Jest Runner Integration
1
2
The main Jest runner that integrates ESLint into the Jest testing framework, treating linting as tests with full Jest compatibility.
3
4
## Capabilities
5
6
### Main Runner Export
7
8
The primary export that provides Jest runner functionality for ESLint integration.
9
10
```javascript { .api }
11
/**
12
* Jest runner that integrates ESLint into Jest testing workflow
13
* Created using create-jest-runner with ESLint-specific configuration
14
*/
15
const runner = require("jest-runner-eslint");
16
```
17
18
**Usage Examples:**
19
20
```javascript
21
// jest.config.js - Standalone runner
22
module.exports = {
23
runner: 'jest-runner-eslint',
24
displayName: 'lint',
25
testMatch: ['<rootDir>/src/**/*.js'],
26
};
27
28
// jest.config.js - Multi-project setup
29
module.exports = {
30
projects: [
31
{
32
displayName: 'test',
33
testMatch: ['<rootDir>/src/**/*.test.js'],
34
},
35
{
36
runner: 'jest-runner-eslint',
37
displayName: 'lint',
38
testMatch: ['<rootDir>/src/**/*.js'],
39
},
40
],
41
};
42
```
43
44
### ESLint Execution Engine
45
46
Core function that runs ESLint on individual files and formats results for Jest.
47
48
```javascript { .api }
49
/**
50
* Executes ESLint on a test file and returns Jest-compatible results
51
* Supports both legacy and flat ESLint configurations automatically
52
* @param testPath - Path to the file to lint
53
* @param config - Jest configuration object
54
* @param extraOptions - Additional options from runner (e.g., fix override)
55
* @returns Promise resolving to Jest TestResult object
56
*/
57
async function runESLint({
58
testPath: string,
59
config: JestConfig,
60
extraOptions: ExtraOptions
61
}): Promise<TestResult>;
62
63
interface JestConfig {
64
rootDir: string;
65
setupTestFrameworkScriptFile?: string;
66
setupFilesAfterEnv?: string[];
67
[key: string]: any;
68
}
69
70
interface ExtraOptions {
71
fix?: boolean;
72
[key: string]: any;
73
}
74
```
75
76
**Usage Examples:**
77
78
```javascript
79
// Internal usage by Jest runner framework
80
const result = await runESLint({
81
testPath: '/path/to/file.js',
82
config: jestConfig,
83
extraOptions: { fix: true }
84
});
85
86
// Result contains Jest-compatible test results
87
console.log(result.numFailingTests); // Number of ESLint errors
88
console.log(result.testResults); // Detailed error information
89
```
90
91
### Test Result Formatting
92
93
Functions that convert ESLint output into Jest-compatible test results.
94
95
```javascript { .api }
96
/**
97
* Creates Jest TestResult object from ESLint execution data
98
* @param options - Result creation options
99
* @returns Jest TestResult object
100
*/
101
function mkTestResults({
102
message?: string,
103
start: number,
104
end: number,
105
numFailingTests: number,
106
numPassingTests: number,
107
testPath: string,
108
assertionResults: TestAssertionResult[],
109
cliOptions?: ESLintCliOptions
110
}): TestResult;
111
112
/**
113
* Converts ESLint messages to Jest assertion results
114
* @param testPath - Path to the file that was linted
115
* @param report - ESLint report output
116
* @returns Array of Jest assertion results
117
*/
118
function mkAssertionResults(
119
testPath: string,
120
report: ESLintReport[]
121
): TestAssertionResult[];
122
123
interface ESLintReport {
124
messages: ESLintMessage[];
125
errorCount: number;
126
warningCount: number;
127
}
128
129
interface ESLintMessage {
130
message: string;
131
line: number;
132
column: number;
133
ruleId: string;
134
severity: number;
135
}
136
```
137
138
### Configuration Integration
139
140
Functions for integrating with Jest runner configuration and ESLint options.
141
142
```javascript { .api }
143
/**
144
* Computes the fix option value based on configuration
145
* @param options - Configuration options
146
* @returns Fix function or boolean value for ESLint
147
*/
148
function getComputedFixValue({
149
fix?: boolean,
150
quiet?: boolean,
151
fixDryRun?: boolean
152
}): boolean | ((result: { severity: number }) => boolean) | undefined;
153
154
/**
155
* Removes undefined values from configuration object
156
* @param object - Configuration object to clean
157
* @returns Object with undefined values removed
158
*/
159
function removeUndefinedFromObject(object: Record<string, any>): Record<string, any>;
160
161
/**
162
* Gets cached ESLint instance and related functions
163
* @param config - Jest configuration
164
* @param extraOptions - Additional options
165
* @returns Cached ESLint instance and utilities
166
*/
167
async function getCachedValues(
168
config: JestConfig,
169
extraOptions: ExtraOptions
170
): Promise<{
171
isPathIgnored: (path: string) => Promise<boolean>;
172
lintFiles: (files: string[]) => Promise<ESLintReport[]>;
173
formatter: (results: ESLintReport[]) => Promise<string>;
174
cliOptions: ESLintCliOptions;
175
ESLintConstructor: typeof ESLint;
176
}>;
177
```
178
179
**Usage Examples:**
180
181
```javascript
182
// Example of how ESLint execution handles different scenarios
183
const { isPathIgnored, lintFiles, formatter } = await getCachedValues(config, extraOptions);
184
185
// Check if file should be ignored
186
if (await isPathIgnored(testPath)) {
187
return mkTestResults({
188
start: Date.now(),
189
end: Date.now(),
190
testPath,
191
numFailingTests: 0,
192
numPassingTests: 0,
193
assertionResults: [{
194
title: 'ESLint',
195
status: 'skipped',
196
}],
197
});
198
}
199
200
// Run ESLint and format results
201
const report = await lintFiles([testPath]);
202
const message = await formatter(report);
203
```