Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type {
TestFunction,
TestOptions,
} from './types/tasks'
import { format, formatRegExp, inspect } from '@vitest/utils/display'
import { format, formatRegExp, inspect, truncateString } from '@vitest/utils/display'
import {
isNegativeNaN,
isObject,
Expand Down Expand Up @@ -1020,9 +1020,9 @@ function formatTitle(template: string, items: any[], idx: number) {
})
}

const inspectOptions: InspectOptions = {
const inspectOptions = {
truncate: runner.config.taskTitleValueFormatTruncate,
}
} satisfies InspectOptions

const isObjectItem = isObject(items[0])
function formatAttribute(s: string) {
Expand All @@ -1033,6 +1033,10 @@ function formatTitle(template: string, items: any[], idx: number) {
}
const arrayElement = isArrayKey ? objectAttr(items, key) : undefined
const value = isObjectItem ? objectAttr(items[0], key, arrayElement) : arrayElement
// print string without quotes
if (typeof value === 'string') {
return truncateString(value, inspectOptions.truncate)
}
return inspect(value, inspectOptions)
})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/runner/src/types/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface VitestRunnerConfig {
chaiConfig: {
truncateThreshold?: number
} | undefined
taskTitleValueFormatTruncate: number | undefined
taskTitleValueFormatTruncate: number
maxConcurrency: number
testTimeout: number
hookTimeout: number
Expand Down
11 changes: 11 additions & 0 deletions packages/utils/src/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ export function inspect(
})
}

export function truncateString(string: string, maxLength: number): string {
if (string.length <= maxLength) {
return string
}
let end = maxLength - 1
if (isHighSurrogate(string[end - 1])) {
end = end - 1
}
return `${string.slice(0, end)}…`
}

