diff --git a/docs/api/index.md b/docs/api/index.md index 00438ed8fadf..ae8dbb08599a 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -313,6 +313,18 @@ Use `bench.only` to only run certain benchmarks in a given suite. This is useful }) ``` +### bench.todo + +- **Type:** `(name: string) => void` + +Use `bench.todo` to stub benchmarks to be implemented later. + + ```ts + import { bench } from 'vitest' + + bench.todo('unimplemented test') + ``` + ## describe When you use `test` or `bench` in the top level of file, they are collected as part of the implicit suite for it. Using `describe` you can define a new suite in the current context, as a set of related tests or benchmarks and other nested suites. A suite lets you organize your tests and benchmarks so reports are more clear. diff --git a/packages/vitest/src/runtime/suite.ts b/packages/vitest/src/runtime/suite.ts index 21dfc7498564..cd6ef0d475e2 100644 --- a/packages/vitest/src/runtime/suite.ts +++ b/packages/vitest/src/runtime/suite.ts @@ -14,7 +14,7 @@ export const test = createTest( ) export const bench = createBenchmark( - function (name, fn, options) { + function (name, fn: BenchFunction = noop, options: BenchOptions = {}) { getCurrentSuite().benchmark.fn.call(this, name, fn, options) }, ) @@ -115,8 +115,8 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m tasks.push(test) }) - const benchmark = createBenchmark(function (name: string, fn = noop, options: BenchOptions) { - const mode = this.only ? 'only' : this.skip ? 'skip' : 'run' + const benchmark = createBenchmark(function (name: string, fn = noop, options: BenchOptions = {}) { + const mode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run' if (!isRunningInBenchmark()) throw new Error('`bench()` is only available in benchmark mode. Run with `vitest bench` instead.') @@ -251,14 +251,14 @@ function createTest(fn: ( function createBenchmark(fn: ( ( - this: Record<'skip' | 'only', boolean | undefined>, + this: Record<'skip' | 'only' | 'todo', boolean | undefined>, name: string, - fn: BenchFunction, - options: BenchOptions + fn?: BenchFunction, + options?: BenchOptions ) => void )) { const benchmark = createChainable( - ['skip', 'only'], + ['skip', 'only', 'todo'], fn, ) as BenchmarkAPI diff --git a/packages/vitest/src/types/benchmark.ts b/packages/vitest/src/types/benchmark.ts index a661ecd4e79a..153e39ea9da6 100644 --- a/packages/vitest/src/types/benchmark.ts +++ b/packages/vitest/src/types/benchmark.ts @@ -53,8 +53,8 @@ export interface BenchmarkResult extends TinybenchResult { export type BenchFunction = (this: BenchFactory) => Promise | void export type BenchmarkAPI = ChainableFunction< -'skip' | 'only', -[name: string, fn: BenchFunction, options?: BenchOptions], +'skip' | 'only' | 'todo', +[name: string, fn?: BenchFunction, options?: BenchOptions], void > & { skipIf(condition: any): BenchmarkAPI diff --git a/test/benchmark/test.mjs b/test/benchmark/test.mjs index e402b9964998..411a5046920b 100644 --- a/test/benchmark/test.mjs +++ b/test/benchmark/test.mjs @@ -22,6 +22,10 @@ const skippedBenches = ['s0', 's1', 's2', 's3', 'sb4', 's4'] if (skippedBenches.some(b => benchResult.includes(b))) process.exit(1) +const todoBenches = ['unimplemented suite', 'unimplemented test'] +if (todoBenches.some(b => benchResult.includes(b))) + process.exit(1) + if (error) { console.error(error) process.exit(1) diff --git a/test/benchmark/test/mode.bench.ts b/test/benchmark/test/mode.bench.ts index 23ea159e093f..0967332de750 100644 --- a/test/benchmark/test/mode.bench.ts +++ b/test/benchmark/test/mode.bench.ts @@ -1,9 +1,13 @@ import { bench, describe } from 'vitest' +describe.todo('unimplemented suite') + describe.skip('skipped', () => { bench('skipped', () => { throw new Error('should be skipped') }) + + bench.todo('unimplemented test') }) bench.skip('skipped', () => {