Skip to content

Commit

Permalink
test: add test for --typecheck filter (#5216)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Feb 16, 2024
1 parent 86297d4 commit 6588922
Showing 1 changed file with 47 additions and 40 deletions.
87 changes: 47 additions & 40 deletions test/core/test/cli-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -14,33 +14,34 @@ 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,
})
})

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,
})
})

test('nested coverage options have correct types', async () => {
expect(parseArguments(`
expect(getCLIOptions(`
--coverage.all
--coverage.enabled=true
--coverage.clean false
Expand Down Expand Up @@ -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
Expand All @@ -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 <name>", 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": [
Expand All @@ -152,7 +153,7 @@ test('array options', () => {
}
`)

expect(parseArguments(`
expect(getCLIOptions(`
--reporter json
--reporter=default
--coverage.reporter=json
Expand Down Expand Up @@ -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' } })
Expand Down

0 comments on commit 6588922

Please sign in to comment.