Skip to content

Commit f859efc

Browse files
Namcheesheremet-va
andauthored
feat: add --exclude CLI flag (#4279)
Co-authored-by: Vladimir <[email protected]>
1 parent 4d55a02 commit f859efc

File tree

13 files changed

+94
-1
lines changed

13 files changed

+94
-1
lines changed

docs/guide/cli.md

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Run only [benchmark](https://vitest.dev/guide/features.html#benchmarking-experim
100100
| `--inspect-brk` | Enables Node.js inspector with break |
101101
| `--bail <number>` | Stop test execution when given number of tests have failed |
102102
| `--retry <times>` | Retry the test specific number of times if it fails |
103+
| `--exclude <glob>` | Additional file globs to be excluded from test |
103104
| `--expand-snapshot-diff` | Show full diff when snapshot fails |
104105
| `--typecheck [options]` | Custom options for typecheck pool. If passed without options, enables typechecking |
105106
| `--typecheck.enabled` | Enable typechecking alongside tests (default: `false`) |

packages/vitest/src/node/cli.ts

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ cli
5757
.option('--bail <number>', 'Stop test execution when given number of tests have failed (default: 0)')
5858
.option('--retry <times>', 'Retry the test specific number of times if it fails (default: 0)')
5959
.option('--diff <path>', 'Path to a diff config that will be used to generate diff interface')
60+
.option('--exclude <glob>', 'Additional file globs to be excluded from test')
6061
.option('--expand-snapshot-diff', 'Show full diff when snapshot fails')
6162
.option('--typecheck [options]', 'Custom options for typecheck pool')
6263
.option('--typecheck.enabled', 'Enable typechecking alongside tests (default: false)')
@@ -165,6 +166,11 @@ function normalizeCliOptions(argv: CliOptions): CliOptions {
165166
else
166167
delete argv.dir
167168

169+
if (argv.exclude) {
170+
argv.cliExclude = toArray(argv.exclude)
171+
delete argv.exclude
172+
}
173+
168174
if (argv.coverage) {
169175
const coverage = argv.coverage
170176
if (coverage.exclude)

packages/vitest/src/node/config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ export function resolveConfig(
192192
resolved.server.deps![option] = resolved.deps[option] as any
193193
})
194194

195+
if (resolved.cliExclude)
196+
resolved.exclude.push(...resolved.cliExclude)
197+
195198
// vitenode will try to import such file with native node,
196199
// but then our mocker will not work properly
197200
if (resolved.server.deps.inline !== true) {

packages/vitest/src/types/config.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,14 @@ export interface UserConfig extends InlineConfig {
752752
* Name of the project or projects to run.
753753
*/
754754
project?: string | string[]
755+
756+
/**
757+
* Additional exclude patterns
758+
*/
759+
cliExclude?: string[]
755760
}
756761

757-
export interface ResolvedConfig extends Omit<Required<UserConfig>, 'config' | 'filters' | 'browser' | 'coverage' | 'testNamePattern' | 'related' | 'api' | 'reporters' | 'resolveSnapshotPath' | 'benchmark' | 'shard' | 'cache' | 'sequence' | 'typecheck' | 'runner' | 'poolOptions' | 'pool'> {
762+
export interface ResolvedConfig extends Omit<Required<UserConfig>, 'config' | 'filters' | 'browser' | 'coverage' | 'testNamePattern' | 'related' | 'api' | 'reporters' | 'resolveSnapshotPath' | 'benchmark' | 'shard' | 'cache' | 'sequence' | 'typecheck' | 'runner' | 'poolOptions' | 'pool' | 'cliExclude'> {
758763
mode: VitestRunMode
759764

760765
base?: string
@@ -776,6 +781,7 @@ export interface ResolvedConfig extends Omit<Required<UserConfig>, 'config' | 'f
776781
defines: Record<string, any>
777782

778783
api?: ApiConfig
784+
cliExclude?: string[]
779785

780786
benchmark?: Required<Omit<BenchmarkUserOptions, 'outputFile'>> & Pick<BenchmarkUserOptions, 'outputFile'>
781787
shard?: {

pnpm-lock.yaml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { expect, test } from 'vitest'
2+
3+
import { add } from './math'
4+
5+
test('should add two numbers correctly', () => {
6+
expect(add(1, 2)).toBe(3)
7+
})

test/cli/fixtures/exclude/math.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function add(a: number, b: number): number {
2+
return a + b
3+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { expect, test } from 'vitest'
2+
3+
import { capitalize } from './string'
4+
5+
test('should capitalize strings correctly', () => {
6+
expect(capitalize('i Love Vitest')).toBe('I love vitest')
7+
})

test/cli/fixtures/exclude/string.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function capitalize(str: string): string {
2+
return str.slice(0, 1).toUpperCase() + str.slice(1).toLowerCase()
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
include: ['fixtures/exclude/*.test.ts'],
6+
},
7+
})

test/cli/package.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@vitest/test-cli",
3+
"type": "module",
4+
"private": true,
5+
"scripts": {
6+
"test": "vitest --exclude fixtures/exclude/**/string.test.ts"
7+
},
8+
"devDependencies": {
9+
"vite": "latest",
10+
"vitest": "workspace:*"
11+
}
12+
}

test/cli/test/exclude.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { expect, test } from 'vitest'
2+
3+
import { runVitestCli } from '../../test-utils'
4+
5+
test('should still test math.test.ts', async () => {
6+
const { stderr, stdout } = await runVitestCli(
7+
'run',
8+
'--config',
9+
'fixtures/exclude/vitest.exclude.config.ts',
10+
'--exclude',
11+
'fixtures/exclude/string.test.ts',
12+
)
13+
14+
expect(stdout).toContain(`✓ fixtures/exclude/math.test.ts`)
15+
expect(stdout).not.toContain(`string.test.ts`)
16+
expect(stderr).toBe('')
17+
})

test/cli/vitest.config.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { defineConfig } from 'vite'
2+
3+
export default defineConfig({
4+
test: {
5+
include: ['test/**.test.ts'],
6+
reporters: ['verbose'],
7+
testTimeout: 60_000,
8+
chaiConfig: {
9+
truncateThreshold: 999,
10+
},
11+
},
12+
})

0 commit comments

Comments
 (0)