|
1 |
| -# Vitest Structured Summary |
| 1 | +# Vitest V8 JSON Coverage Summary |
2 | 2 |
|
3 |
| -A vitest plugin that generates a JSON file with structured summary output. |
| 3 | +A plugin for [Vitest](https://vitest.dev/) that generates a structured JSON coverage summary from V8 coverage data. This reporter creates a `coverage-summary.json` file with detailed coverage information for statements, branches, functions, and lines. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- ✅ Generates structured JSON coverage summary |
| 8 | +- ✅ Supports V8 coverage provider |
| 9 | +- ✅ Includes file-level and overall coverage statistics |
| 10 | +- ✅ Tracks uncovered lines for detailed analysis |
| 11 | +- ✅ Compatible with Vitest 3.0+ |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +npm install --save-dev vitest-v8-json-coverage-summary |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### Basic Setup |
| 22 | + |
| 23 | +Add the reporter to your Vitest configuration: |
| 24 | + |
| 25 | +```javascript |
| 26 | +// vitest.config.js |
| 27 | +import { defineConfig } from "vitest/config"; |
| 28 | +import V8JSONSummaryReporter from "vitest-v8-json-coverage-summary"; |
| 29 | + |
| 30 | +export default defineConfig({ |
| 31 | + test: { |
| 32 | + coverage: { |
| 33 | + provider: "v8", |
| 34 | + reporter: ["text", "json"], |
| 35 | + reportsDirectory: "./coverage", |
| 36 | + }, |
| 37 | + reporters: ["default", new V8JSONSummaryReporter()], |
| 38 | + }, |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +### TypeScript Configuration |
| 43 | + |
| 44 | +```typescript |
| 45 | +// vitest.config.ts |
| 46 | +import { defineConfig } from "vitest/config"; |
| 47 | +import V8JSONSummaryReporter from "vitest-v8-json-coverage-summary"; |
| 48 | + |
| 49 | +export default defineConfig({ |
| 50 | + test: { |
| 51 | + coverage: { |
| 52 | + provider: "v8", |
| 53 | + reporter: ["text", "json"], |
| 54 | + reportsDirectory: "./coverage", |
| 55 | + }, |
| 56 | + reporters: ["default", new V8JSONSummaryReporter()], |
| 57 | + }, |
| 58 | +}); |
| 59 | +``` |
| 60 | + |
| 61 | +## Output Format |
| 62 | + |
| 63 | +The reporter generates a `coverage-summary.json` file in your coverage directory with the following structure: |
| 64 | + |
| 65 | +```json |
| 66 | +{ |
| 67 | + "summary": { |
| 68 | + "statements": 85.5, |
| 69 | + "branches": 72.3, |
| 70 | + "functions": 90.1, |
| 71 | + "lines": 85.5 |
| 72 | + }, |
| 73 | + "files": [ |
| 74 | + { |
| 75 | + "file": "src/example.js", |
| 76 | + "statements": 95.2, |
| 77 | + "branches": 80.0, |
| 78 | + "functions": 100.0, |
| 79 | + "lines": 95.2, |
| 80 | + "uncoveredLines": [15, 23, 45] |
| 81 | + } |
| 82 | + ] |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### Coverage Metrics |
| 87 | + |
| 88 | +- **statements**: Percentage of statements covered |
| 89 | +- **branches**: Percentage of branch paths covered |
| 90 | +- **functions**: Percentage of functions covered |
| 91 | +- **lines**: Percentage of lines covered (matches statements for V8) |
| 92 | +- **uncoveredLines**: Array of line numbers that are not covered (optional) |
| 93 | + |
| 94 | +## API Reference |
| 95 | + |
| 96 | +### V8JSONSummaryReporter |
| 97 | + |
| 98 | +The main reporter class that implements Vitest's Reporter interface. |
| 99 | + |
| 100 | +#### Methods |
| 101 | + |
| 102 | +- `onInit(vitest: Vitest)`: Called when the reporter is initialized |
| 103 | +- `onCoverage(coverage: any)`: Called when coverage data is available |
| 104 | +- `onTestRunEnd()`: Called when the test run ends (kept for compatibility) |
| 105 | + |
| 106 | +### generateV8CoverageSummary(coverage: any): CoverageSummary |
| 107 | + |
| 108 | +Utility function that processes V8 coverage data and returns a structured summary. |
| 109 | + |
| 110 | +## Configuration Options |
| 111 | + |
| 112 | +The reporter uses the coverage configuration from your Vitest config: |
| 113 | + |
| 114 | +- `coverage.reportsDirectory`: Directory where the summary file will be written (default: `./coverage`) |
| 115 | +- `coverage.provider`: Must be set to `'v8'` for this reporter to work |
| 116 | + |
| 117 | +## Example Output |
| 118 | + |
| 119 | +After running your tests with coverage, you'll find a `coverage-summary.json` file that looks like this: |
| 120 | + |
| 121 | +```json |
| 122 | +{ |
| 123 | + "summary": { |
| 124 | + "statements": 87.5, |
| 125 | + "branches": 75.0, |
| 126 | + "functions": 92.3, |
| 127 | + "lines": 87.5 |
| 128 | + }, |
| 129 | + "files": [ |
| 130 | + { |
| 131 | + "file": "src/utils.js", |
| 132 | + "statements": 100.0, |
| 133 | + "branches": 100.0, |
| 134 | + "functions": 100.0, |
| 135 | + "lines": 100.0 |
| 136 | + }, |
| 137 | + { |
| 138 | + "file": "src/main.js", |
| 139 | + "statements": 75.0, |
| 140 | + "branches": 50.0, |
| 141 | + "functions": 85.7, |
| 142 | + "lines": 75.0, |
| 143 | + "uncoveredLines": [12, 15, 23] |
| 144 | + } |
| 145 | + ] |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +## Integration Examples |
| 150 | + |
| 151 | +### With CI/CD |
| 152 | + |
| 153 | +```yaml |
| 154 | +# GitHub Actions example |
| 155 | +- name: Run tests with coverage |
| 156 | + run: npm test |
| 157 | + |
| 158 | +- name: Upload coverage summary |
| 159 | + uses: actions/upload-artifact@v3 |
| 160 | + with: |
| 161 | + name: coverage-summary |
| 162 | + path: coverage/coverage-summary.json |
| 163 | +``` |
| 164 | +
|
| 165 | +### With Coverage Badges |
| 166 | +
|
| 167 | +You can use the generated JSON to create coverage badges or integrate with coverage reporting services. |
| 168 | +
|
| 169 | +## Contributing |
| 170 | +
|
| 171 | +1. Fork the repository |
| 172 | +2. Create a feature branch |
| 173 | +3. Make your changes |
| 174 | +4. Add tests if applicable |
| 175 | +5. Submit a pull request |
| 176 | +
|
| 177 | +## License |
| 178 | +
|
| 179 | +MIT License - see [LICENSE](LICENSE) file for details. |
| 180 | +
|
| 181 | +## Changelog |
| 182 | +
|
| 183 | +### 1.0.0 |
| 184 | +
|
| 185 | +- Initial release |
| 186 | +- V8 coverage support |
| 187 | +- JSON summary generation |
| 188 | +- File-level and overall coverage statistics |
0 commit comments