Skip to content

Commit 4f6c134

Browse files
committedApr 11, 2023
fix: test.each respects chaiConfig
1 parent 06852f1 commit 4f6c134

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed
 

‎packages/runner/src/suite.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ function formatTitle(template: string, items: any[], idx: number) {
248248
let formatted = format(template, ...items.slice(0, count))
249249
if (isObject(items[0])) {
250250
formatted = formatted.replace(/\$([$\w_.]+)/g,
251-
(_, key) => objDisplay(objectAttr(items[0], key)) as unknown as string,
251+
(_, key) => objDisplay(objectAttr(items[0], key), runner?.config?.chaiConfig) as unknown as string,
252252
// https://github.com/chaijs/chai/pull/1490
253253
)
254254
}

‎packages/runner/src/types/runner.ts

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export interface VitestRunnerConfig {
1313
hooks: SequenceHooks
1414
setupFiles: SequenceSetupFiles
1515
}
16+
chaiConfig?: {
17+
truncateThreshold?: number
18+
}
1619
maxConcurrency: number
1720
testTimeout: number
1821
hookTimeout: number

‎packages/utils/src/display.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import util from 'util'
33
// @ts-expect-error doesn't have types
44
import loupeImport from 'loupe'
55

6+
interface LoupeOptions {
7+
truncateThreshold?: number
8+
}
9+
610
const loupe = (typeof loupeImport.default === 'function' ? loupeImport.default : loupeImport)
711

812
export function format(...args: any[]) {
@@ -14,19 +18,21 @@ export function utilInspect(item: unknown, options?: util.InspectOptions) {
1418
}
1519

1620
// chai utils
17-
export function loupeInspect(obj: unknown): string {
21+
export function loupeInspect(obj: unknown, options: LoupeOptions = {}): string {
1822
return loupe(obj, {
1923
depth: 2,
20-
truncate: 40,
24+
truncate: options.truncateThreshold === 0
25+
? Infinity
26+
: (options.truncateThreshold ?? 40),
2127
})
2228
}
2329

24-
export function objDisplay(obj: unknown) {
25-
const truncateThreshold = 40
26-
const str = loupeInspect(obj)
30+
export function objDisplay(obj: unknown, options: LoupeOptions = {}): string {
31+
const truncateThreshold = options.truncateThreshold ?? 40
32+
const str = loupeInspect(obj, options)
2733
const type = Object.prototype.toString.call(obj)
2834

29-
if (str.length >= truncateThreshold) {
35+
if (truncateThreshold && str.length >= truncateThreshold) {
3036
if (type === '[object Function]') {
3137
const fn = obj as () => void
3238
return (!fn.name || fn.name === '')

0 commit comments

Comments
 (0)
Please sign in to comment.