Skip to content

Commit f01c783

Browse files
authoredMar 10, 2023
fix: config errors not visible (#2995)
1 parent c44e5af commit f01c783

File tree

7 files changed

+71
-1
lines changed

7 files changed

+71
-1
lines changed
 

‎packages/vitest/src/node/error.ts

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
3838
} as any
3939
}
4040

41+
// Error may have occured even before the configuration was resolved
42+
if (!ctx.config)
43+
return printErrorMessage(e, ctx.logger)
44+
4145
const stacks = parseErrorStacktrace(e, fullStack)
4246

4347
const nearest = error instanceof TypeCheckError

‎packages/vitest/src/node/plugins/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t
233233
(await import('../../api/setup')).setup(ctx)
234234
}
235235
catch (err) {
236-
ctx.logger.printError(err, true)
236+
await ctx.logger.printError(err, true)
237237
process.exit(1)
238238
}
239239

‎pnpm-lock.yaml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, test } from 'vitest'
2+
3+
test('it works', () => {
4+
expect(true).toBe(true)
5+
})

‎test/config/package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "@vitest/test-config",
3+
"private": true,
4+
"scripts": {
5+
"test": "vitest run test/**"
6+
},
7+
"devDependencies": {
8+
"execa": "^7.0.0",
9+
"strip-ansi": "^7.0.1",
10+
"vite": "latest",
11+
"vitest": "workspace:*"
12+
}
13+
}

‎test/config/test/failures.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { expect, test } from 'vitest'
2+
3+
import { runVitest } from './utils'
4+
5+
test('shard cannot be used with watch mode', async () => {
6+
const { error } = await runVitest('watch', ['--shard', '1/2'])
7+
8+
expect(error).toMatch('Error: You cannot use --shard option with enabled watch')
9+
})
10+
11+
test('shard must be positive number', async () => {
12+
const { error } = await runVitest('run', ['--shard', '"-1"'])
13+
14+
expect(error).toMatch('Error: --shard <count> must be a positive number')
15+
})
16+
17+
test('shard index must be smaller than count', async () => {
18+
const { error } = await runVitest('run', ['--shard', '2/1'])
19+
20+
expect(error).toMatch('Error: --shard <index> must be a positive number less then <count>')
21+
})

‎test/config/test/utils.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { execa } from 'execa'
2+
import stripAnsi from 'strip-ansi'
3+
4+
export async function runVitest(mode: 'run' | 'watch', cliArguments: string[]) {
5+
const subprocess = execa('vitest', [mode, 'fixtures/test/', ...cliArguments])
6+
let error = ''
7+
8+
subprocess.stderr?.on('data', (data) => {
9+
error += stripAnsi(data.toString())
10+
})
11+
12+
await new Promise(resolve => subprocess.on('exit', resolve))
13+
14+
return { error }
15+
}

0 commit comments

Comments
 (0)
Please sign in to comment.