diff --git a/test/core/test/cli-test.test.ts b/test/core/test/cli-test.test.ts index 90612b9e651a..4bc5b3b7a69a 100644 --- a/test/core/test/cli-test.test.ts +++ b/test/core/test/cli-test.test.ts @@ -3,7 +3,7 @@ import { createCLI } from '../../../packages/vitest/src/node/cli/cac.js' const vitestCli = createCLI() -function parseArguments(commands: string, full = false, includeArgs = false) { +function parseArguments(commands: string, full = false) { const cliArgs = commands.trim().replace(/\s+/g, ' ').split(' ') const { options, args } = vitestCli.parse(['node', '/index.js', ...cliArgs], { run: false, @@ -14,17 +14,18 @@ function parseArguments(commands: string, full = false, includeArgs = false) { delete options.color } - if (includeArgs) - return { options, args } + return { options, args } +} - return options +function getCLIOptions(commands: string) { + return parseArguments(commands).options } const enabled = { enabled: true } const disabled = { enabled: false } test('top level nested options return boolean', async () => { - expect(parseArguments('--coverage --browser --typecheck')).toEqual({ + expect(getCLIOptions('--coverage --browser --typecheck')).toEqual({ coverage: enabled, browser: enabled, typecheck: enabled, @@ -32,7 +33,7 @@ test('top level nested options return boolean', async () => { }) test('negated top level nested options return boolean', async () => { - expect(parseArguments('--no-coverage --no-browser --no-typecheck')).toEqual({ + expect(getCLIOptions('--no-coverage --no-browser --no-typecheck')).toEqual({ coverage: disabled, browser: disabled, typecheck: disabled, @@ -40,7 +41,7 @@ test('negated top level nested options return boolean', async () => { }) test('nested coverage options have correct types', async () => { - expect(parseArguments(` + expect(getCLIOptions(` --coverage.all --coverage.enabled=true --coverage.clean false @@ -104,7 +105,7 @@ test('nested coverage options have correct types', async () => { }) test('correctly normalizes methods to be an array', async () => { - expect(parseArguments(` + expect(getCLIOptions(` --coverage.ignoreClassMethods method2 --coverage.include pattern --coverage.exclude pattern @@ -118,25 +119,25 @@ test('correctly normalizes methods to be an array', async () => { }) test('all coverage enable options are working correctly', () => { - expect(parseArguments('--coverage').coverage).toEqual({ enabled: true }) - expect(parseArguments('--coverage.enabled --coverage.all=false').coverage).toEqual({ enabled: true, all: false }) - expect(parseArguments('--coverage.enabled --coverage.all').coverage).toEqual({ enabled: true, all: true }) + expect(getCLIOptions('--coverage').coverage).toEqual({ enabled: true }) + expect(getCLIOptions('--coverage.enabled --coverage.all=false').coverage).toEqual({ enabled: true, all: false }) + expect(getCLIOptions('--coverage.enabled --coverage.all').coverage).toEqual({ enabled: true, all: true }) }) test('fails when an array is passed down for a single value', async () => { - expect(() => parseArguments('--coverage.provider v8 --coverage.provider istanbul')) + expect(() => getCLIOptions('--coverage.provider v8 --coverage.provider istanbul')) .toThrowErrorMatchingInlineSnapshot(`[Error: Expected a single value for option "--coverage.provider ", received ["v8", "istanbul"]]`) }) test('even if coverage is boolean, don\'t fail', () => { - expect(parseArguments('--coverage --coverage.provider v8').coverage).toEqual({ + expect(getCLIOptions('--coverage --coverage.provider v8').coverage).toEqual({ enabled: true, provider: 'v8', }) }) test('array options', () => { - expect(parseArguments('--reporter json --coverage.reporter=html --coverage.extension ts')).toMatchInlineSnapshot(` + expect(getCLIOptions('--reporter json --coverage.reporter=html --coverage.extension ts')).toMatchInlineSnapshot(` { "coverage": { "extension": [ @@ -152,7 +153,7 @@ test('array options', () => { } `) - expect(parseArguments(` + expect(getCLIOptions(` --reporter json --reporter=default --coverage.reporter=json @@ -180,68 +181,74 @@ test('array options', () => { }) test('hookTimeout is parsed correctly', () => { - expect(parseArguments('--hookTimeout 1000')).toEqual({ hookTimeout: 1000 }) - expect(parseArguments('--hook-timeout 1000')).toEqual({ hookTimeout: 1000 }) - expect(parseArguments('--hook-timeout=1000')).toEqual({ hookTimeout: 1000 }) - expect(parseArguments('--hookTimeout=1000')).toEqual({ hookTimeout: 1000 }) + expect(getCLIOptions('--hookTimeout 1000')).toEqual({ hookTimeout: 1000 }) + expect(getCLIOptions('--hook-timeout 1000')).toEqual({ hookTimeout: 1000 }) + expect(getCLIOptions('--hook-timeout=1000')).toEqual({ hookTimeout: 1000 }) + expect(getCLIOptions('--hookTimeout=1000')).toEqual({ hookTimeout: 1000 }) }) test('teardownTimeout is parsed correctly', () => { - expect(parseArguments('--teardownTimeout 1000')).toEqual({ teardownTimeout: 1000 }) - expect(parseArguments('--teardown-timeout 1000')).toEqual({ teardownTimeout: 1000 }) - expect(parseArguments('--teardownTimeout=1000')).toEqual({ teardownTimeout: 1000 }) - expect(parseArguments('--teardown-timeout=1000')).toEqual({ teardownTimeout: 1000 }) + expect(getCLIOptions('--teardownTimeout 1000')).toEqual({ teardownTimeout: 1000 }) + expect(getCLIOptions('--teardown-timeout 1000')).toEqual({ teardownTimeout: 1000 }) + expect(getCLIOptions('--teardownTimeout=1000')).toEqual({ teardownTimeout: 1000 }) + expect(getCLIOptions('--teardown-timeout=1000')).toEqual({ teardownTimeout: 1000 }) }) test('slowTestThreshold is parsed correctly', () => { - expect(parseArguments('--slowTestThreshold 1000')).toEqual({ slowTestThreshold: 1000 }) - expect(parseArguments('--slow-test-threshold 1000')).toEqual({ slowTestThreshold: 1000 }) - expect(parseArguments('--slowTestThreshold=1000')).toEqual({ slowTestThreshold: 1000 }) - expect(parseArguments('--slow-test-threshold=1000')).toEqual({ slowTestThreshold: 1000 }) + expect(getCLIOptions('--slowTestThreshold 1000')).toEqual({ slowTestThreshold: 1000 }) + expect(getCLIOptions('--slow-test-threshold 1000')).toEqual({ slowTestThreshold: 1000 }) + expect(getCLIOptions('--slowTestThreshold=1000')).toEqual({ slowTestThreshold: 1000 }) + expect(getCLIOptions('--slow-test-threshold=1000')).toEqual({ slowTestThreshold: 1000 }) }) test('maxConcurrency is parsed correctly', () => { - expect(parseArguments('--maxConcurrency 1000')).toEqual({ maxConcurrency: 1000 }) - expect(parseArguments('--max-concurrency 1000')).toEqual({ maxConcurrency: 1000 }) - expect(parseArguments('--maxConcurrency=1000')).toEqual({ maxConcurrency: 1000 }) - expect(parseArguments('--max-concurrency=1000')).toEqual({ maxConcurrency: 1000 }) + expect(getCLIOptions('--maxConcurrency 1000')).toEqual({ maxConcurrency: 1000 }) + expect(getCLIOptions('--max-concurrency 1000')).toEqual({ maxConcurrency: 1000 }) + expect(getCLIOptions('--maxConcurrency=1000')).toEqual({ maxConcurrency: 1000 }) + expect(getCLIOptions('--max-concurrency=1000')).toEqual({ maxConcurrency: 1000 }) }) test('cache is parsed correctly', () => { - expect(parseArguments('--cache')).toEqual({ cache: {} }) - expect(parseArguments('--no-cache')).toEqual({ cache: false }) + expect(getCLIOptions('--cache')).toEqual({ cache: {} }) + expect(getCLIOptions('--no-cache')).toEqual({ cache: false }) - expect(parseArguments('--cache.dir=./test/cache.json')).toEqual({ + expect(getCLIOptions('--cache.dir=./test/cache.json')).toEqual({ cache: { dir: 'test/cache.json' }, }) - expect(parseArguments('--cache.dir ./test/cache.json')).toEqual({ + expect(getCLIOptions('--cache.dir ./test/cache.json')).toEqual({ cache: { dir: 'test/cache.json' }, }) - expect(parseArguments('--cache.dir .\\test\\cache.json')).toEqual({ + expect(getCLIOptions('--cache.dir .\\test\\cache.json')).toEqual({ cache: { dir: 'test/cache.json' }, }) }) +test('typecheck correctly passes down arguments', () => { + const { options, args } = parseArguments('--typecheck some.name.ts') + expect(options).toEqual({ typecheck: { enabled: true } }) + expect(args).toEqual(['some.name.ts']) +}) + test('browser as implicit boolean', () => { - const { options, args } = parseArguments('--browser', false, true) + const { options, args } = parseArguments('--browser', false) expect(options).toEqual({ browser: { enabled: true } }) expect(args).toEqual([]) }) test('browser as explicit boolean', () => { - const { options, args } = parseArguments('--browser=true', false, true) + const { options, args } = parseArguments('--browser=true', false) expect(options).toEqual({ browser: { enabled: true } }) expect(args).toEqual([]) }) test('browser as explicit boolean with space', () => { - const { options, args } = parseArguments('--browser true', false, true) + const { options, args } = parseArguments('--browser true', false) expect(options).toEqual({ browser: { enabled: true } }) expect(args).toEqual([]) }) test('browser by name', () => { - const { options, args } = parseArguments('--browser=firefox', false, true) + const { options, args } = parseArguments('--browser=firefox', false) expect(args).toEqual([]) expect(options).toEqual({ browser: { enabled: true, name: 'firefox' } })