Skip to content

Commit

Permalink
feat: add --exclude CLI flag (#4279)
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>
  • Loading branch information
Namchee and sheremet-va committed Dec 19, 2023
1 parent 4d55a02 commit f859efc
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/guide/cli.md
Expand Up @@ -100,6 +100,7 @@ Run only [benchmark](https://vitest.dev/guide/features.html#benchmarking-experim
| `--inspect-brk` | Enables Node.js inspector with break |
| `--bail <number>` | Stop test execution when given number of tests have failed |
| `--retry <times>` | Retry the test specific number of times if it fails |
| `--exclude <glob>` | Additional file globs to be excluded from test |
| `--expand-snapshot-diff` | Show full diff when snapshot fails |
| `--typecheck [options]` | Custom options for typecheck pool. If passed without options, enables typechecking |
| `--typecheck.enabled` | Enable typechecking alongside tests (default: `false`) |
Expand Down
6 changes: 6 additions & 0 deletions packages/vitest/src/node/cli.ts
Expand Up @@ -57,6 +57,7 @@ cli
.option('--bail <number>', 'Stop test execution when given number of tests have failed (default: 0)')
.option('--retry <times>', 'Retry the test specific number of times if it fails (default: 0)')
.option('--diff <path>', 'Path to a diff config that will be used to generate diff interface')
.option('--exclude <glob>', 'Additional file globs to be excluded from test')
.option('--expand-snapshot-diff', 'Show full diff when snapshot fails')
.option('--typecheck [options]', 'Custom options for typecheck pool')
.option('--typecheck.enabled', 'Enable typechecking alongside tests (default: false)')
Expand Down Expand Up @@ -165,6 +166,11 @@ function normalizeCliOptions(argv: CliOptions): CliOptions {
else
delete argv.dir

if (argv.exclude) {
argv.cliExclude = toArray(argv.exclude)
delete argv.exclude
}

if (argv.coverage) {
const coverage = argv.coverage
if (coverage.exclude)
Expand Down
3 changes: 3 additions & 0 deletions packages/vitest/src/node/config.ts
Expand Up @@ -192,6 +192,9 @@ export function resolveConfig(
resolved.server.deps![option] = resolved.deps[option] as any
})

if (resolved.cliExclude)
resolved.exclude.push(...resolved.cliExclude)

// vitenode will try to import such file with native node,
// but then our mocker will not work properly
if (resolved.server.deps.inline !== true) {
Expand Down
8 changes: 7 additions & 1 deletion packages/vitest/src/types/config.ts
Expand Up @@ -752,9 +752,14 @@ export interface UserConfig extends InlineConfig {
* Name of the project or projects to run.
*/
project?: string | string[]

/**
* Additional exclude patterns
*/
cliExclude?: string[]
}

export interface ResolvedConfig extends Omit<Required<UserConfig>, 'config' | 'filters' | 'browser' | 'coverage' | 'testNamePattern' | 'related' | 'api' | 'reporters' | 'resolveSnapshotPath' | 'benchmark' | 'shard' | 'cache' | 'sequence' | 'typecheck' | 'runner' | 'poolOptions' | 'pool'> {
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'> {
mode: VitestRunMode

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

api?: ApiConfig
cliExclude?: string[]

benchmark?: Required<Omit<BenchmarkUserOptions, 'outputFile'>> & Pick<BenchmarkUserOptions, 'outputFile'>
shard?: {
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/cli/fixtures/exclude/math.test.ts
@@ -0,0 +1,7 @@
import { expect, test } from 'vitest'

import { add } from './math'

test('should add two numbers correctly', () => {
expect(add(1, 2)).toBe(3)
})
3 changes: 3 additions & 0 deletions test/cli/fixtures/exclude/math.ts
@@ -0,0 +1,3 @@
export function add(a: number, b: number): number {
return a + b
}
7 changes: 7 additions & 0 deletions test/cli/fixtures/exclude/string.test.ts
@@ -0,0 +1,7 @@
import { expect, test } from 'vitest'

import { capitalize } from './string'

test('should capitalize strings correctly', () => {
expect(capitalize('i Love Vitest')).toBe('I love vitest')
})
3 changes: 3 additions & 0 deletions test/cli/fixtures/exclude/string.ts
@@ -0,0 +1,3 @@
export function capitalize(str: string): string {
return str.slice(0, 1).toUpperCase() + str.slice(1).toLowerCase()
}
7 changes: 7 additions & 0 deletions test/cli/fixtures/exclude/vitest.exclude.config.ts
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
include: ['fixtures/exclude/*.test.ts'],
},
})
12 changes: 12 additions & 0 deletions test/cli/package.json
@@ -0,0 +1,12 @@
{
"name": "@vitest/test-cli",
"type": "module",
"private": true,
"scripts": {
"test": "vitest --exclude fixtures/exclude/**/string.test.ts"
},
"devDependencies": {
"vite": "latest",
"vitest": "workspace:*"
}
}
17 changes: 17 additions & 0 deletions test/cli/test/exclude.test.ts
@@ -0,0 +1,17 @@
import { expect, test } from 'vitest'

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

test('should still test math.test.ts', async () => {
const { stderr, stdout } = await runVitestCli(
'run',
'--config',
'fixtures/exclude/vitest.exclude.config.ts',
'--exclude',
'fixtures/exclude/string.test.ts',
)

expect(stdout).toContain(`✓ fixtures/exclude/math.test.ts`)
expect(stdout).not.toContain(`string.test.ts`)
expect(stderr).toBe('')
})
12 changes: 12 additions & 0 deletions test/cli/vitest.config.ts
@@ -0,0 +1,12 @@
import { defineConfig } from 'vite'

export default defineConfig({
test: {
include: ['test/**.test.ts'],
reporters: ['verbose'],
testTimeout: 60_000,
chaiConfig: {
truncateThreshold: 999,
},
},
})

0 comments on commit f859efc

Please sign in to comment.