Skip to content

Commit a749a6c

Browse files
authoredJul 3, 2023
feat: support use function/class as bench name (#3711)
1 parent 1621cc6 commit a749a6c

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed
 

‎docs/api/index.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ You cannot use this syntax, when using Vitest as [type checker](/guide/testing-t
344344

345345
## bench
346346

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

349349
`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.
350350

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

412412
### bench.skip
413413

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

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

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

429429
### bench.only
430430

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

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

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

446446
### bench.todo
447447

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

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

‎packages/vitest/src/runtime/benchmark.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const bench = createBenchmark(
2121
if (!isRunningInBenchmark())
2222
throw new Error('`bench()` is only available in benchmark mode.')
2323

24-
const task = getCurrentSuite().custom.call(this, name)
24+
const task = getCurrentSuite().custom.call(this, formatName(name))
2525
task.meta = {
2626
benchmark: true,
2727
}
@@ -33,7 +33,7 @@ export const bench = createBenchmark(
3333
function createBenchmark(fn: (
3434
(
3535
this: Record<'skip' | 'only' | 'todo', boolean | undefined>,
36-
name: string,
36+
name: string | Function,
3737
fn?: BenchFunction,
3838
options?: BenchOptions
3939
) => void
@@ -48,3 +48,7 @@ function createBenchmark(fn: (
4848

4949
return benchmark as BenchmarkAPI
5050
}
51+
52+
function formatName(name: string | Function) {
53+
return typeof name === 'string' ? name : name instanceof Function ? (name.name || '<anonymous>') : String(name)
54+
}

‎packages/vitest/src/types/benchmark.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export interface BenchmarkResult extends TinybenchResult {
5454
export type BenchFunction = (this: BenchFactory) => Promise<void> | void
5555
export type BenchmarkAPI = ChainableFunction<
5656
'skip' | 'only' | 'todo',
57-
[name: string, fn?: BenchFunction, options?: BenchOptions],
57+
[name: string | Function, fn?: BenchFunction, options?: BenchOptions],
5858
void
5959
> & {
6060
skipIf(condition: any): BenchmarkAPI
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { bench } from 'vitest'
2+
3+
function foo() {}
4+
class Bar {}
5+
6+
bench(foo, () => {})
7+
bench(Bar, () => {})
8+
bench(() => {}, () => {})

‎test/reporters/tests/function-as-name.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@ test('should print function name', async () => {
1414
expect(stdout).toContain('function-as-name.test.ts > foo > foo')
1515
expect(stdout).toContain('function-as-name.test.ts > Bar > Bar')
1616
})
17+
18+
test('should print function name in benchmark', async () => {
19+
const filename = resolve('./fixtures/function-as-name.bench.ts')
20+
const { stdout } = await runVitest({ root: './fixtures' }, [filename], 'benchmark')
21+
22+
expect(stdout).toBeTruthy()
23+
expect(stdout).toContain('Bar')
24+
expect(stdout).toContain('foo')
25+
expect(stdout).toContain('<anonymous>')
26+
})

0 commit comments

Comments
 (0)
Please sign in to comment.