Skip to content

Commit

Permalink
feat: extend config.outputFile to allow objects, close #1068 (#1073)
Browse files Browse the repository at this point in the history
Co-authored-by: steve <steve.scheder@avenga.com>
  • Loading branch information
steve-py96 and steve committed Apr 7, 2022
1 parent 45ef020 commit a63cfa2
Show file tree
Hide file tree
Showing 9 changed files with 609 additions and 8 deletions.
3 changes: 2 additions & 1 deletion docs/config/index.md
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
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 @@ -163,3 +163,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
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
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
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
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
@@ -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]
}

0 comments on commit a63cfa2

Please sign in to comment.