Skip to content

Commit

Permalink
feat: format test tablename (#2405)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
poyoho committed Dec 3, 2022
1 parent 2c52c52 commit 45c5c45
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
18 changes: 18 additions & 0 deletions docs/api/index.md
Expand Up @@ -244,6 +244,24 @@ You cannot use this syntax, when using Vitest as [type checker](/guide/testing-t
// ✓ add(2, 1) -> 3
```

You can also access Object attributes with `.`, if you are using objects as arguments:

```ts
test.each`
a | b | expected
${{ val: 1 }} | ${'b'} | ${'1b'}
${{ val: 2 }} | ${'b'} | ${'2b'}
${{ val: 3 }} | ${'b'} | ${'3b'}
`('add($a.val, $b) -> $expected', ({ a, b, expected }) => {
expect(a.val + b).toBe(expected)
})

// this will return
// ✓ add(1, b) -> 1b
// ✓ add(2, b) -> 2b
// ✓ add(3, b) -> 3b
```


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

Expand Down
8 changes: 4 additions & 4 deletions packages/vitest/src/runtime/suite.ts
@@ -1,6 +1,6 @@
import util from 'util'
import type { BenchFunction, BenchOptions, Benchmark, BenchmarkAPI, File, RunMode, Suite, SuiteAPI, SuiteCollector, SuiteFactory, SuiteHooks, Task, Test, TestAPI, TestFunction, TestOptions } from '../types'
import { getWorkerState, isObject, isRunningInBenchmark, isRunningInTest, noop } from '../utils'
import { getWorkerState, isObject, isRunningInBenchmark, isRunningInTest, noop, objectAttr } from '../utils'
import { createChainable } from './chain'
import { collectTask, collectorContext, createTestContext, runWithSuite, withTimeout } from './context'
import { getHooks, setBenchOptions, setFn, setHooks } from './map'
Expand Down Expand Up @@ -226,6 +226,7 @@ function createTest(fn: (
const arrayOnlyCases = cases.every(Array.isArray)
cases.forEach((i, idx) => {
const items = Array.isArray(i) ? i : [i]

arrayOnlyCases
? test(formatTitle(name, items, idx), () => fn(...items), options)
: test(formatTitle(name, items, idx), () => fn(i), options)
Expand Down Expand Up @@ -269,12 +270,11 @@ function formatTitle(template: string, items: any[], idx: number) {
.replace(/%#/g, `${idx}`)
.replace(/__vitest_escaped_%__/g, '%%')
}

const count = template.split('%').length - 1
let formatted = util.format(template, ...items.slice(0, count))
if (isObject(items[0])) {
formatted = formatted.replace(/\$([$\w_]+)/g, (_, key) => {
return items[0][key]
formatted = formatted.replace(/\$([$\w_.]+)/g, (_, key) => {
return objectAttr(items[0], key)
})
}
return formatted
Expand Down
12 changes: 12 additions & 0 deletions packages/vitest/src/utils/index.ts
Expand Up @@ -180,3 +180,15 @@ export function createDefer<T>(): DeferPromise<T> {
p.reject = reject!
return p
}

export function objectAttr(source: any, path: string, defaultValue = undefined) {
// a[3].b -> a.3.b
const paths = path.replace(/\[(\d+)\]/g, '.$1').split('.')
let result = source
for (const p of paths) {
result = Object(result)[p]
if (result === undefined)
return defaultValue
}
return result
}
9 changes: 9 additions & 0 deletions test/core/test/each.test.ts
Expand Up @@ -194,6 +194,15 @@ ${{ asd: 1 }} | ${'b'} | ${'[object Object]b'}
expect(a + b).toBe(expected)
})

test.each`
a | b | expected
${{ val: 1 }} | ${'b'} | ${'1b'}
${{ val: 2 }} | ${'b'} | ${'2b'}
${{ val: 3 }} | ${'b'} | ${'3b'}
`('returns $expected when $a.val is added $b', ({ a, b, expected }) => {
expect(a.val + b).toBe(expected)
})

test.each`
a | b | expected
${true} | ${true} | ${true}
Expand Down

0 comments on commit 45c5c45

Please sign in to comment.