Skip to content

Commit 1868f1c

Browse files
authoredDec 5, 2022
feat: format test objects in each title (fix #2420) (#2421)
* test: add test for objectAttr * feat: pretty print test title objects * style: apply lint * refactor(dev): add return type for formatObject * feat(dev): use maxWidth specify maximum printed width for properties such as arrays and sets * refactor: use `chaiUtil.objDisplay` instead
1 parent d43f3f5 commit 1868f1c

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed
 

‎packages/vitest/src/runtime/suite.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import util from 'util'
2+
import { util as chaiUtil } from 'chai'
23
import type { BenchFunction, BenchOptions, Benchmark, BenchmarkAPI, File, RunMode, Suite, SuiteAPI, SuiteCollector, SuiteFactory, SuiteHooks, Task, Test, TestAPI, TestFunction, TestOptions } from '../types'
34
import { getWorkerState, isObject, isRunningInBenchmark, isRunningInTest, noop, objectAttr } from '../utils'
45
import { createChainable } from './chain'
@@ -273,9 +274,10 @@ function formatTitle(template: string, items: any[], idx: number) {
273274
const count = template.split('%').length - 1
274275
let formatted = util.format(template, ...items.slice(0, count))
275276
if (isObject(items[0])) {
276-
formatted = formatted.replace(/\$([$\w_.]+)/g, (_, key) => {
277-
return objectAttr(items[0], key)
278-
})
277+
formatted = formatted.replace(/\$([$\w_.]+)/g,
278+
(_, key) => chaiUtil.objDisplay(objectAttr(items[0], key)) as unknown as string,
279+
// https://github.com/chaijs/chai/pull/1490
280+
)
279281
}
280282
return formatted
281283
}

‎test/core/test/utils.spec.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test } from 'vitest'
2-
import { assertTypes, deepClone, deepMerge, resetModules, toArray } from '../../../packages/vitest/src/utils'
2+
import { assertTypes, deepClone, deepMerge, objectAttr, resetModules, toArray } from '../../../packages/vitest/src/utils'
33
import { deepMergeSnapshot } from '../../../packages/vitest/src/integrations/snapshot/port/utils'
44
import type { ModuleCacheMap } from '../../../packages/vite-node/src/types'
55

@@ -182,3 +182,28 @@ describe('resetModules doesn\'t resets only user modules', () => {
182182
expect(moduleCache.size).toBe(2)
183183
})
184184
})
185+
186+
describe('objectAttr', () => {
187+
const arrow = (a: number) => a * 3
188+
const func = function (a: number) {
189+
return a * 3
190+
}
191+
192+
test.each`
193+
value | path | expected
194+
${{ foo: 'bar' }} | ${'foo'} | ${'bar'}
195+
${{ foo: { bar: 'baz' } }} | ${'foo'} | ${{ bar: 'baz' }}
196+
${{ foo: { bar: 'baz' } }} | ${'foo.bar'} | ${'baz'}
197+
${{ foo: [{ bar: 'baz' }] }} | ${'foo.0.bar'} | ${'baz'}
198+
${{ foo: [1, 2, ['a']] }} | ${'foo'} | ${[1, 2, ['a']]}
199+
${{ foo: [1, 2, ['a']] }} | ${'foo.2'} | ${['a']}
200+
${{ foo: [1, 2, ['a']] }} | ${'foo.2.0'} | ${'a'}
201+
${{ foo: [[1]] }} | ${'foo.0.0'} | ${1}
202+
${{ deep: [[[1]]] }} | ${'deep.0.0.0'} | ${1}
203+
${{ a: 1, b: 2, c: 3, d: 4 }} | ${'a'} | ${1}
204+
${{ arrow }} | ${'arrow'} | ${arrow}
205+
${{ func }} | ${'func'} | ${func}
206+
`('objectAttr($value, $path) -> $expected', ({ value, path, expected }) => {
207+
expect(objectAttr(value, path)).toEqual(expected)
208+
})
209+
})

0 commit comments

Comments
 (0)
Please sign in to comment.