Skip to content

Commit

Permalink
feat: support use function/class as bench name (#3711)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenghan34 authored Jul 3, 2023
1 parent 1621cc6 commit a749a6c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
8 changes: 4 additions & 4 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ You cannot use this syntax, when using Vitest as [type checker](/guide/testing-t

## bench

- **Type:** `(name: string, fn: BenchFunction, options?: BenchOptions) => void`
- **Type:** `(name: string | Function, fn: BenchFunction, options?: BenchOptions) => void`

`bench` defines a benchmark. In Vitest terms benchmark is a function that defines a series of operations. Vitest runs this function multiple times to display different performance results.

Expand Down Expand Up @@ -411,7 +411,7 @@ Vitest uses [`tinybench`](https://github.com/tinylibs/tinybench) library under t

### bench.skip

- **Type:** `(name: string, fn: BenchFunction, options?: BenchOptions) => void`
- **Type:** `(name: string | Function, fn: BenchFunction, options?: BenchOptions) => void`

You can use `bench.skip` syntax to skip running certain benchmarks.

Expand All @@ -428,7 +428,7 @@ You can use `bench.skip` syntax to skip running certain benchmarks.

### bench.only

- **Type:** `(name: string, fn: BenchFunction, options?: BenchOptions) => void`
- **Type:** `(name: string | Function, fn: BenchFunction, options?: BenchOptions) => void`

Use `bench.only` to only run certain benchmarks in a given suite. This is useful when debugging.

Expand All @@ -445,7 +445,7 @@ Use `bench.only` to only run certain benchmarks in a given suite. This is useful

### bench.todo

- **Type:** `(name: string) => void`
- **Type:** `(name: string | Function) => void`

Use `bench.todo` to stub benchmarks to be implemented later.

Expand Down
8 changes: 6 additions & 2 deletions packages/vitest/src/runtime/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const bench = createBenchmark(
if (!isRunningInBenchmark())
throw new Error('`bench()` is only available in benchmark mode.')

const task = getCurrentSuite().custom.call(this, name)
const task = getCurrentSuite().custom.call(this, formatName(name))
task.meta = {
benchmark: true,
}
Expand All @@ -33,7 +33,7 @@ export const bench = createBenchmark(
function createBenchmark(fn: (
(
this: Record<'skip' | 'only' | 'todo', boolean | undefined>,
name: string,
name: string | Function,
fn?: BenchFunction,
options?: BenchOptions
) => void
Expand All @@ -48,3 +48,7 @@ function createBenchmark(fn: (

return benchmark as BenchmarkAPI
}

function formatName(name: string | Function) {
return typeof name === 'string' ? name : name instanceof Function ? (name.name || '<anonymous>') : String(name)
}
2 changes: 1 addition & 1 deletion packages/vitest/src/types/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface BenchmarkResult extends TinybenchResult {
export type BenchFunction = (this: BenchFactory) => Promise<void> | void
export type BenchmarkAPI = ChainableFunction<
'skip' | 'only' | 'todo',
[name: string, fn?: BenchFunction, options?: BenchOptions],
[name: string | Function, fn?: BenchFunction, options?: BenchOptions],
void
> & {
skipIf(condition: any): BenchmarkAPI
Expand Down
8 changes: 8 additions & 0 deletions test/reporters/fixtures/function-as-name.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { bench } from 'vitest'

function foo() {}
class Bar {}

bench(foo, () => {})
bench(Bar, () => {})
bench(() => {}, () => {})
10 changes: 10 additions & 0 deletions test/reporters/tests/function-as-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ test('should print function name', async () => {
expect(stdout).toContain('function-as-name.test.ts > foo > foo')
expect(stdout).toContain('function-as-name.test.ts > Bar > Bar')
})

test('should print function name in benchmark', async () => {
const filename = resolve('./fixtures/function-as-name.bench.ts')
const { stdout } = await runVitest({ root: './fixtures' }, [filename], 'benchmark')

expect(stdout).toBeTruthy()
expect(stdout).toContain('Bar')
expect(stdout).toContain('foo')
expect(stdout).toContain('<anonymous>')
})

0 comments on commit a749a6c

Please sign in to comment.