Skip to content

Commit

Permalink
feat: add --outputDiffLines cli flag
Browse files Browse the repository at this point in the history
Use this flag in order to control how many lines are printed,
on each diff.
  • Loading branch information
Dragomir-Ivanov committed Jun 7, 2022
1 parent cf4c982 commit a56b036
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
7 changes: 7 additions & 0 deletions docs/config/index.md
Expand Up @@ -245,6 +245,13 @@ Custom reporters for output. Reporters can be [a Reporter instance](https://gith
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
1 change: 1 addition & 0 deletions docs/guide/cli.md
Expand Up @@ -55,6 +55,7 @@ vitest related /src/index.ts /src/hello-world.js
| `--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
1 change: 1 addition & 0 deletions packages/vitest/src/node/cli.ts
Expand Up @@ -23,6 +23,7 @@ cli
.option('--isolate', 'isolate environment for each test file (default: true)')
.option('--reporter <name>', 'reporter')
.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 a56b036

Please sign in to comment.