Skip to content

Commit 45c5c45

Browse files
authoredDec 3, 2022
feat: format test tablename (#2405)
* feat: custom format test.each named * docs: format title * chore: update * chore: update * chore: update * chore: upadte * docs: update * chore: revert * chore: update * feat: object attr getter * docs: object getter
1 parent 2c52c52 commit 45c5c45

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed
 

‎docs/api/index.md

+18
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,24 @@ You cannot use this syntax, when using Vitest as [type checker](/guide/testing-t
244244
// ✓ add(2, 1) -> 3
245245
```
246246

247+
You can also access Object attributes with `.`, if you are using objects as arguments:
248+
249+
```ts
250+
test.each`
251+
a | b | expected
252+
${{ val: 1 }} | ${'b'} | ${'1b'}
253+
${{ val: 2 }} | ${'b'} | ${'2b'}
254+
${{ val: 3 }} | ${'b'} | ${'3b'}
255+
`('add($a.val, $b) -> $expected', ({ a, b, expected }) => {
256+
expect(a.val + b).toBe(expected)
257+
})
258+
259+
// this will return
260+
// ✓ add(1, b) -> 1b
261+
// ✓ add(2, b) -> 2b
262+
// ✓ add(3, b) -> 3b
263+
```
264+
247265

248266
Starting from Vitest 0.25.3, you can also use template string table.
249267

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import util from 'util'
22
import type { BenchFunction, BenchOptions, Benchmark, BenchmarkAPI, File, RunMode, Suite, SuiteAPI, SuiteCollector, SuiteFactory, SuiteHooks, Task, Test, TestAPI, TestFunction, TestOptions } from '../types'
3-
import { getWorkerState, isObject, isRunningInBenchmark, isRunningInTest, noop } from '../utils'
3+
import { getWorkerState, isObject, isRunningInBenchmark, isRunningInTest, noop, objectAttr } from '../utils'
44
import { createChainable } from './chain'
55
import { collectTask, collectorContext, createTestContext, runWithSuite, withTimeout } from './context'
66
import { getHooks, setBenchOptions, setFn, setHooks } from './map'
@@ -226,6 +226,7 @@ function createTest(fn: (
226226
const arrayOnlyCases = cases.every(Array.isArray)
227227
cases.forEach((i, idx) => {
228228
const items = Array.isArray(i) ? i : [i]
229+
229230
arrayOnlyCases
230231
? test(formatTitle(name, items, idx), () => fn(...items), options)
231232
: test(formatTitle(name, items, idx), () => fn(i), options)
@@ -269,12 +270,11 @@ function formatTitle(template: string, items: any[], idx: number) {
269270
.replace(/%#/g, `${idx}`)
270271
.replace(/__vitest_escaped_%__/g, '%%')
271272
}
272-
273273
const count = template.split('%').length - 1
274274
let formatted = util.format(template, ...items.slice(0, count))
275275
if (isObject(items[0])) {
276-
formatted = formatted.replace(/\$([$\w_]+)/g, (_, key) => {
277-
return items[0][key]
276+
formatted = formatted.replace(/\$([$\w_.]+)/g, (_, key) => {
277+
return objectAttr(items[0], key)
278278
})
279279
}
280280
return formatted

‎packages/vitest/src/utils/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,15 @@ export function createDefer<T>(): DeferPromise<T> {
180180
p.reject = reject!
181181
return p
182182
}
183+
184+
export function objectAttr(source: any, path: string, defaultValue = undefined) {
185+
// a[3].b -> a.3.b
186+
const paths = path.replace(/\[(\d+)\]/g, '.$1').split('.')
187+
let result = source
188+
for (const p of paths) {
189+
result = Object(result)[p]
190+
if (result === undefined)
191+
return defaultValue
192+
}
193+
return result
194+
}

‎test/core/test/each.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ ${{ asd: 1 }} | ${'b'} | ${'[object Object]b'}
194194
expect(a + b).toBe(expected)
195195
})
196196

197+
test.each`
198+
a | b | expected
199+
${{ val: 1 }} | ${'b'} | ${'1b'}
200+
${{ val: 2 }} | ${'b'} | ${'2b'}
201+
${{ val: 3 }} | ${'b'} | ${'3b'}
202+
`('returns $expected when $a.val is added $b', ({ a, b, expected }) => {
203+
expect(a.val + b).toBe(expected)
204+
})
205+
197206
test.each`
198207
a | b | expected
199208
${true} | ${true} | ${true}

0 commit comments

Comments
 (0)
Please sign in to comment.