Skip to content

Commit f859efc

Browse files
Namcheesheremet-va
andauthored
feat: add --exclude CLI flag (#4279)
Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 7 additions & 1 deletion
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

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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+
}
Lines changed: 7 additions & 0 deletions
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+
})
Lines changed: 3 additions & 0 deletions
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+
}
Lines changed: 7 additions & 0 deletions
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+
})

0 commit comments

Comments
 (0)