From 4f6c13401383d487eca030106ad4cc24d202e912 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 11 Apr 2023 13:06:00 +0200 Subject: [PATCH] fix: test.each respects chaiConfig --- packages/runner/src/suite.ts | 2 +- packages/runner/src/types/runner.ts | 3 +++ packages/utils/src/display.ts | 18 ++++++++++++------ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/runner/src/suite.ts b/packages/runner/src/suite.ts index 11a25d13c7f9..45cb3b6991fe 100644 --- a/packages/runner/src/suite.ts +++ b/packages/runner/src/suite.ts @@ -248,7 +248,7 @@ function formatTitle(template: string, items: any[], idx: number) { let formatted = format(template, ...items.slice(0, count)) if (isObject(items[0])) { formatted = formatted.replace(/\$([$\w_.]+)/g, - (_, key) => objDisplay(objectAttr(items[0], key)) as unknown as string, + (_, key) => objDisplay(objectAttr(items[0], key), runner?.config?.chaiConfig) as unknown as string, // https://github.com/chaijs/chai/pull/1490 ) } diff --git a/packages/runner/src/types/runner.ts b/packages/runner/src/types/runner.ts index ef30599a864a..382b7eee04c1 100644 --- a/packages/runner/src/types/runner.ts +++ b/packages/runner/src/types/runner.ts @@ -13,6 +13,9 @@ export interface VitestRunnerConfig { hooks: SequenceHooks setupFiles: SequenceSetupFiles } + chaiConfig?: { + truncateThreshold?: number + } maxConcurrency: number testTimeout: number hookTimeout: number diff --git a/packages/utils/src/display.ts b/packages/utils/src/display.ts index f64a993531dc..7dff5b2c4fa4 100644 --- a/packages/utils/src/display.ts +++ b/packages/utils/src/display.ts @@ -3,6 +3,10 @@ import util from 'util' // @ts-expect-error doesn't have types import loupeImport from 'loupe' +interface LoupeOptions { + truncateThreshold?: number +} + const loupe = (typeof loupeImport.default === 'function' ? loupeImport.default : loupeImport) export function format(...args: any[]) { @@ -14,19 +18,21 @@ export function utilInspect(item: unknown, options?: util.InspectOptions) { } // chai utils -export function loupeInspect(obj: unknown): string { +export function loupeInspect(obj: unknown, options: LoupeOptions = {}): string { return loupe(obj, { depth: 2, - truncate: 40, + truncate: options.truncateThreshold === 0 + ? Infinity + : (options.truncateThreshold ?? 40), }) } -export function objDisplay(obj: unknown) { - const truncateThreshold = 40 - const str = loupeInspect(obj) +export function objDisplay(obj: unknown, options: LoupeOptions = {}): string { + const truncateThreshold = options.truncateThreshold ?? 40 + const str = loupeInspect(obj, options) const type = Object.prototype.toString.call(obj) - if (str.length >= truncateThreshold) { + if (truncateThreshold && str.length >= truncateThreshold) { if (type === '[object Function]') { const fn = obj as () => void return (!fn.name || fn.name === '')