Skip to content

Commit

Permalink
chore: add try/catch to plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jun 14, 2022
1 parent ed16364 commit 631b5a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
16 changes: 3 additions & 13 deletions packages/vitest/src/node/config.ts
Expand Up @@ -56,16 +56,6 @@ export function resolveApiConfig<Options extends ApiConfig & UserConfig>(
return api
}

const configError = (error: string): never => {
console.warn(
c.yellow(
`${c.inverse(c.red(' VITEST '))} ${error}\n`,
),
)

process.exit(1)
}

export function resolveConfig(
options: UserConfig,
viteConfig: ResolvedViteConfig,
Expand Down Expand Up @@ -102,17 +92,17 @@ export function resolveConfig(

if (options.shard) {
if (resolved.watch)
configError('You cannot use --shard option with enabled watch')
throw new Error('You cannot use --shard option with enabled watch')

const [indexString, countString] = options.shard.split('/')
const index = Math.abs(parseInt(indexString, 10))
const count = Math.abs(parseInt(countString, 10))

if (isNaN(count) || count <= 0)
configError('--shard <count> must be a positive number')
throw new Error('--shard <count> must be a positive number')

if (isNaN(index) || index <= 0 || index > count)
configError('--shard <index> must be a positive number less then <count>')
throw new Error('--shard <index> must be a positive number less then <count>')

resolved.shard = { index, count }
}
Expand Down
14 changes: 10 additions & 4 deletions packages/vitest/src/node/plugins/index.ts
Expand Up @@ -125,10 +125,16 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
async configureServer(server) {
if (haveStarted)
await ctx.report('onServerRestart')
await ctx.setServer(options, server)
haveStarted = true
if (options.api && options.watch)
(await import('../../api/setup')).setup(ctx)
try {
await ctx.setServer(options, server)
haveStarted = true
if (options.api && options.watch)
(await import('../../api/setup')).setup(ctx)
}
catch (err) {
ctx.printError(err, true)
process.exit(1)
}

// #415, in run mode we don't need the watcher, close it would improve the performance
if (!options.watch)
Expand Down

0 comments on commit 631b5a9

Please sign in to comment.