Skip to content

Commit

Permalink
feat: outputTruncateLength option (#977)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aslemammad committed Mar 19, 2022
1 parent 2dfcdf9 commit d3b4639
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/vitest/src/node/cli.ts
Expand Up @@ -22,6 +22,7 @@ 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('--outputFile <filename>', 'write test results to a file when the --reporter=json option is also specified')
.option('--coverage', 'use c8 for coverage')
.option('--run', 'do not watch')
Expand Down
14 changes: 7 additions & 7 deletions packages/vitest/src/node/diff.ts
Expand Up @@ -2,8 +2,8 @@ import c from 'picocolors'
import * as diff from 'diff'
import cliTruncate from 'cli-truncate'

export function formatLine(line: string) {
return cliTruncate(line, (process.stdout.columns || 80) - 4)
export function formatLine(line: string, outputTruncateLength?: number) {
return cliTruncate(line, (outputTruncateLength ?? (process.stdout.columns || 80)) - 4)
}

/**
Expand All @@ -15,7 +15,7 @@ export function formatLine(line: string) {
* @return {string} The diff.
*/

export function unifiedDiff(actual: string, expected: string) {
export function unifiedDiff(actual: string, expected: string, outputTruncateLength?: number) {
if (actual === expected)
return ''

Expand Down Expand Up @@ -54,16 +54,16 @@ export function unifiedDiff(actual: string, expected: string) {

let formatted = lines.map((line: string) => {
if (line[0] === '-') {
line = formatLine(line.slice(1))
line = formatLine(line.slice(1), outputTruncateLength)
if (isCompact)
return c.green(line)
return c.green(`- ${formatLine(line)}`)
return c.green(`- ${formatLine(line, outputTruncateLength)}`)
}
if (line[0] === '+') {
line = formatLine(line.slice(1))
line = formatLine(line.slice(1), outputTruncateLength)
if (isCompact)
return c.red(line)
return c.red(`+ ${formatLine(line)}`)
return c.red(`+ ${formatLine(line, outputTruncateLength)}`)
}
if (line.match(/@@/))
return '--'
Expand Down
6 changes: 3 additions & 3 deletions packages/vitest/src/node/error.ts
Expand Up @@ -40,7 +40,7 @@ export async function printError(error: unknown, ctx: Vitest) {
handleImportOutsideModuleError(e.stack || e.stackStr || '', ctx)

if (e.showDiff)
displayDiff(e.actual, e.expected, ctx.console)
displayDiff(e.actual, e.expected, ctx.console, ctx.config.outputTruncateLength)
}

const esmErrors = [
Expand Down Expand Up @@ -79,8 +79,8 @@ function handleImportOutsideModuleError(stack: string, ctx: Vitest) {
}\n`)))
}

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

function printErrorMessage(error: ErrorWithDiff, console: Console) {
Expand Down
5 changes: 5 additions & 0 deletions packages/vitest/src/types/config.ts
Expand Up @@ -137,6 +137,11 @@ export interface InlineConfig {
*/
reporters?: Arrayable<BuiltinReporters | Reporter>

/**
* diff output length
*/
outputTruncateLength?: number

/**
* Write test results to a file when the --reporter=json option is also specified
*/
Expand Down

0 comments on commit d3b4639

Please sign in to comment.