Skip to content

Commit

Permalink
feat: --bail option for cancelling test run
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Apr 14, 2023
1 parent ab40293 commit c9773df
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/runner/src/types/runner.ts
Expand Up @@ -27,7 +27,7 @@ export interface VitestRunnerConstructor {
new(config: VitestRunnerConfig): VitestRunner
}

export type CancelReason = 'keyboard-input' | string & {}
export type CancelReason = 'keyboard-input' | 'test-failure' | string & {}

export interface VitestRunner {
/**
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/cli.ts
Expand Up @@ -45,6 +45,7 @@ cli
.option('--inspect', 'Enable Node.js inspector')
.option('--inspect-brk', 'Enable Node.js inspector with break')
.option('--test-timeout <time>', 'Default timeout of a test in milliseconds (default: 5000)')
.option('--bail <number>', 'Stop test execution after specified number of failures', { default: 0 })
.help()

cli
Expand Down
6 changes: 6 additions & 0 deletions packages/vitest/src/node/pools/rpc.ts
Expand Up @@ -58,5 +58,11 @@ export function createMethodsRPC(project: WorkspaceProject): RuntimeRPC {
onFinished(files) {
project.report('onFinished', files, ctx.state.getUnhandledErrors())
},
onCancel(reason) {
ctx.cancelCurrentRun(reason)
},
getCountOfFailedTests() {
return Array.from(ctx.state.idMap.values()).filter(t => t.result?.state === 'fail').length
},
}
}
14 changes: 14 additions & 0 deletions packages/vitest/src/runtime/entry.ts
Expand Up @@ -71,6 +71,20 @@ async function getTestRunner(config: ResolvedConfig, executor: VitestExecutor):
await originalOnAfterRun?.call(testRunner, files)
}

const originalOnAfterRunTest = testRunner.onAfterRunTest
testRunner.onAfterRunTest = async (test) => {
if (config.bail && test.result?.state === 'fail') {
const previousFailures = await rpc().getCountOfFailedTests()
const currentFailures = 1 + previousFailures

if (currentFailures >= config.bail) {
rpc().onCancel('test-failure')
testRunner.onCancel?.('test-failure')
}
}
await originalOnAfterRunTest?.call(testRunner, test)
}

return testRunner
}

Expand Down
5 changes: 5 additions & 0 deletions packages/vitest/src/types/config.ts
Expand Up @@ -567,6 +567,11 @@ export interface InlineConfig {
* https://github.com/chaijs/chai/blob/4.x.x/lib/chai/config.js
*/
chaiConfig?: ChaiConfig

/**
* Stop test execution when given number of tests have failed. Defaults to 1.
*/
bail?: number
}

export interface TypecheckConfig {
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/types/rpc.ts
Expand Up @@ -20,6 +20,8 @@ export interface RuntimeRPC {
onCollected: (files: File[]) => void
onAfterSuiteRun: (meta: AfterSuiteRunMeta) => void
onTaskUpdate: (pack: TaskResultPack[]) => void
onCancel(reason: CancelReason): void
getCountOfFailedTests(): number

snapshotSaved: (snapshot: SnapshotResult) => void
resolveSnapshotPath: (testPath: string) => string
Expand Down

0 comments on commit c9773df

Please sign in to comment.