Skip to content

Commit

Permalink
fix: test.each respects chaiConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Apr 11, 2023
1 parent 06852f1 commit 4f6c134
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/runner/src/suite.ts
Expand Up @@ -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
)
}
Expand Down
3 changes: 3 additions & 0 deletions packages/runner/src/types/runner.ts
Expand Up @@ -13,6 +13,9 @@ export interface VitestRunnerConfig {
hooks: SequenceHooks
setupFiles: SequenceSetupFiles
}
chaiConfig?: {
truncateThreshold?: number
}
maxConcurrency: number
testTimeout: number
hookTimeout: number
Expand Down
18 changes: 12 additions & 6 deletions packages/utils/src/display.ts
Expand Up @@ -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[]) {
Expand All @@ -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 === '')
Expand Down

0 comments on commit 4f6c134

Please sign in to comment.