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

feat: extend config.outputFile to allow objects, close #1068 #1073

Merged
merged 5 commits into from
Apr 7, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,10 @@ Custom reporters for output. Reporters can be [a Reporter instance](https://gith

### outputFile

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

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

### threads

Expand Down
4 changes: 3 additions & 1 deletion docs/guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ You can specify additional CLI options like `--port` or `--https`. For a full li
| `--silent` | Silent console output from tests |
| `--isolate` | Isolate environment for each test file (default: `true`) |
| `--reporter <name>` | Select reporter: `default`, `verbose`, `dot`, `junit` or `json` |
| `--outputFile <filename>` | Write test results to a file when the `--reporter=json` or `--reporter=junit` option is also specified |
| `--outputFile <filename/-s>` | Write test results to a file when the `--reporter=json` or `--reporter=junit` option is also specified <br /> Via [cac's dot notation] you can specify individual outputs for multiple reporters |
| `--coverage` | Use c8 for coverage |
| `--run` | Do not watch |
| `--mode` | Override Vite mode (default: `test`) |
Expand Down Expand Up @@ -162,3 +162,5 @@ Then go to the project where you are using Vitest and run `pnpm link --global vi
## Community

If you have questions or need help, reach out to the community at [Discord](https://chat.vitest.dev) and [GitHub Discussions](https://github.com/vitest-dev/vitest/discussions).

[cac's dot notation]: https://github.com/cacjs/cac#dot-nested-options
2 changes: 1 addition & 1 deletion packages/vitest/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cli
.option('--isolate', 'isolate environment for each test file (default: true)')
.option('--reporter <name>', 'reporter')
.option('--outputTruncateLength <length>', 'diff output length')
.option('--outputFile <filename>', 'write test results to a file when the --reporter=json or --reporter=junit option is also specified')
.option('--outputFile <filename/-s>', 'write test results to a file when the --reporter=json or --reporter=junit option is also specified, use cac\'s dot notation for individual outputs of mutliple reporters')
.option('--coverage', 'use c8 for coverage')
.option('--run', 'do not watch')
.option('--mode <name>', 'override Vite mode (default: test)')
Expand Down
7 changes: 5 additions & 2 deletions packages/vitest/src/node/reporters/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { dirname, resolve } from 'pathe'
import type { Vitest } from '../../node'
import type { File, Reporter, Suite, TaskState } from '../../types'
import { getSuites, getTests } from '../../utils'
import { getOutputFile } from '../../utils/config-helpers'

// for compatibility reasons, the reporter produces a JSON similar to the one produced by the Jest JSON reporter
// the following types are extracted from the Jest repository (and simplified)
Expand Down Expand Up @@ -159,8 +160,10 @@ export class JsonReporter implements Reporter {
* @param report
*/
async writeReport(report: string) {
if (this.ctx.config.outputFile) {
const reportFile = resolve(this.ctx.config.root, this.ctx.config.outputFile)
const outputFile = getOutputFile(this.ctx, 'json')

if (outputFile) {
const reportFile = resolve(this.ctx.config.root, outputFile)

const outputDirectory = dirname(reportFile)
if (!existsSync(outputDirectory))
Expand Down
7 changes: 5 additions & 2 deletions packages/vitest/src/node/reporters/junit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Vitest } from '../../node'
import type { ErrorWithDiff, Reporter, Task } from '../../types'
import { parseStacktrace } from '../../utils/source-map'
import { F_POINTER } from '../../utils/figures'
import { getOutputFile } from '../../utils/config-helpers'
import { IndentedLogger } from './renderers/indented-logger'

function flattenTasks(task: Task, baseName = ''): Task[] {
Expand Down Expand Up @@ -44,8 +45,10 @@ export class JUnitReporter implements Reporter {
async onInit(ctx: Vitest): Promise<void> {
this.ctx = ctx

if (this.ctx.config.outputFile) {
this.reportFile = resolve(this.ctx.config.root, this.ctx.config.outputFile)
const outputFile = getOutputFile(this.ctx, 'junit')

if (outputFile) {
this.reportFile = resolve(this.ctx.config.root, outputFile)

const outputDirectory = dirname(this.reportFile)
if (!existsSync(outputDirectory))
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ export interface InlineConfig {

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

/**
* Enable multi-threading
Expand Down
12 changes: 12 additions & 0 deletions packages/vitest/src/utils/config-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Vitest } from '../node/core'
import type { BuiltinReporters } from '../node/reporters'

export const getOutputFile = ({ config }: Vitest, reporter: BuiltinReporters) => {
if (!config.outputFile)
return

if (typeof config.outputFile === 'string')
return config.outputFile

return config.outputFile[reporter]
}
Loading