From a7931bbf76b80343799046f69b7b5e36c0f0a95b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Sat, 9 Dec 2023 18:58:58 +0200 Subject: [PATCH] fix(cli): `--coverage.all=false` resolved incorrectly (#4697) --- packages/vitest/src/node/cli.ts | 1 + test/config/fixtures/test/log-output.test.ts | 19 +++++++++++++++ test/config/test/failures.test.ts | 2 +- test/config/test/options.test.ts | 25 ++++++++++++++++++++ test/config/vitest.config.ts | 3 +++ 5 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 test/config/fixtures/test/log-output.test.ts create mode 100644 test/config/test/options.test.ts diff --git a/packages/vitest/src/node/cli.ts b/packages/vitest/src/node/cli.ts index aa8d728108a4..598fc07f53ed 100644 --- a/packages/vitest/src/node/cli.ts +++ b/packages/vitest/src/node/cli.ts @@ -26,6 +26,7 @@ cli .option('--reporter ', 'Specify reporters') .option('--outputFile ', 'Write test results to a file when supporter reporter is also specified, use cac\'s dot notation for individual outputs of multiple reporters') .option('--coverage', 'Enable coverage report') + .option('--coverage.all', 'Whether to include all files, including the untested ones into report', { default: true }) .option('--run', 'Disable watch mode') .option('--mode ', 'Override Vite mode (default: test)') .option('--globals', 'Inject apis globally') diff --git a/test/config/fixtures/test/log-output.test.ts b/test/config/fixtures/test/log-output.test.ts new file mode 100644 index 000000000000..ffbe181a385a --- /dev/null +++ b/test/config/fixtures/test/log-output.test.ts @@ -0,0 +1,19 @@ +import { test } from 'vitest' +import type { UserConfig } from 'vitest/config' + +/* eslint-disable no-console, unused-imports/no-unused-vars */ + +test('logs resolved configuration', async () => { + // @ts-expect-error -- internal + const { snapshotOptions, ...config }: UserConfig['test'] = globalThis.__vitest_worker__.config + + // Log options that are tested + log('coverage.enabled', config.coverage?.enabled) + + if(config.coverage?.provider === "istanbul" || config.coverage?.provider === "v8") + log('coverage.all', config.coverage?.all) +}) + +function log(label: string, value: unknown) { + console.log(label, value, typeof value) +} diff --git a/test/config/test/failures.test.ts b/test/config/test/failures.test.ts index 31cb4194ba8b..ee57b0b168df 100644 --- a/test/config/test/failures.test.ts +++ b/test/config/test/failures.test.ts @@ -92,7 +92,7 @@ test('coverage.autoUpdate cannot update thresholds when configuration file doesn }, }) - expect(stderr).toMatch('Error: Unable to parse thresholds from configuration file: Cannot read properties of undefined') + expect(stderr).toMatch('Error: Unable to parse thresholds from configuration file: Expected config.test.coverage.thresholds to be an object') }) test('boolean flag 100 should not crash CLI', async () => { diff --git a/test/config/test/options.test.ts b/test/config/test/options.test.ts new file mode 100644 index 000000000000..ba5c608a778b --- /dev/null +++ b/test/config/test/options.test.ts @@ -0,0 +1,25 @@ +import { expect, test } from 'vitest' + +import * as testUtils from '../../test-utils' + +function runVitestCli(...cliArgs: string[]) { + return testUtils.runVitestCli('--root', 'fixtures', 'run', 'test/log-output.test.ts', ...cliArgs) +} + +test('--coverage', async () => { + const { stdout } = await runVitestCli('--coverage') + + expect(stdout).toMatch('coverage.enabled true boolean') +}) + +test('--coverage.all=false', async () => { + const { stdout } = await runVitestCli('--coverage.enabled', '--coverage.all=false') + + expect(stdout).toMatch('coverage.all false boolean') +}) + +test('--coverage.all', async () => { + const { stdout } = await runVitestCli('--coverage.enabled', '--coverage.all') + + expect(stdout).toMatch('coverage.all true boolean') +}) diff --git a/test/config/vitest.config.ts b/test/config/vitest.config.ts index 7161494d71ba..3cf9037293b6 100644 --- a/test/config/vitest.config.ts +++ b/test/config/vitest.config.ts @@ -9,5 +9,8 @@ export default defineConfig({ chaiConfig: { truncateThreshold: 999, }, + coverage: { + reporter: [], + }, }, })