Skip to content

Commit fdeb2f4

Browse files
authored
feat(api): expose experimental_parseSpecifications (#8408)
1 parent c0ec08a commit fdeb2f4

File tree

8 files changed

+625
-2
lines changed

8 files changed

+625
-2
lines changed

docs/advanced/api/vitest.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,67 @@ function matchesProjectFilter(name: string): boolean
528528
Check if the name matches the current [project filter](/guide/cli#project). If there is no project filter, this will always return `true`.
529529

530530
It is not possible to programmatically change the `--project` CLI option.
531+
532+
## waitForTestRunEnd <Version>4.0.0</Version> {#waitfortestrunend}
533+
534+
```ts
535+
function waitForTestRunEnd(): Promise<void>
536+
```
537+
538+
If there is a test run happening, returns a promise that will resolve when the test run is finished.
539+
540+
## createCoverageProvider <Version>4.0.0</Version> {#createcoverageprovider}
541+
542+
```ts
543+
function createCoverageProvider(): Promise<CoverageProvider | null>
544+
```
545+
546+
Creates a coverage provider if `coverage` is enabled in the config. This is done automatically if you are running tests with [`start`](#start) or [`init`](#init) methods.
547+
548+
::: warning
549+
This method will also clean all previous reports if [`coverage.clean`](/config/#coverage-clean) is not set to `false`.
550+
:::
551+
552+
## experimental_parseSpecification <Version>4.0.0</Version> <Badge type="warning">experimental</Badge> {#parsespecification}
553+
554+
```ts
555+
function experimental_parseSpecification(
556+
specification: TestSpecification
557+
): Promise<TestModule>
558+
```
559+
560+
This function will collect all tests inside the file without running it. It uses rollup's `parseAst` function on top of Vite's `ssrTransform` to statically analyse the file and collect all tests that it can.
561+
562+
::: warning
563+
If Vitest could not analyse the name of the test, it will inject a hidden `dynamic: true` property to the test or a suite. The `id` will also have a postfix with `-dynamic` to not break tests that were collected properly.
564+
565+
Vitest always injects this property in tests with `for` or `each` modifier or tests with a dynamic name (like, `hello ${property}` or `'hello' + ${property}`). Vitest will still assign a name to the test, but it cannot be used to filter the tests.
566+
567+
There is nothing Vitest can do to make it possible to filter dynamic tests, but you can turn a test with `for` or `each` modifier into a name pattern with `escapeTestName` function:
568+
569+
```ts
570+
import { escapeTestName } from 'vitest/node'
571+
572+
// turns into /hello, .+?/
573+
const escapedPattern = new RegExp(escapeTestName('hello, %s', true))
574+
```
575+
:::
576+
577+
::: warning
578+
Vitest will only collect tests defined in the file. It will never follow imports to other files.
579+
580+
Vitest collects all `it`, `test`, `suite` and `describe` definitions even if they were not imported from the `vitest` entry point.
581+
:::
582+
583+
## experimental_parseSpecifications <Version>4.0.0</Version> <Badge type="warning">experimental</Badge> {#parsespecifications}
584+
585+
```ts
586+
function experimental_parseSpecifications(
587+
specifications: TestSpecification[],
588+
options?: {
589+
concurrency?: number
590+
}
591+
): Promise<TestModule[]>
592+
```
593+
594+
This method will [collect tests](#parsespecification) from an array of specifications. By default, Vitest will run only `os.availableParallelism()` number of specifications at a time to reduce the potential performance degradation. You can specify a different number in a second argument.

packages/runner/src/types/tasks.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ export interface TaskBase {
7171
line: number
7272
column: number
7373
}
74+
/**
75+
* If the test was collected by parsing the file AST, and the name
76+
* is not a static string, this property will be set to `true`.
77+
* @experimental
78+
*/
79+
dynamic?: boolean
7480
}
7581

7682
export interface TaskPopulated extends TaskBase {

0 commit comments

Comments
 (0)