Skip to content

Commit

Permalink
fix(browser): support benchmark (#5622)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Apr 29, 2024
1 parent 30e5dc1 commit becabb5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/browser/src/client/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ export async function initiateRunner() {
if (cachedRunner)
return cachedRunner
const config = getConfig()
const [{ VitestTestRunner }, { takeCoverageInsideWorker, loadDiffConfig, loadSnapshotSerializers }] = await Promise.all([
const [{ VitestTestRunner, NodeBenchmarkRunner }, { takeCoverageInsideWorker, loadDiffConfig, loadSnapshotSerializers }] = await Promise.all([
importId('vitest/runners') as Promise<typeof import('vitest/runners')>,
importId('vitest/browser') as Promise<typeof import('vitest/browser')>,
])
const BrowserRunner = createBrowserRunner(VitestTestRunner, {
const runnerClass = config.mode === 'test' ? VitestTestRunner : NodeBenchmarkRunner
const BrowserRunner = createBrowserRunner(runnerClass, {
takeCoverage: () => takeCoverageInsideWorker(config.coverage, { executeId: importId }),
})
if (!config.snapshotOptions.snapshotEnvironment)
Expand Down
3 changes: 1 addition & 2 deletions test/benchmark/fixtures/basic/only.bench.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import assert from 'node:assert'
import { bench, describe, expect } from 'vitest'
import { bench, describe, expect, assert } from 'vitest'

const run = [false, false, false, false, false]

Expand Down
32 changes: 32 additions & 0 deletions test/browser/fixtures/benchmark/basic.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { bench, describe } from 'vitest'

describe('suite-a', () => {
bench('good', async () => {
await sleep(10)
}, options)

bench('bad', async () => {
await sleep(300)
}, options)
})

describe('suite-b', () => {
bench('good', async () => {
await sleep(25)
}, options)

describe('suite-b-nested', () => {
bench('good', async () => {
await sleep(50)
}, options)
})
})

const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

const options = {
time: 0,
iterations: 2,
warmupIterations: 0,
warmupTime: 0,
}
17 changes: 17 additions & 0 deletions test/browser/fixtures/benchmark/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { fileURLToPath } from 'node:url'
import { defineConfig } from 'vitest/config'

const provider = process.env.PROVIDER || 'playwright'
const name =
process.env.BROWSER || (provider === 'playwright' ? 'chromium' : 'chrome')

export default defineConfig({
test: {
browser: {
enabled: true,
provider,
name,
},
},
cacheDir: fileURLToPath(new URL("./node_modules/.vite", import.meta.url)),
})
8 changes: 8 additions & 0 deletions test/browser/specs/benchmark.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'

test('benchmark', async () => {
const result = await runVitest({ root: 'fixtures/benchmark' }, [], 'benchmark')
expect(result.stdout).toContain('✓ basic.bench.ts > suite-a')
expect(result.exitCode).toBe(0)
})

0 comments on commit becabb5

Please sign in to comment.