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: add --exclude CLI flag #4279

Merged
merged 25 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
662db02
feat: add --exclude cli flag
Namchee Oct 9, 2023
27a21f4
Merge branch 'main' of github.com:vitest-dev/vitest into feat/exclude…
Namchee Oct 9, 2023
a720dea
Merge branch 'main' of github.com:vitest-dev/vitest into feat/exclude…
Namchee Oct 10, 2023
dd96d97
docs: add documentation about --exclude CLI flag
Namchee Oct 10, 2023
eab5ab4
chore: resolve merge conflicts
Namchee Oct 19, 2023
ae05f63
refactor: extend config instead of init from default const
Namchee Oct 29, 2023
0d016b0
chore: merge with upstream
Namchee Oct 29, 2023
e8ce3c7
test: add broken test
Namchee Oct 29, 2023
05994e1
Merge branch 'main' of github.com:vitest-dev/vitest into feat/exclude…
Namchee Oct 31, 2023
9d382a3
test: create cli test package
Namchee Oct 31, 2023
2a3b33e
test: add test fixtures
Namchee Nov 1, 2023
814a4de
fix(test): explicitly include the fixtures
Namchee Nov 1, 2023
936d988
test: pass test
Namchee Nov 2, 2023
818d116
Merge branch 'main' into feat/exclude-cli
Namchee Nov 2, 2023
018f983
Merge branch 'main' into feat/exclude-cli
Namchee Nov 11, 2023
78a5c75
Merge branch 'main' into feat/exclude-cli
Namchee Nov 13, 2023
568cde5
Merge branch 'main' into feat/exclude-cli
Namchee Nov 15, 2023
bc18b46
Merge branch 'main' into feat/exclude-cli
Namchee Nov 20, 2023
0f70521
Merge branch 'main' into feat/exclude-cli
sheremet-va Nov 27, 2023
fb06c53
Merge branch 'main' into feat/exclude-cli
sheremet-va Dec 9, 2023
3c77c76
Merge branch 'main' into feat/exclude-cli
sheremet-va Dec 19, 2023
73cc816
chore: update specifier
sheremet-va Dec 19, 2023
c9d1430
test: fix issue
sheremet-va Dec 19, 2023
29f8ff0
Merge branch 'main' into feat/exclude-cli
sheremet-va Dec 19, 2023
f6da69f
refactor: use hidden cliExclude to filter CLI args
sheremet-va Dec 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/guide/cli.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from 'vite'

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