Skip to content

Commit

Permalink
fix: don't fail bench when using options (#2295)
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimir <sheremet.va@icloud.com>
  • Loading branch information
poyoho and sheremet-va committed Nov 8, 2022
1 parent c386bce commit 1703f7c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
11 changes: 10 additions & 1 deletion packages/vitest/src/runtime/map.ts
@@ -1,8 +1,9 @@
import type { Awaitable, BenchFunction, Benchmark, Suite, SuiteHooks, Test } from '../types'
import type { Awaitable, BenchFunction, BenchOptions, Benchmark, Suite, SuiteHooks, Test } from '../types'

// use WeakMap here to make the Test and Suite object serializable
const fnMap = new WeakMap()
const hooksMap = new WeakMap()
const benchOptsMap = new WeakMap()

export function setFn(key: Test | Benchmark, fn: (() => Awaitable<void>) | BenchFunction) {
fnMap.set(key, fn)
Expand All @@ -23,3 +24,11 @@ export function getHooks(key: Suite): SuiteHooks {
export function isTest(task: Test | Benchmark): task is Test {
return task.type === 'test'
}

export function setBenchOptions(key: Benchmark, val: BenchOptions) {
benchOptsMap.set(key, val)
}

export function getBenchOptions(key: Benchmark): BenchOptions {
return benchOptsMap.get(key)
}
5 changes: 3 additions & 2 deletions packages/vitest/src/runtime/run.ts
Expand Up @@ -6,7 +6,7 @@ import { clearTimeout, createDefer, getFullName, getWorkerState, hasFailed, hasT
import { getState, setState } from '../integrations/chai/jest-expect'
import { GLOBAL_EXPECT } from '../integrations/chai/constants'
import { takeCoverageInsideWorker } from '../integrations/coverage'
import { getFn, getHooks } from './map'
import { getBenchOptions, getFn, getHooks } from './map'
import { rpc } from './rpc'
import { collectTests } from './collect'
import { processError } from './error'
Expand Down Expand Up @@ -366,7 +366,8 @@ async function runBenchmarkSuite(suite: Suite) {
}
updateTask(suite)
benchmarkGroup.forEach((benchmark, idx) => {
const benchmarkInstance = new Bench(benchmark.options)
const options = getBenchOptions(benchmark)
const benchmarkInstance = new Bench(options)

const benchmarkFn = getFn(benchmark)

Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/suite.ts
Expand Up @@ -3,7 +3,7 @@ import type { BenchFunction, BenchOptions, Benchmark, BenchmarkAPI, File, RunMod
import { getWorkerState, isObject, isRunningInBenchmark, isRunningInTest, noop } from '../utils'
import { createChainable } from './chain'
import { collectTask, collectorContext, createTestContext, runWithSuite, withTimeout } from './context'
import { getHooks, setFn, setHooks } from './map'
import { getHooks, setBenchOptions, setFn, setHooks } from './map'

// apis
export const suite = createSuite()
Expand Down Expand Up @@ -106,11 +106,11 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
id: '',
name,
mode,
options,
suite: undefined!,
}

setFn(benchmark, fn)
setBenchOptions(benchmark, options)
tasks.push(benchmark)
})

Expand Down
1 change: 0 additions & 1 deletion packages/vitest/src/types/benchmark.ts
Expand Up @@ -43,7 +43,6 @@ export interface Benchmark extends TaskBase {
result?: TaskResult
fails?: boolean
task?: BenchTask
options: BenchOptions
}

export interface BenchmarkResult extends TinybenchResult {
Expand Down
7 changes: 7 additions & 0 deletions test/benchmark/test/base.bench.ts
Expand Up @@ -31,6 +31,13 @@ function timeout(time: number) {
describe('timeout', () => {
bench('timeout100', async () => {
await timeout(100)
}, {
setup() {

},
teardown() {

},
})

bench('timeout75', async () => {
Expand Down

0 comments on commit 1703f7c

Please sign in to comment.