Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(benchmark): todo mode #2057

Merged
merged 2 commits into from Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/api/index.md
Expand Up @@ -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.
Expand Down
14 changes: 7 additions & 7 deletions packages/vitest/src/runtime/suite.ts
Expand Up @@ -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)
},
)
Expand Down Expand Up @@ -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.')
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/types/benchmark.ts
Expand Up @@ -53,8 +53,8 @@ export interface BenchmarkResult extends TinybenchResult {

export type BenchFunction = (this: BenchFactory) => Promise<void> | 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
Expand Down
4 changes: 4 additions & 0 deletions test/benchmark/test.mjs
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions 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', () => {
Expand Down