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

fix: move outputFile to benchmark, allow --reporter for benchmark #2037

Merged
merged 2 commits into from Sep 14, 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
9 changes: 9 additions & 0 deletions docs/config/index.md
Expand Up @@ -168,6 +168,15 @@ When defined, Vitest will run all matched files with `import.meta.vitest` inside

Custom reporter for output. Can contain one or more built-in report names, reporter instances, and/or paths to custom reporters.

### benchmark.outputFile

- **Type:** `string | Record<string, string>`

Write benchmark results to a file when the `--reporter=json` option is also specified.
By providing an object instead of a string you can define individual outputs when using multiple reporters.

To provide object via CLI command, use the following syntax: `--outputFile.json=./path --outputFile.junit=./other-path`.

### alias

- **Type:** `Record<string, string> | Array<{ find: string | RegExp, replacement: string, customResolver?: ResolverFunction | ResolverObject }>`
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/defaults.ts
Expand Up @@ -3,7 +3,7 @@ import type { BenchmarkUserOptions, ResolvedCoverageOptions, UserConfig } from '
export const defaultInclude = ['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}']
export const defaultExclude = ['**/node_modules/**', '**/dist/**', '**/cypress/**', '**/.{idea,git,cache,output,temp}/**']

export const benchmarkConfigDefaults: Required<BenchmarkUserOptions> = {
export const benchmarkConfigDefaults: Required<Omit<BenchmarkUserOptions, 'outputFile'>> = {
include: ['**/*.{bench,benchmark}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
exclude: defaultExclude,
includeSource: [],
Expand Down
25 changes: 20 additions & 5 deletions packages/vitest/src/node/config.ts
Expand Up @@ -10,6 +10,7 @@ import { toArray } from '../utils'
import { VitestCache } from './cache'
import { BaseSequencer } from './sequencers/BaseSequencer'
import { RandomSequencer } from './sequencers/RandomSequencer'
import type { BenchmarkBuiltinReporters } from './reporters'

const extraInlineDeps = [
/^(?!.*(?:node_modules)).*\.mjs$/,
Expand Down Expand Up @@ -169,6 +170,18 @@ export function resolveConfig(
resolved.include = resolved.benchmark.include
resolved.exclude = resolved.benchmark.exclude
resolved.includeSource = resolved.benchmark.includeSource
const reporters = Array.from(new Set<BenchmarkBuiltinReporters>([
...toArray(resolved.benchmark.reporters),
// @ts-expect-error reporter is CLI flag
...toArray(options.reporter),
])).filter(Boolean)
if (reporters.length)
resolved.benchmark.reporters = reporters
else
resolved.benchmark.reporters = ['default']

if (options.outputFile)
resolved.benchmark.outputFile = options.outputFile
}

resolved.setupFiles = toArray(resolved.setupFiles || []).map(file =>
Expand All @@ -184,11 +197,13 @@ export function resolveConfig(
if (options.related)
resolved.related = toArray(options.related).map(file => resolve(resolved.root, file))

resolved.reporters = Array.from(new Set([
...toArray(resolved.reporters),
// @ts-expect-error from CLI
...toArray(resolved.reporter),
])).filter(Boolean)
if (mode !== 'benchmark') {
resolved.reporters = Array.from(new Set([
...toArray(resolved.reporters),
// @ts-expect-error from CLI
...toArray(resolved.reporter),
])).filter(Boolean)
}

if (!resolved.reporters.length)
resolved.reporters.push('default')
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/reporters/benchmark/json.ts
Expand Up @@ -25,7 +25,7 @@ export class JsonReporter implements Reporter {
const tests = getTests(files)
const numTotalTests = tests.length
const testResults: Record<string, BenchTaskResult[]> = {}
const outputFile = getOutputFile(this.ctx, 'json')
const outputFile = getOutputFile(this.ctx.config.benchmark, 'json')
for (const file of files) {
const tests = getTests([file])
for (const test of tests) {
Expand Down Expand Up @@ -63,7 +63,7 @@ export class JsonReporter implements Reporter {
* @param report
*/
async writeReport(report: string) {
const outputFile = getOutputFile(this.ctx, 'json')
const outputFile = getOutputFile(this.ctx.config.benchmark, 'json')

if (outputFile) {
const reportFile = resolve(this.ctx.config.root, outputFile)
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/reporters/json.ts
Expand Up @@ -163,7 +163,7 @@ export class JsonReporter implements Reporter {
* @param report
*/
async writeReport(report: string) {
const outputFile = getOutputFile(this.ctx, 'json')
const outputFile = getOutputFile(this.ctx.config, 'json')

if (outputFile) {
const reportFile = resolve(this.ctx.config.root, outputFile)
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/reporters/junit.ts
Expand Up @@ -72,7 +72,7 @@ export class JUnitReporter implements Reporter {
async onInit(ctx: Vitest): Promise<void> {
this.ctx = ctx

const outputFile = getOutputFile(this.ctx, 'junit')
const outputFile = getOutputFile(this.ctx.config, 'junit')

if (outputFile) {
this.reportFile = resolve(this.ctx.config.root, outputFile)
Expand Down
6 changes: 6 additions & 0 deletions packages/vitest/src/types/benchmark.ts
Expand Up @@ -29,6 +29,12 @@ export interface BenchmarkUserOptions {
* and/or paths to custom reporters
*/
reporters?: Arrayable<BenchmarkBuiltinReporters | Reporter>

/**
* Write test results to a file when the `--reporter=json` option is also specified.
* Also definable individually per reporter by using an object instead.
*/
outputFile?: string | (Partial<Record<BenchmarkBuiltinReporters, string>> & Record<string, string>)
}

export interface Benchmark extends TaskBase {
Expand Down
4 changes: 3 additions & 1 deletion packages/vitest/src/types/config.ts
Expand Up @@ -508,7 +508,9 @@ export interface ResolvedConfig extends Omit<Required<UserConfig>, 'config' | 'f

api?: ApiConfig

benchmark?: Required<BenchmarkUserOptions>
benchmark?: Required<Omit<BenchmarkUserOptions, 'outputFile'>> & {
outputFile?: BenchmarkUserOptions['outputFile']
}

shard?: {
index: number
Expand Down
9 changes: 6 additions & 3 deletions packages/vitest/src/utils/config-helpers.ts
@@ -1,8 +1,11 @@
import type { Vitest } from '../node/core'
import type { BenchmarkBuiltinReporters, BuiltinReporters } from '../node/reporters'

export const getOutputFile = ({ config }: Vitest, reporter: BuiltinReporters | BenchmarkBuiltinReporters) => {
if (!config.outputFile)
interface PotentialConfig {
outputFile?: string | Partial<Record<string, string>>
}

export const getOutputFile = (config: PotentialConfig | undefined, reporter: BuiltinReporters | BenchmarkBuiltinReporters) => {
if (!config?.outputFile)
return

if (typeof config.outputFile === 'string')
Expand Down
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion test/benchmark/package.json
Expand Up @@ -3,7 +3,10 @@
"private": true,
"scripts": {
"test": "node test.mjs",
"testu": "vitest -u",
"bench": "vitest bench --reporter=json",
"coverage": "vitest run --coverage"
},
"devDependencies": {
"execa": "^6.1.0"
}
}
24 changes: 16 additions & 8 deletions test/benchmark/test.mjs
@@ -1,18 +1,26 @@
import { readFile } from 'fs/promises'
import { startVitest } from 'vitest/node'
import { execa } from 'execa'

const success = await startVitest('benchmark', ['base.bench', 'mode.bench'], {
run: true,
update: false,
outputFile: './bench.json', // TODO move outputFile to benchmark
benchmark: {
reporters: ['json'],
let error
await execa('npx', ['vitest', 'bench', 'base.bench', 'mode.bench'], {
env: {
...process.env,
CI: 'true',
NO_COLOR: 'true',
},
})
.catch((e) => {
error = e
})

const benchResult = await readFile('./bench.json', 'utf-8')

if (benchResult.includes('skip'))
process.exit(1)

process.exit(success ? 0 : 1)
if (error) {
console.error(error)
process.exit(1)
}

process.exit(0)
4 changes: 3 additions & 1 deletion test/benchmark/vitest.config.ts
Expand Up @@ -2,8 +2,10 @@ import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
update: false,
benchmark: {
reporters: 'json',
outputFile: './bench.json',
reporters: ['json'],
},
},
})