Skip to content

Commit

Permalink
fix: config errors not visible (#2995)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Mar 10, 2023
1 parent c44e5af commit f01c783
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/vitest/src/node/error.ts
Expand Up @@ -38,6 +38,10 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
} as any
}

// Error may have occured even before the configuration was resolved
if (!ctx.config)
return printErrorMessage(e, ctx.logger)

const stacks = parseErrorStacktrace(e, fullStack)

const nearest = error instanceof TypeCheckError
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/plugins/index.ts
Expand Up @@ -233,7 +233,7 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t
(await import('../../api/setup')).setup(ctx)
}
catch (err) {
ctx.logger.printError(err, true)
await ctx.logger.printError(err, true)
process.exit(1)
}

Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/config/fixtures/test/example.test.ts
@@ -0,0 +1,5 @@
import { expect, test } from 'vitest'

test('it works', () => {
expect(true).toBe(true)
})
13 changes: 13 additions & 0 deletions test/config/package.json
@@ -0,0 +1,13 @@
{
"name": "@vitest/test-config",
"private": true,
"scripts": {
"test": "vitest run test/**"
},
"devDependencies": {
"execa": "^7.0.0",
"strip-ansi": "^7.0.1",
"vite": "latest",
"vitest": "workspace:*"
}
}
21 changes: 21 additions & 0 deletions test/config/test/failures.test.ts
@@ -0,0 +1,21 @@
import { expect, test } from 'vitest'

import { runVitest } from './utils'

test('shard cannot be used with watch mode', async () => {
const { error } = await runVitest('watch', ['--shard', '1/2'])

expect(error).toMatch('Error: You cannot use --shard option with enabled watch')
})

test('shard must be positive number', async () => {
const { error } = await runVitest('run', ['--shard', '"-1"'])

expect(error).toMatch('Error: --shard <count> must be a positive number')
})

test('shard index must be smaller than count', async () => {
const { error } = await runVitest('run', ['--shard', '2/1'])

expect(error).toMatch('Error: --shard <index> must be a positive number less then <count>')
})
15 changes: 15 additions & 0 deletions test/config/test/utils.ts
@@ -0,0 +1,15 @@
import { execa } from 'execa'
import stripAnsi from 'strip-ansi'

export async function runVitest(mode: 'run' | 'watch', cliArguments: string[]) {
const subprocess = execa('vitest', [mode, 'fixtures/test/', ...cliArguments])
let error = ''

subprocess.stderr?.on('data', (data) => {
error += stripAnsi(data.toString())
})

await new Promise(resolve => subprocess.on('exit', resolve))

return { error }
}

0 comments on commit f01c783

Please sign in to comment.