From f859efc0940f6e72766b56f528975f62578f8762 Mon Sep 17 00:00:00 2001 From: Cristopher <32661241+Namchee@users.noreply.github.com> Date: Tue, 19 Dec 2023 20:53:26 +0700 Subject: [PATCH] feat: add `--exclude` CLI flag (#4279) Co-authored-by: Vladimir --- docs/guide/cli.md | 1 + packages/vitest/src/node/cli.ts | 6 ++++++ packages/vitest/src/node/config.ts | 3 +++ packages/vitest/src/types/config.ts | 8 +++++++- pnpm-lock.yaml | 9 +++++++++ test/cli/fixtures/exclude/math.test.ts | 7 +++++++ test/cli/fixtures/exclude/math.ts | 3 +++ test/cli/fixtures/exclude/string.test.ts | 7 +++++++ test/cli/fixtures/exclude/string.ts | 3 +++ .../fixtures/exclude/vitest.exclude.config.ts | 7 +++++++ test/cli/package.json | 12 ++++++++++++ test/cli/test/exclude.test.ts | 17 +++++++++++++++++ test/cli/vitest.config.ts | 12 ++++++++++++ 13 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 test/cli/fixtures/exclude/math.test.ts create mode 100644 test/cli/fixtures/exclude/math.ts create mode 100644 test/cli/fixtures/exclude/string.test.ts create mode 100644 test/cli/fixtures/exclude/string.ts create mode 100644 test/cli/fixtures/exclude/vitest.exclude.config.ts create mode 100644 test/cli/package.json create mode 100644 test/cli/test/exclude.test.ts create mode 100644 test/cli/vitest.config.ts diff --git a/docs/guide/cli.md b/docs/guide/cli.md index 72d0dc2f96fb..fc8140e9baa9 100644 --- a/docs/guide/cli.md +++ b/docs/guide/cli.md @@ -100,6 +100,7 @@ Run only [benchmark](https://vitest.dev/guide/features.html#benchmarking-experim | `--inspect-brk` | Enables Node.js inspector with break | | `--bail ` | Stop test execution when given number of tests have failed | | `--retry ` | Retry the test specific number of times if it fails | +| `--exclude ` | 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`) | diff --git a/packages/vitest/src/node/cli.ts b/packages/vitest/src/node/cli.ts index 1d28f29bf8ce..77734459a400 100644 --- a/packages/vitest/src/node/cli.ts +++ b/packages/vitest/src/node/cli.ts @@ -57,6 +57,7 @@ cli .option('--bail ', 'Stop test execution when given number of tests have failed (default: 0)') .option('--retry ', 'Retry the test specific number of times if it fails (default: 0)') .option('--diff ', 'Path to a diff config that will be used to generate diff interface') + .option('--exclude ', '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)') @@ -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) diff --git a/packages/vitest/src/node/config.ts b/packages/vitest/src/node/config.ts index f2722d014fc9..55ea0813a500 100644 --- a/packages/vitest/src/node/config.ts +++ b/packages/vitest/src/node/config.ts @@ -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) { diff --git a/packages/vitest/src/types/config.ts b/packages/vitest/src/types/config.ts index e3605e7c7633..6e91559d18d4 100644 --- a/packages/vitest/src/types/config.ts +++ b/packages/vitest/src/types/config.ts @@ -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, 'config' | 'filters' | 'browser' | 'coverage' | 'testNamePattern' | 'related' | 'api' | 'reporters' | 'resolveSnapshotPath' | 'benchmark' | 'shard' | 'cache' | 'sequence' | 'typecheck' | 'runner' | 'poolOptions' | 'pool'> { +export interface ResolvedConfig extends Omit, 'config' | 'filters' | 'browser' | 'coverage' | 'testNamePattern' | 'related' | 'api' | 'reporters' | 'resolveSnapshotPath' | 'benchmark' | 'shard' | 'cache' | 'sequence' | 'typecheck' | 'runner' | 'poolOptions' | 'pool' | 'cliExclude'> { mode: VitestRunMode base?: string @@ -776,6 +781,7 @@ export interface ResolvedConfig extends Omit, 'config' | 'f defines: Record api?: ApiConfig + cliExclude?: string[] benchmark?: Required> & Pick shard?: { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fd5712f38867..5126ab2de462 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1537,6 +1537,15 @@ importers: specifier: workspace:* version: link:../../packages/vitest + test/cli: + devDependencies: + vite: + specifier: ^5.0.0 + version: 5.0.2(@types/node@18.18.9)(less@4.1.3) + vitest: + specifier: workspace:* + version: link:../../packages/vitest + test/config: devDependencies: vite: diff --git a/test/cli/fixtures/exclude/math.test.ts b/test/cli/fixtures/exclude/math.test.ts new file mode 100644 index 000000000000..597efcfefb99 --- /dev/null +++ b/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) +}) diff --git a/test/cli/fixtures/exclude/math.ts b/test/cli/fixtures/exclude/math.ts new file mode 100644 index 000000000000..a39d4ef196bc --- /dev/null +++ b/test/cli/fixtures/exclude/math.ts @@ -0,0 +1,3 @@ +export function add(a: number, b: number): number { + return a + b +} diff --git a/test/cli/fixtures/exclude/string.test.ts b/test/cli/fixtures/exclude/string.test.ts new file mode 100644 index 000000000000..f45777d7866a --- /dev/null +++ b/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') +}) diff --git a/test/cli/fixtures/exclude/string.ts b/test/cli/fixtures/exclude/string.ts new file mode 100644 index 000000000000..c8f32ebe25b8 --- /dev/null +++ b/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() +} diff --git a/test/cli/fixtures/exclude/vitest.exclude.config.ts b/test/cli/fixtures/exclude/vitest.exclude.config.ts new file mode 100644 index 000000000000..a76ac25d9e36 --- /dev/null +++ b/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'], + }, +}) diff --git a/test/cli/package.json b/test/cli/package.json new file mode 100644 index 000000000000..7ed0d533eebb --- /dev/null +++ b/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:*" + } +} diff --git a/test/cli/test/exclude.test.ts b/test/cli/test/exclude.test.ts new file mode 100644 index 000000000000..8ef73969d360 --- /dev/null +++ b/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('') +}) diff --git a/test/cli/vitest.config.ts b/test/cli/vitest.config.ts new file mode 100644 index 000000000000..d3d5f5dc2452 --- /dev/null +++ b/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, + }, + }, +})