Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: format test tablename #2405

Merged
merged 11 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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