function stringifyByMaxWidth(object: unknown, threshold: number, options: StringifyOptions): string {
function evaluate(x: number) {
return stringify(object, undefined, {
Expand Down
9 changes: 2 additions & 7 deletions packages/vitest/src/node/reporters/renderers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { SnapshotSummary } from '@vitest/snapshot'
import type { Formatter } from 'tinyrainbow'
import type { TestProject } from '../../project'
import { stripVTControlCharacters } from 'node:util'
import { truncateString as utilsTruncateString } from '@vitest/utils/display'
import { slash } from '@vitest/utils/helpers'
import { basename, dirname, isAbsolute, relative } from 'pathe'
import c from 'tinyrainbow'
Expand Down Expand Up @@ -278,13 +279,7 @@ export function padSummaryTitle(str: string): string {
}

export function truncateString(text: string, maxLength: number): string {
const plainText = stripVTControlCharacters(text)

if (plainText.length <= maxLength) {
return text
}

return `${plainText.slice(0, maxLength - 1)}…`
return utilsTruncateString(stripVTControlCharacters(text), maxLength)
}

function capitalize<T extends string>(text: T) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export interface SerializedConfig {
showDiff?: boolean
truncateThreshold?: number
} | undefined
taskTitleValueFormatTruncate: number | undefined
taskTitleValueFormatTruncate: number
api: {
allowExec: boolean | undefined
allowWrite: boolean | undefined
Expand Down
48 changes: 24 additions & 24 deletions test/cli/test/reporters/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,30 +240,30 @@ describe('default reporter', async () => {

expect(trimReporterOutput(stdout)).toMatchInlineSnapshot(`
"✓ fixtures/reporters/test-for-title.test.ts (24 tests) [...]ms
✓ test.for object : 0 = 'a', 2 = { te: 'st' } [...]ms
✓ test.for object : 0 = 'b', 2 = [ 'test' ] [...]ms
✓ test.each object : 0 = 'a', 2 = { te: 'st' } [...]ms
✓ test.each object : 0 = 'b', 2 = [ 'test' ] [...]ms
✓ test.for array : 0 = 'a', 2 = { te: 'st' } [...]ms
✓ test.for array : 0 = 'b', 2 = [ 'test' ] [...]ms
✓ test.each array : 0 = 'a', 2 = { te: 'st' } [...]ms
✓ test.each array : 0 = 'b', 2 = [ 'test' ] [...]ms
✓ test.for object : 0 = a, 2 = { te: 'st' } [...]ms
✓ test.for object : 0 = b, 2 = [ 'test' ] [...]ms
✓ test.each object : 0 = a, 2 = { te: 'st' } [...]ms
✓ test.each object : 0 = b, 2 = [ 'test' ] [...]ms
✓ test.for array : 0 = a, 2 = { te: 'st' } [...]ms
✓ test.for array : 0 = b, 2 = [ 'test' ] [...]ms
✓ test.each array : 0 = a, 2 = { te: 'st' } [...]ms
✓ test.each array : 0 = b, 2 = [ 'test' ] [...]ms
✓ object : add(1, 1) -> 2 [...]ms
✓ object : add(1, 2) -> 3 [...]ms
✓ object : add(2, 1) -> 3 [...]ms
✓ array : add(1, 1) -> 2 [...]ms
✓ array : add(1, 2) -> 3 [...]ms
✓ array : add(2, 1) -> 3 [...]ms
✓ first array element is object: 0 = { k1: 'v1' }, 1 = { k2: 'v2' }, k1 = 'v1', k2 = undefined [...]ms
✓ first array element is not object: 0 = 'foo', 1 = 'bar', k = $k [...]ms
✓ not array: 0 = { k: 'v1' }, 1 = undefined, k = 'v1' [...]ms
✓ not array: 0 = { k: 'v2' }, 1 = undefined, k = 'v2' [...]ms
✓ first array element is object: 0 = { k1: 'v1' }, 1 = { k2: 'v2' }, k1 = v1, k2 = undefined [...]ms
✓ first array element is not object: 0 = foo, 1 = bar, k = $k [...]ms
✓ not array: 0 = { k: 'v1' }, 1 = undefined, k = v1 [...]ms
✓ not array: 0 = { k: 'v2' }, 1 = undefined, k = v2 [...]ms
✓ handles whole numbers: 343434 as $343,434.00 [...]ms
✓ { a: '$b', b: 'yay' } [...]ms
'%o' [...]ms
%o [...]ms
✓ { a: '%o' } [...]ms
'%o' { a: '%o' } [...]ms
✓ { a: '%o' } '%o' [...]ms"
%o { a: '%o' } [...]ms
✓ { a: '%o' } %o [...]ms"
`)
})

Expand All @@ -282,9 +282,9 @@ describe('default reporter', async () => {
"$ (object: 3) { one: 1, two: 2, three: 3 }": "passed",
"$ (object: 4) { one: 1, two: 2, three: 3, four: 4 }": "passed",
"$ (object: 5) { one: 1, two: 2, three: 3, …(2) }": "passed",
"$ (string: 30) '012345678901234567890123456789'": "passed",
"$ (string: 40) '01234567890123456789012345678901234567…'": "passed",
"$ (string: 50) '01234567890123456789012345678901234567…'": "passed",
"$ (string: 30) 012345678901234567890123456789": "passed",
"$ (string: 40) 0123456789012345678901234567890123456789": "passed",
"$ (string: 50) 012345678901234567890123456789012345678…": "passed",
"% (array: 3) [ 'one', 'two', 'three' ]": "passed",
"% (array: 4) [ 'one', 'two', 'three', 'four' ]": "passed",
"% (array: 5) [ 'one', 'two', 'three', 'four', …(1) ]": "passed",
Expand Down Expand Up @@ -313,9 +313,9 @@ describe('default reporter', async () => {
"$ (object: 3) { one: 1, …(2) }": "passed",
"$ (object: 4) { one: 1, …(3) }": "passed",
"$ (object: 5) { one: 1, …(4) }": "passed",
"$ (string: 30) '012345678901234567…'": "passed",
"$ (string: 40) '012345678901234567…'": "passed",
"$ (string: 50) '012345678901234567…'": "passed",
"$ (string: 30) 0123456789012345678…": "passed",
"$ (string: 40) 0123456789012345678…": "passed",
"$ (string: 50) 0123456789012345678…": "passed",
"% (array: 3) [ 'one', …(2) ]": "passed",
"% (array: 4) [ 'one', …(3) ]": "passed",
"% (array: 5) [ 'one', …(4) ]": "passed",
Expand Down Expand Up @@ -344,9 +344,9 @@ describe('default reporter', async () => {
"$ (object: 3) { one: 1, two: 2, three: 3 }": "passed",
"$ (object: 4) { one: 1, two: 2, three: 3, four: 4 }": "passed",
"$ (object: 5) { one: 1, two: 2, three: 3, four: 4, five: 5 }": "passed",
"$ (string: 30) '012345678901234567890123456789'": "passed",
"$ (string: 40) '0123456789012345678901234567890123456789'": "passed",
"$ (string: 50) '01234567890123456789012345678901234567890123456789'": "passed",
"$ (string: 30) 01234567890123456789012345678…": "passed",
"$ (string: 40) 012345678901234567890123456789012345678…": "passed",
"$ (string: 50) 0123456789012345678901234567890123456789012345678…": "passed",
"% (array: 3) [ 'one', 'two', 'three' ]": "passed",
"% (array: 4) [ 'one', 'two', 'three', 'four' ]": "passed",
"% (array: 5) [ 'one', 'two', 'three', 'four', 'five' ]": "passed",
Expand Down
4 changes: 2 additions & 2 deletions test/core/test/__snapshots__/test-for-suite.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exports[`basic case2 > test 1`] = `
}
`;

exports[`template 'x' true > test 1`] = `
exports[`template x true > test 1`] = `
{
"args": [
{
Expand All @@ -33,7 +33,7 @@ exports[`template 'x' true > test 1`] = `
}
`;

exports[`template 'y' false > test 1`] = `
exports[`template y false > test 1`] = `
{
"args": [
{
Expand Down
12 changes: 6 additions & 6 deletions test/core/test/__snapshots__/test-for.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ exports[`fixture case2 1`] = `
}
`;

exports[`object 'case1' 1`] = `
exports[`object case1 1`] = `
{
"args": {
"k": "case1",
Expand All @@ -105,7 +105,7 @@ exports[`object 'case1' 1`] = `
}
`;

exports[`object 'case2' 1`] = `
exports[`object case2 1`] = `
{
"args": {
"k": "case2",
Expand All @@ -114,21 +114,21 @@ exports[`object 'case2' 1`] = `
}
`;

exports[`object destructure 'case1' 1`] = `
exports[`object destructure case1 1`] = `
{
"myFixture": 1234,
"v": "case1",
}
`;

exports[`object destructure 'case2' 1`] = `
exports[`object destructure case2 1`] = `
{
"myFixture": 1234,
"v": "case2",
}
`;

exports[`template 'x' true 1`] = `
exports[`template x true 1`] = `
{
"args": {
"a": "x",
Expand All @@ -138,7 +138,7 @@ exports[`template 'x' true 1`] = `
}
`;

exports[`template 'y' false 1`] = `
exports[`template y false 1`] = `
{
"args": {
"a": "y",
Expand Down
Loading