Skip to content

Commit ebfe942

Browse files
authoredNov 19, 2024··
feat(cli): support excluding projects with --project=!pattern (#6924)
1 parent 03ca286 commit ebfe942

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed
 

‎docs/guide/cli-generated.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ Path to a custom tsconfig file
752752
- **CLI:** `--project <name>`
753753
- **Config:** [project](/config/#project)
754754

755-
The name of the project to run if you are using Vitest workspace feature. This can be repeated for multiple projects: `--project=1 --project=2`. You can also filter projects using wildcards like `--project=packages*`
755+
The name of the project to run if you are using Vitest workspace feature. This can be repeated for multiple projects: `--project=1 --project=2`. You can also filter projects using wildcards like `--project=packages*`, and exclude projects with `--project=!pattern`.
756756

757757
### slowTestThreshold
758758

‎packages/vitest/src/node/cli/cli-config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ export const cliOptionsConfig: VitestCLIOptions = {
700700
},
701701
project: {
702702
description:
703-
'The name of the project to run if you are using Vitest workspace feature. This can be repeated for multiple projects: `--project=1 --project=2`. You can also filter projects using wildcards like `--project=packages*`',
703+
'The name of the project to run if you are using Vitest workspace feature. This can be repeated for multiple projects: `--project=1 --project=2`. You can also filter projects using wildcards like `--project=packages*`, and exclude projects with `--project=!pattern`.',
704704
argument: '<name>',
705705
array: true,
706706
},

‎packages/vitest/src/utils/base.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,17 @@ export function escapeRegExp(s: string) {
2424
}
2525

2626
export function wildcardPatternToRegExp(pattern: string): RegExp {
27-
return new RegExp(
28-
`^${pattern.split('*').map(escapeRegExp).join('.*')}$`,
29-
'i',
30-
)
27+
const negated = pattern.startsWith('!')
28+
29+
if (negated) {
30+
pattern = pattern.slice(1)
31+
}
32+
33+
let regexp = `${pattern.split('*').map(escapeRegExp).join('.*')}$`
34+
35+
if (negated) {
36+
regexp = `(?!${regexp})`
37+
}
38+
39+
return new RegExp(`^${regexp}`, 'i')
3140
}

‎test/config/test/project.test.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import { expect, test } from 'vitest'
22
import { runVitest } from '../../test-utils'
33

4+
const allProjects = ['project_1', 'project_2', 'space_1']
5+
46
test.each([
57
{ pattern: 'project_1', expected: ['project_1'] },
6-
{ pattern: '*', expected: ['project_1', 'project_2', 'space_1'] },
8+
{ pattern: '*', expected: allProjects },
79
{ pattern: '*j*', expected: ['project_1', 'project_2'] },
810
{ pattern: 'project*', expected: ['project_1', 'project_2'] },
911
{ pattern: 'space*', expected: ['space_1'] },
12+
{ pattern: '!project_1', expected: ['project_2', 'space_1'] },
13+
{ pattern: '!project*', expected: ['space_1'] },
14+
{ pattern: '!project', expected: allProjects },
1015
])('should match projects correctly: $pattern', async ({ pattern, expected }) => {
11-
const { stdout, stderr } = await runVitest({
16+
const { ctx, stderr, stdout } = await runVitest({
1217
root: 'fixtures/project',
1318
reporters: ['basic'],
1419
project: pattern,
@@ -17,5 +22,14 @@ test.each([
1722
expect(stderr).toBeFalsy()
1823
expect(stdout).toBeTruthy()
1924

20-
expected.forEach(name => expect(stdout).toContain(name))
25+
for (const project of allProjects) {
26+
if (expected.includes(project)) {
27+
expect(stdout).toContain(project)
28+
}
29+
else {
30+
expect(stdout).not.toContain(project)
31+
}
32+
}
33+
34+
expect(ctx?.projects.map(p => p.name).sort()).toEqual(expected)
2135
})

0 commit comments

Comments
 (0)
Please sign in to comment.