diff --git a/docs/config/index.md b/docs/config/index.md index 8951feafdc7b..43be6387a4f8 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -1375,3 +1375,11 @@ Influences whether or not the `showDiff` flag should be included in the thrown A Sets length threshold for actual and expected values in assertion errors. If this threshold is exceeded, for example for large data structures, the value is replaced with something like `[ Array(3) ]` or `{ Object (prop1, prop2) }`. Set it to `0` if you want to disable truncating altogether. This config option affects truncating values in `test.each` titles and inside the assertion error message. + +### bail + +- **Type:** `number` +- **Default:** `0` +- **CLI**: `--bail=` + +Stop test execution when given number of tests have failed. diff --git a/package.json b/package.json index 713ed041f43a..2c23bf7c34d0 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "test:run": "vitest run -r test/core", "test:all": "CI=true pnpm -r --stream run test --allowOnly", "test:ci": "CI=true pnpm -r --stream --filter !test-fails --filter !test-browser --filter !test-esm --filter !test-browser run test --allowOnly", - "test:ci:single-thread": "CI=true pnpm -r --stream --filter !test-fails --filter !test-coverage --filter !test-watch --filter !test-esm --filter !test-browser run test --allowOnly --no-threads", + "test:ci:single-thread": "CI=true pnpm -r --stream --filter !test-fails --filter !test-coverage --filter !test-watch --filter !test-bail --filter !test-esm --filter !test-browser run test --allowOnly --no-threads", "typecheck": "tsc --noEmit", "typecheck:why": "tsc --noEmit --explainFiles > explainTypes.txt", "ui:build": "vite build packages/ui", diff --git a/packages/browser/src/client/runner.ts b/packages/browser/src/client/runner.ts index 3bdc40c1f0d2..d73c2ccc6ea3 100644 --- a/packages/browser/src/client/runner.ts +++ b/packages/browser/src/client/runner.ts @@ -23,10 +23,20 @@ export function createBrowserRunner(original: any, coverageModule: CoverageHandl } async onAfterRunTest(task: Test) { - await super.onAfterRunTest?.() + await super.onAfterRunTest?.(task) task.result?.errors?.forEach((error) => { console.error(error.message) }) + + if (this.config.bail && task.result?.state === 'fail') { + const previousFailures = await rpc().getCountOfFailedTests() + const currentFailures = 1 + previousFailures + + if (currentFailures >= this.config.bail) { + rpc().onCancel('test-failure') + this.onCancel?.('test-failure') + } + } } async onAfterRunSuite() { diff --git a/packages/runner/src/types/runner.ts b/packages/runner/src/types/runner.ts index e74b93dcbb36..694d6c70045d 100644 --- a/packages/runner/src/types/runner.ts +++ b/packages/runner/src/types/runner.ts @@ -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 { /** diff --git a/packages/vitest/src/api/setup.ts b/packages/vitest/src/api/setup.ts index 6fe9999ca4cb..545ff2cf00bc 100644 --- a/packages/vitest/src/api/setup.ts +++ b/packages/vitest/src/api/setup.ts @@ -111,6 +111,12 @@ export function setup(vitestOrWorkspace: Vitest | WorkspaceProject, server?: Vit return ctx.updateSnapshot() return ctx.updateSnapshot([file.filepath]) }, + onCancel(reason) { + ctx.cancelCurrentRun(reason) + }, + getCountOfFailedTests() { + return ctx.state.getCountOfFailedTests() + }, }, { post: msg => ws.send(msg), diff --git a/packages/vitest/src/api/types.ts b/packages/vitest/src/api/types.ts index e3b0dcac024f..3f96018b2d82 100644 --- a/packages/vitest/src/api/types.ts +++ b/packages/vitest/src/api/types.ts @@ -11,6 +11,8 @@ export interface WebSocketHandlers { onTaskUpdate(packs: TaskResultPack[]): void onAfterSuiteRun(meta: AfterSuiteRunMeta): void onDone(name: string): void + onCancel(reason: CancelReason): void + getCountOfFailedTests(): number sendLog(log: UserConsoleLog): void getFiles(): File[] getPaths(): string[] diff --git a/packages/vitest/src/node/cli.ts b/packages/vitest/src/node/cli.ts index 617b20494379..f9599ad667fe 100644 --- a/packages/vitest/src/node/cli.ts +++ b/packages/vitest/src/node/cli.ts @@ -45,6 +45,7 @@ cli .option('--inspect', 'Enable Node.js inspector') .option('--inspect-brk', 'Enable Node.js inspector with break') .option('--test-timeout