Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): support wildcards in --project option #5295

Merged
2 changes: 1 addition & 1 deletion docs/guide/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Run only [benchmark](https://vitest.dev/guide/features.html#benchmarking-experim
| `--typecheck [options]` | Custom options for typecheck pool. If passed without options, enables typechecking |
| `--typecheck.enabled` | Enable typechecking alongside tests (default: `false`) |
| `--typecheck.only` | Run only typecheck tests. This automatically enables typecheck (default: `false`) |
| `--project` | 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` |
| `--project` | 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*` |
| `-h, --help` | Display available CLI options |

::: tip
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/cli/cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ export const cliOptionsConfig: VitestCLIOptions = {
},
},
project: {
description: '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',
description: '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*',
argument: '<name>',
array: true,
},
Expand Down
12 changes: 11 additions & 1 deletion packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,17 @@ export class Vitest {
const projects = await this.resolveWorkspace(cliOptions)
this.projects = projects
this.resolvedProjects = projects
const filteredProjects = toArray(resolved.project)
const filteredProjects = toArray(resolved.project).reduce((acc, s) => {
hi-ogawa marked this conversation as resolved.
Show resolved Hide resolved
if (s.includes('*')) {
const pattern = new RegExp(`^${s.replace(/\*/, '.*')}`)
hi-ogawa marked this conversation as resolved.
Show resolved Hide resolved
const included = projects.filter(p => pattern.test(p.getName())).map(p => p.getName())
acc.push(...included)
}
else {
acc.push(s)
}
return acc
}, [] as string[])
if (filteredProjects.length)
this.projects = this.projects.filter(p => filteredProjects.includes(p.getName()))
if (!this.coreWorkspaceProject)
Expand Down
5 changes: 5 additions & 0 deletions test/cli/fixtures/project/packages/project_1/base.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { expect, test } from "vitest";

test("", () => {
expect(1).toBe(1);
})
5 changes: 5 additions & 0 deletions test/cli/fixtures/project/packages/project_2/base.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { expect, test } from "vitest";

test("", () => {
expect(1).toBe(1);
})
5 changes: 5 additions & 0 deletions test/cli/fixtures/project/packages/space_1/base.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { expect, test } from "vitest";

test("", () => {
expect(1).toBe(1);
})
3 changes: 3 additions & 0 deletions test/cli/fixtures/project/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from "vitest/config";

export default defineConfig({})
3 changes: 3 additions & 0 deletions test/cli/fixtures/project/vitest.workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default [
'packages/*'
]
30 changes: 30 additions & 0 deletions test/cli/test/project.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect, test } from 'vitest'

import { runVitestCli } from '../../test-utils'

test.each([
'project*',
'space*',
])('should match projects correctly: %s', async (project) => {
const { stdout, stderr } = await runVitestCli(
'run',
'--root',
'fixtures/project',
'--project',
project,
)

expect(stderr).toBeFalsy()
expect(stdout).toBeTruthy()

if (project === 'project*') {
expect(stdout).toContain('project_1')
expect(stdout).toContain('project_2')
expect(stdout).not.toContain('space_1')
}
else {
expect(stdout).toContain('space_1')
expect(stdout).not.toContain('project_1')
expect(stdout).not.toContain('project_2')
}
})