Skip to content

Commit 9ae9dac

Browse files
authoredJan 12, 2024
fix(utils): fix objDisplay default truncate option for test.each title (#4917)
1 parent 21513f0 commit 9ae9dac

File tree

4 files changed

+110
-3
lines changed

4 files changed

+110
-3
lines changed
 

‎packages/utils/src/display.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,19 @@ export function format(...args: unknown[]) {
9898
return str
9999
}
100100

101-
export function inspect(obj: unknown, options: LoupeOptions = {}) {
101+
export function inspect(obj: unknown, options: LoupeOptions = {}): string {
102102
if (options.truncate === 0)
103103
options.truncate = Number.POSITIVE_INFINITY
104104
return loupe(obj, options)
105105
}
106106

107107
export function objDisplay(obj: unknown, options: LoupeOptions = {}): string {
108-
const truncateThreshold = options.truncate ?? 40
108+
if (typeof options.truncate === 'undefined')
109+
options.truncate = 40
109110
const str = inspect(obj, options)
110111
const type = Object.prototype.toString.call(obj)
111112

112-
if (truncateThreshold && str.length >= truncateThreshold) {
113+
if (options.truncate && str.length >= options.truncate) {
113114
if (type === '[object Function]') {
114115
const fn = obj as () => void
115116
return (!fn.name || fn.name === '')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { expect, test } from 'vitest'
2+
3+
test.each`
4+
length | param
5+
${30} | ${'0123456789'.repeat(3)}
6+
${40} | ${'0123456789'.repeat(4)}
7+
${50} | ${'0123456789'.repeat(5)}
8+
`('$param (length = $length)', () => {});
9+
10+
test.each`
11+
param
12+
${['one', 'two', 'three']}
13+
${['one', 'two', 'three', 'four']}
14+
${['one', 'two', 'three', 'four', 'five']}
15+
`('$param', () => {});
16+
17+
test.each`
18+
param
19+
${{ one: 1, two: 2, three: 3 }}
20+
${{ one: 1, two: 2, three: 3, four: 4 }}
21+
${{ one: 1, two: 2, three: 3, four: 4, five: 5 }}
22+
`('$param', () => {});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({})

‎test/config/test/chai-config.test.ts

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { runVitest } from '../../test-utils'
3+
4+
describe('truncateThreshold', () => {
5+
it('default', async () => {
6+
const result = await runVitest({
7+
root: 'fixtures/chai-config',
8+
reporters: ['tap-flat'],
9+
})
10+
expect(cleanOutput(result.stdout)).toMatchInlineSnapshot(`
11+
"TAP version 13
12+
1..9
13+
ok 1 - test-each-title.test.ts > '012345678901234567890123456789' (length = 30)
14+
ok 2 - test-each-title.test.ts > '0123456789012345678901234567890123456…' (length = 40)
15+
ok 3 - test-each-title.test.ts > '0123456789012345678901234567890123456…' (length = 50)
16+
ok 4 - test-each-title.test.ts > [ 'one', 'two', 'three' ]
17+
ok 5 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four' ]
18+
ok 6 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four', …(1) ]
19+
ok 7 - test-each-title.test.ts > { one: 1, two: 2, three: 3 }
20+
ok 8 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4 }
21+
ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, …(2) }
22+
"
23+
`)
24+
expect(result.exitCode).toBe(0)
25+
})
26+
27+
it('40', async () => {
28+
const result = await runVitest({
29+
root: 'fixtures/chai-config',
30+
reporters: ['tap-flat'],
31+
chaiConfig: {
32+
truncateThreshold: 40,
33+
},
34+
})
35+
expect(cleanOutput(result.stdout)).toMatchInlineSnapshot(`
36+
"TAP version 13
37+
1..9
38+
ok 1 - test-each-title.test.ts > '012345678901234567890123456789' (length = 30)
39+
ok 2 - test-each-title.test.ts > '0123456789012345678901234567890123456…' (length = 40)
40+
ok 3 - test-each-title.test.ts > '0123456789012345678901234567890123456…' (length = 50)
41+
ok 4 - test-each-title.test.ts > [ 'one', 'two', 'three' ]
42+
ok 5 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four' ]
43+
ok 6 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four', …(1) ]
44+
ok 7 - test-each-title.test.ts > { one: 1, two: 2, three: 3 }
45+
ok 8 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4 }
46+
ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, …(2) }
47+
"
48+
`)
49+
expect(result.exitCode).toBe(0)
50+
})
51+
52+
it('0', async () => {
53+
const result = await runVitest({
54+
root: 'fixtures/chai-config',
55+
reporters: ['tap-flat'],
56+
chaiConfig: {
57+
truncateThreshold: 0,
58+
},
59+
})
60+
expect(cleanOutput(result.stdout)).toMatchInlineSnapshot(`
61+
"TAP version 13
62+
1..9
63+
ok 1 - test-each-title.test.ts > '012345678901234567890123456789' (length = 30)
64+
ok 2 - test-each-title.test.ts > '0123456789012345678901234567890123456789' (length = 40)
65+
ok 3 - test-each-title.test.ts > '01234567890123456789012345678901234567890123456789' (length = 50)
66+
ok 4 - test-each-title.test.ts > [ 'one', 'two', 'three' ]
67+
ok 5 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four' ]
68+
ok 6 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four', 'five' ]
69+
ok 7 - test-each-title.test.ts > { one: 1, two: 2, three: 3 }
70+
ok 8 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4 }
71+
ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4, five: 5 }
72+
"
73+
`)
74+
expect(result.exitCode).toBe(0)
75+
})
76+
})
77+
78+
function cleanOutput(output: string) {
79+
// remove non-deterministic output
80+
return output.replaceAll(/\s*# time=.*/g, '')
81+
}

0 commit comments

Comments
 (0)
Please sign in to comment.