Skip to content

Commit

Permalink
feat: new --ouputDiffLines cli flag (#1446)
Browse files Browse the repository at this point in the history
* docs: add documentation for `--outputTruncateLength` cli flag

* feat: add `--outputDiffLines` cli flag

Use this flag in order to control how many lines are printed,
on each diff.
  • Loading branch information
Dragomir-Ivanov committed Jun 8, 2022
1 parent 60eaee0 commit 0b8a5bc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
15 changes: 15 additions & 0 deletions docs/config/index.md
Expand Up @@ -237,6 +237,21 @@ Custom reporters for output. Reporters can be [a Reporter instance](https://gith
- `'json'` - give a simple JSON summary
- path of a custom reporter (e.g. `'./path/to/reporter.ts'`, `'@scope/reporter'`)

### outputTruncateLength

- **Type:** `number`
- **Default:** `80`

Truncate output diff lines up to `80` number of characters. You may wish to tune this,
depending on you terminal window width.

### outputDiffLines

- **Type:** `number`
- **Default:** `15`

Limit number of output diff lines up to `15`.

### outputFile

- **Type:** `string | Record<string, string>`
Expand Down
2 changes: 2 additions & 0 deletions docs/guide/cli.md
Expand Up @@ -54,6 +54,8 @@ vitest related /src/index.ts /src/hello-world.js
| `--silent` | Silent console output from tests |
| `--isolate` | Isolate environment for each test file (default: `true`) |
| `--reporter <name>` | Select reporter: `default`, `verbose`, `dot`, `junit`, `json`, or a path to a custom reporter |
| `--outputTruncateLength <length>` | Truncate output diff lines up to `<length>` number of characters. |
| `--outputDiffLines <lines>` | Limit number of output diff lines up to `<lines>`. |
| `--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 |
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/node/cli.ts
Expand Up @@ -22,7 +22,8 @@ cli
.option('--silent', 'silent console output from tests')
.option('--isolate', 'isolate environment for each test file (default: true)')
.option('--reporter <name>', 'reporter')
.option('--outputTruncateLength <length>', 'diff output length')
.option('--outputTruncateLength <length>', 'diff output length (default: 80)')
.option('--outputDiffLines <lines>', 'number of diff output lines (default: 15)')
.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')
Expand Down
5 changes: 3 additions & 2 deletions packages/vitest/src/node/diff.ts
Expand Up @@ -8,6 +8,7 @@ export function formatLine(line: string, outputTruncateLength?: number) {

export interface DiffOptions {
outputTruncateLength?: number
outputDiffLines?: number
showLegend?: boolean
}

Expand All @@ -24,10 +25,10 @@ export function unifiedDiff(actual: string, expected: string, options: DiffOptio
if (actual === expected)
return ''

const { outputTruncateLength, showLegend = true } = options
const { outputTruncateLength, outputDiffLines, showLegend = true } = options

const indent = ' '
const diffLimit = 15
const diffLimit = outputDiffLines || 15

const counts = {
'+': 0,
Expand Down
14 changes: 9 additions & 5 deletions packages/vitest/src/node/error.ts
Expand Up @@ -8,7 +8,7 @@ import { interpretSourcePos, lineSplitRE, parseStacktrace, posToNumber } from '.
import { F_POINTER } from '../utils/figures'
import { stringify } from '../integrations/chai/jest-matcher-utils'
import type { Vitest } from './core'
import { unifiedDiff } from './diff'
import { type DiffOptions, unifiedDiff } from './diff'
import { divider } from './reporters/renderers/utils'

export function fileFromParsedStack(stack: ParsedStack) {
Expand Down Expand Up @@ -62,8 +62,12 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro

handleImportOutsideModuleError(e.stack || e.stackStr || '', ctx)

if (e.showDiff)
displayDiff(stringify(e.actual), stringify(e.expected), ctx.console, ctx.config.outputTruncateLength)
if (e.showDiff) {
displayDiff(stringify(e.actual), stringify(e.expected), ctx.console, {
outputTruncateLength: ctx.config.outputTruncateLength,
outputDiffLines: ctx.config.outputDiffLines,
})
}
}

function printErrorType(type: string, ctx: Vitest) {
Expand Down Expand Up @@ -135,8 +139,8 @@ function handleImportOutsideModuleError(stack: string, ctx: Vitest) {
}\n`)))
}

function displayDiff(actual: string, expected: string, console: Console, outputTruncateLength?: number) {
console.error(c.gray(unifiedDiff(actual, expected, { outputTruncateLength })) + '\n')
function displayDiff(actual: string, expected: string, console: Console, options?: Omit<DiffOptions, 'showLegend'>) {
console.error(c.gray(unifiedDiff(actual, expected, options)) + '\n')
}

function printErrorMessage(error: ErrorWithDiff, console: Console) {
Expand Down
5 changes: 5 additions & 0 deletions packages/vitest/src/types/config.ts
Expand Up @@ -141,6 +141,11 @@ export interface InlineConfig {
*/
outputTruncateLength?: number

/**
* number of diff output lines
*/
outputDiffLines?: number

/**
* 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.
Expand Down

0 comments on commit 0b8a5bc

Please sign in to comment.