Skip to content

Commit e20538a

Browse files
authored
feat: add blob reporter (#5663)
1 parent 7adb8e8 commit e20538a

File tree

36 files changed

+658
-89
lines changed

36 files changed

+658
-89
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ docs/public/sponsors
2222
.eslintcache
2323
docs/.vitepress/cache/
2424
!test/cli/fixtures/dotted-files/**/.cache
25+
.vitest-reports

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Next generation testing framework powered by Vite.
3232

3333
- [Vite](https://vitejs.dev/)'s config, transformers, resolvers, and plugins. Use the same setup from your app!
3434
- [Jest Snapshot](https://jestjs.io/docs/snapshot-testing)
35-
- [Chai](https://www.chaijs.com/) built-in for assertions, with [Jest expect](https://jestjs.io/docs/expect) compatible APIs.
35+
- [Chai](https://www.chaijs.com/) built-in for assertions, with [Jest expect](https://jestjs.io/docs/expect) compatible APIs
3636
- [Smart & instant watch mode](https://vitest.dev/guide/features.html#watch-mode), like HMR for tests!
3737
- [Native code coverage](https://vitest.dev/guide/features.html#coverage) via [`v8`](https://v8.dev/blog/javascript-code-coverage) or [`istanbul`](https://istanbul.js.org/).
3838
- [Tinyspy](https://github.com/tinylibs/tinyspy) built-in for mocking, stubbing, and spies.
@@ -45,6 +45,7 @@ Next generation testing framework powered by Vite.
4545
- ESM first, top level await
4646
- Out-of-box TypeScript / JSX support
4747
- Filtering, timeouts, concurrent for suite and tests
48+
- Sharding support
4849

4950
> Vitest 1.0 requires Vite >=v5.0.0 and Node >=v18.0.0
5051

docs/.vitepress/components.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-disable */
2-
/* prettier-ignore */
32
// @ts-nocheck
43
// Generated by unplugin-vue-components
54
// Read more: https://github.com/vuejs/core/pull/3399
65
export {}
76

7+
/* prettier-ignore */
88
declare module 'vue' {
99
export interface GlobalComponents {
1010
Contributors: typeof import('./components/Contributors.vue')['default']

docs/.vitepress/components/FeaturesList.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
dir="auto"
55
flex="~ col gap2 md:gap-3"
66
>
7-
<ListItem><a target="_blank" href="https://vitejs.dev" rel="noopener noreferrer">Vite</a>'s config, transformers, resolvers, and plugins.</ListItem>
7+
<ListItem><a target="_blank" href="https://vitejs.dev" rel="noopener noreferrer">Vite</a>'s config, transformers, resolvers, and plugins</ListItem>
88
<ListItem>Use the same setup from your app to run the tests!</ListItem>
99
<ListItem><a target="_blank" href="https://twitter.com/antfu7/status/1468233216939245579" rel="noopener noreferrer">Smart & instant watch mode, like HMR for tests!</a></ListItem>
1010
<ListItem>Component testing for Vue, React, Svelte, Lit, Marko and more</ListItem>
@@ -25,6 +25,7 @@
2525
<ListItem>Code coverage via <a target="_blank" href="https://v8.dev/blog/javascript-code-coverage" rel="noopener noreferrer">v8</a> or <a target="_blank" href="https://istanbul.js.org/" rel="noopener noreferrer">istanbul</a></ListItem>
2626
<ListItem>Rust-like <a href="/guide/in-source">in-source testing</a></ListItem>
2727
<ListItem>Type Testing via <a target="_blank" href="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/mmkal/expect-type" rel="noopener noreferrer">expect-type</a></ListItem>
28+
<ListItem>Sharding support</ListItem>
2829
</ul>
2930
</template>
3031

docs/guide/cli-table.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,4 @@
120120
| `--no-color` | Removes colors from the console output |
121121
| `--clearScreen` | Clear terminal screen when re-running tests during watch mode (default: `true`) |
122122
| `--standalone` | Start Vitest without running tests. File filters will be ignored, tests will be running only on change (default: `false`) |
123+
| `--mergeReports [path]` | Paths to blob reports directory. If this options is used, Vitest won't run any tests, it will only report previously recorded tests |

docs/guide/cli.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,18 @@ vitest --api=false
113113
You cannot use this option with `--watch` enabled (enabled in dev by default).
114114
:::
115115

116+
::: tip
117+
If `--reporter=blob` is used without an output file, the default path will include the current shard config to avoid collisions with other Vitest processes.
118+
:::
119+
120+
### merge-reports
121+
122+
- **Type:** `boolean | string`
123+
124+
Merges every blob report located in the specified folder (`.vitest-reports` by default). You can use any reporters with this command (except [`blob`](/guide/reporters#blob-reporter)):
125+
126+
```sh
127+
vitest --merge-reports --reporter=junit
128+
```
129+
116130
[cac's dot notation]: https://github.com/cacjs/cac#dot-nested-options

docs/guide/features.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,14 @@ test('my types work properly', () => {
229229
assertType(mount({ name: 42 }))
230230
})
231231
```
232+
233+
## Sharding
234+
235+
Run tests on different machines using [`--shard`](/guide/cli#shard) and [`--reporter=blob`](/guide/reporters#blob-reporter) flags.
236+
All test results can be merged at the end of your CI pipeline using `--merge-reports` command:
237+
238+
```bash
239+
vitest --shard=1/2 --reporter=blob
240+
vitest --shard=2/2 --reporter=blob
241+
vitest --merge-reports --reporter=junit
242+
```

docs/guide/reporters.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,26 @@ export default defineConfig({
462462
<img alt="Github Actions" img-dark src="https://github.com/vitest-dev/vitest/assets/4232207/336cddc2-df6b-4b8a-8e72-4d00010e37f5">
463463
<img alt="Github Actions" img-light src="https://github.com/vitest-dev/vitest/assets/4232207/ce8447c1-0eab-4fe1-abef-d0d322290dca">
464464

465+
### Blob Reporter
466+
467+
Stores test results on the machine so they can be later merged using [`--merge-reports`](/guide/cli#merge-reports) command.
468+
By default, stores all results in `.vitest-reports` folder, but can be overriden with `--outputFile` or `--outputFile.blob` flags.
469+
470+
```bash
471+
npx vitest --reporter=blob --outputFile=reports/blob-1.json
472+
```
473+
474+
We recommend using this reporter if you are running Vitest on different machines with the [`--shard`](/guide/cli#shard) flag.
475+
All blob reports can be merged into any report by using `--merge-reports` command at the end of your CI pipeline:
476+
477+
```bash
478+
npx vitest --merge-reports=reports --reporter=json --reporter=default
479+
```
480+
481+
::: tip
482+
Both `--reporter=blob` and `--merge-reports` do not work in watch mode.
483+
:::
484+
465485
## Custom Reporters
466486

467487
You can use third-party custom reporters installed from NPM by specifying their package name in the reporters' option:

packages/runner/src/collect.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { relative } from 'pathe'
21
import { processError } from '@vitest/utils/error'
32
import type { File, SuiteHooks } from './types'
43
import type { VitestRunner } from './types/runner'
5-
import { calculateSuiteHash, generateHash, interpretTaskModes, someTasksAreOnly } from './utils/collect'
4+
import { calculateSuiteHash, createFileTask, interpretTaskModes, someTasksAreOnly } from './utils/collect'
65
import { clearCollectorContext, createSuiteHooks, getDefaultSuite } from './suite'
76
import { getHooks, setHooks } from './map'
87
import { collectorContext } from './context'
@@ -16,19 +15,9 @@ export async function collectTests(paths: string[], runner: VitestRunner): Promi
1615
const config = runner.config
1716

1817
for (const filepath of paths) {
19-
const path = relative(config.root, filepath)
20-
const file: File = {
21-
id: generateHash(`${path}${config.name || ''}`),
22-
name: path,
23-
type: 'suite',
24-
mode: 'run',
25-
filepath,
26-
tasks: [],
27-
meta: Object.create(null),
28-
projectName: config.name,
29-
file: undefined!,
30-
}
31-
file.file = file
18+
const file = createFileTask(filepath, config.root, config.name)
19+
20+
runner.onCollectStart?.(file)
3221

3322
clearCollectorContext(filepath, runner)
3423

packages/runner/src/types/runner.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export interface VitestRunner {
5353
* First thing that's getting called before actually collecting and running tests.
5454
*/
5555
onBeforeCollect?: (paths: string[]) => unknown
56+
/**
57+
* Called after the file task was created but not collected yet.
58+
*/
59+
onCollectStart?: (file: File) => unknown
5660
/**
5761
* Called after collecting tests and before "onBeforeRun".
5862
*/

0 commit comments

Comments
 (0)