From 0b8a5bc0d671021db57c5275d3b46fd94522e6c5 Mon Sep 17 00:00:00 2001 From: Dragomir Ivanov Date: Wed, 8 Jun 2022 18:41:01 +0300 Subject: [PATCH] feat: new `--ouputDiffLines` cli flag (#1446) * 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. --- docs/config/index.md | 15 +++++++++++++++ docs/guide/cli.md | 2 ++ packages/vitest/src/node/cli.ts | 3 ++- packages/vitest/src/node/diff.ts | 5 +++-- packages/vitest/src/node/error.ts | 14 +++++++++----- packages/vitest/src/types/config.ts | 5 +++++ 6 files changed, 36 insertions(+), 8 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index 26a4604eb6c8..f9b7b0deafe4 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -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` diff --git a/docs/guide/cli.md b/docs/guide/cli.md index 57aea9b0c516..2ff00fd5fa41 100644 --- a/docs/guide/cli.md +++ b/docs/guide/cli.md @@ -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 ` | Select reporter: `default`, `verbose`, `dot`, `junit`, `json`, or a path to a custom reporter | +| `--outputTruncateLength ` | Truncate output diff lines up to `` number of characters. | +| `--outputDiffLines ` | Limit number of output diff lines up to ``. | | `--outputFile ` | Write test results to a file when the `--reporter=json` or `--reporter=junit` option is also specified
Via [cac's dot notation] you can specify individual outputs for multiple reporters | | `--coverage` | Use c8 for coverage | | `--run` | Do not watch | diff --git a/packages/vitest/src/node/cli.ts b/packages/vitest/src/node/cli.ts index 7c9901723209..d258d43ed042 100644 --- a/packages/vitest/src/node/cli.ts +++ b/packages/vitest/src/node/cli.ts @@ -22,7 +22,8 @@ cli .option('--silent', 'silent console output from tests') .option('--isolate', 'isolate environment for each test file (default: true)') .option('--reporter ', 'reporter') - .option('--outputTruncateLength ', 'diff output length') + .option('--outputTruncateLength ', 'diff output length (default: 80)') + .option('--outputDiffLines ', 'number of diff output lines (default: 15)') .option('--outputFile ', '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') diff --git a/packages/vitest/src/node/diff.ts b/packages/vitest/src/node/diff.ts index 6a7c74bd5d6d..aba19864e5c0 100644 --- a/packages/vitest/src/node/diff.ts +++ b/packages/vitest/src/node/diff.ts @@ -8,6 +8,7 @@ export function formatLine(line: string, outputTruncateLength?: number) { export interface DiffOptions { outputTruncateLength?: number + outputDiffLines?: number showLegend?: boolean } @@ -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, diff --git a/packages/vitest/src/node/error.ts b/packages/vitest/src/node/error.ts index 795e6da4fbc8..f92d23e1c1d0 100644 --- a/packages/vitest/src/node/error.ts +++ b/packages/vitest/src/node/error.ts @@ -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) { @@ -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) { @@ -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) { + console.error(c.gray(unifiedDiff(actual, expected, options)) + '\n') } function printErrorMessage(error: ErrorWithDiff, console: Console) { diff --git a/packages/vitest/src/types/config.ts b/packages/vitest/src/types/config.ts index 474c72582b24..1c133f281283 100644 --- a/packages/vitest/src/types/config.ts +++ b/packages/vitest/src/types/config.ts @@ -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.