Skip to content

Commit c8e6c7f

Browse files
authored
feat(api): add filters to createSpecification (#9336)
1 parent f396792 commit c8e6c7f

File tree

29 files changed

+161
-48
lines changed

29 files changed

+161
-48
lines changed

docs/api/advanced/test-specification.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ You can only create a specification by calling [`createSpecification`](/api/adva
77
```ts
88
const specification = project.createSpecification(
99
resolve('./example.test.ts'),
10-
[20, 40], // optional test lines
10+
{
11+
testLines: [20, 40],
12+
testNamePattern: /hello world/,
13+
testIds: ['1223128da3_0_0_0', '1223128da3_0_0'],
14+
} // optional test filters
1115
)
1216
```
1317

14-
`createSpecification` expects resolved module ID. It doesn't auto-resolve the file or check that it exists on the file system.
18+
`createSpecification` expects resolved module identifier. It doesn't auto-resolve the file or check that it exists on the file system.
1519

1620
## taskId
1721

@@ -40,7 +44,7 @@ Instance of [`TestModule`](/api/advanced/test-module) associated with the specif
4044
The [`pool`](/config/#pool) in which the test module will run.
4145

4246
::: danger
43-
It's possible to have multiple pools in a single test project with [`poolMatchGlob`](/config/#poolmatchglob) and [`typecheck.enabled`](/config/#typecheck-enabled). This means it's possible to have several specifications with the same `moduleId` but different `pool`. In Vitest 4, the project will only support a single pool, and this property will be removed.
47+
It's possible to have multiple pools in a single test project with [`typecheck.enabled`](/config/#typecheck-enabled). This means it's possible to have several specifications with the same `moduleId` but different `pool`. In later versions, the project will only support a single pool.
4448
:::
4549

4650
## testLines
@@ -70,6 +74,14 @@ describe('a group of tests', () => { // [!code error]
7074
```
7175
:::
7276

77+
## testNamePattern <Version>4.1.0</Version> {#testnamepattern}
78+
79+
A regexp that matches the name of the test in this module. This value will override the global [`testNamePattern`](/config/testnamepattern) option if it's set.
80+
81+
## testIds <Version>4.1.0</Version> {#testids}
82+
83+
The ids of tasks inside of this specification to run.
84+
7385
## toJSON
7486

7587
```ts

packages/runner/src/collect.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export async function collectTests(
3535
{ 'code.file.path': filepath },
3636
async () => {
3737
const testLocations = typeof spec === 'string' ? undefined : spec.testLocations
38+
const testNamePattern = typeof spec === 'string' ? undefined : spec.testNamePattern
39+
const testIds = typeof spec === 'string' ? undefined : spec.testIds
3840

3941
const file = createFileTask(filepath, config.root, config.name, runner.pool, runner.viteEnvironment)
4042
setFileContext(file, Object.create(null))
@@ -108,8 +110,9 @@ export async function collectTests(
108110
const hasOnlyTasks = someTasksAreOnly(file)
109111
interpretTaskModes(
110112
file,
111-
config.testNamePattern,
113+
testNamePattern ?? config.testNamePattern,
112114
testLocations,
115+
testIds,
113116
hasOnlyTasks,
114117
false,
115118
config.allowOnly,

packages/runner/src/types/runner.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export interface VitestRunnerConfig {
4848
export interface FileSpecification {
4949
filepath: string
5050
testLocations: number[] | undefined
51+
testNamePattern: RegExp | undefined
52+
testIds: string[] | undefined
5153
}
5254

5355
export type VitestRunnerImportSource = 'collect' | 'setup'

packages/runner/src/utils/collect.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function interpretTaskModes(
1111
file: Suite,
1212
namePattern?: string | RegExp,
1313
testLocations?: number[] | undefined,
14+
testIds?: string[] | undefined,
1415
onlyMode?: boolean,
1516
parentIsOnly?: boolean,
1617
allowOnly?: boolean,
@@ -50,7 +51,7 @@ export function interpretTaskModes(
5051

5152
let hasLocationMatch = parentMatchedWithLocation
5253
// Match test location against provided locations, only run if present
53-
// in `testLocations`. Note: if `includeTaskLocations` is not enabled,
54+
// in `testLocations`. Note: if `includeTaskLocation` is not enabled,
5455
// all test will be skipped.
5556
if (testLocations !== undefined && testLocations.length !== 0) {
5657
if (t.location && testLocations?.includes(t.location.line)) {
@@ -70,6 +71,9 @@ export function interpretTaskModes(
7071
if (namePattern && !getTaskFullName(t).match(namePattern)) {
7172
t.mode = 'skip'
7273
}
74+
if (testIds && !testIds.includes(t.id)) {
75+
t.mode = 'skip'
76+
}
7377
}
7478
else if (t.type === 'suite') {
7579
if (t.mode === 'skip') {

packages/vitest/src/api/setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { ViteDevServer } from 'vite'
55
import type { WebSocket } from 'ws'
66
import type { Vitest } from '../node/core'
77
import type { TestCase, TestModule } from '../node/reporters/reported-tasks'
8-
import type { TestSpecification } from '../node/spec'
8+
import type { TestSpecification } from '../node/test-specification'
99
import type { Reporter } from '../node/types/reporter'
1010
import type { LabelColor, ModuleGraphData, UserConsoleLog } from '../types/general'
1111
import type {

packages/vitest/src/node/ast-collect.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ function createFileTask(
377377
file,
378378
options.testNamePattern,
379379
undefined,
380+
undefined,
380381
hasOnly,
381382
false,
382383
options.allowOnly,

packages/vitest/src/node/cache/files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Stats } from 'node:fs'
2-
import type { TestSpecification } from '../spec'
2+
import type { TestSpecification } from '../test-specification'
33
import fs from 'node:fs'
44
import { relative } from 'pathe'
55

packages/vitest/src/node/cli/cli-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { InlineConfig as ViteInlineConfig, UserConfig as ViteUserConfig } f
22
import type { environments } from '../../integrations/env'
33
import type { Vitest, VitestOptions } from '../core'
44
import type { TestModule, TestSuite } from '../reporters/reported-tasks'
5-
import type { TestSpecification } from '../spec'
5+
import type { TestSpecification } from '../test-specification'
66
import type { UserConfig, VitestEnvironment, VitestRunMode } from '../types/config'
77
import { mkdirSync, writeFileSync } from 'node:fs'
88
import { dirname, isAbsolute, relative, resolve } from 'pathe'

packages/vitest/src/node/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { CliOptions } from './cli/cli-api'
1010
import type { VitestFetchFunction } from './environments/fetchModule'
1111
import type { ProcessPool } from './pool'
1212
import type { TestModule } from './reporters/reported-tasks'
13-
import type { TestSpecification } from './spec'
13+
import type { TestSpecification } from './test-specification'
1414
import type { ResolvedConfig, TestProjectConfiguration, UserConfig, VitestRunMode } from './types/config'
1515
import type { CoverageProvider, ResolvedCoverageOptions } from './types/coverage'
1616
import type { Reporter } from './types/reporter'

packages/vitest/src/node/pool.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ContextTestEnvironment } from '../types/worker'
33
import type { Vitest } from './core'
44
import type { PoolTask } from './pools/types'
55
import type { TestProject } from './project'
6-
import type { TestSpecification } from './spec'
6+
import type { TestSpecification } from './test-specification'
77
import type { BuiltinPool, ResolvedConfig } from './types/config'
88
import * as nodeos from 'node:os'
99
import { isatty } from 'node:tty'
@@ -147,7 +147,12 @@ export function createPool(ctx: Vitest): ProcessPool {
147147

148148
taskGroup.push({
149149
context: {
150-
files: specs.map(spec => ({ filepath: spec.moduleId, testLocations: spec.testLines })),
150+
files: specs.map(spec => ({
151+
filepath: spec.moduleId,
152+
testLocations: spec.testLines,
153+
testNamePattern: spec.testNamePattern,
154+
testIds: spec.testIds,
155+
})),
151156
invalidates,
152157
providedContext: project.getProvidedContext(),
153158
workerId: workerId++,

0 commit comments

Comments
 (0)