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: allow array element for test.each/for title formatting #7522

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 22 additions & 9 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,16 +330,29 @@ test.each([
// ✓ add(2, 1) -> 3
```

You can also access object properties with `$` prefix, if you are using objects as arguments:
You can also access object properties and array elements with `$` prefix:

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

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

test.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('add($0, $1) -> $2', (a, b, expected) => {
expect(a + b).toBe(expected)
})

// this will return
// ✓ add(1, 1) -> 2
Expand Down
25 changes: 15 additions & 10 deletions packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -809,16 +809,21 @@ 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,
// https://github.com/chaijs/chai/pull/1490
(_, key) =>
objDisplay(objectAttr(items[0], key), {
truncate: runner?.config?.chaiConfig?.truncateThreshold,
}) as unknown as string,
)
}
const isObjectItem = isObject(items[0])
formatted = formatted.replace(
/\$([$\w.]+)/g,
(_, key: string) => {
const isArrayKey = /^\d+$/.test(key)
if (!isObjectItem && !isArrayKey) {
return `$${key}`
}
const arrayElement = isArrayKey ? objectAttr(items, key) : undefined
const value = isObjectItem ? objectAttr(items[0], key, arrayElement) : arrayElement
return objDisplay(value, {
truncate: runner?.config?.chaiConfig?.truncateThreshold,
}) as unknown as string
},
)
return formatted
}

Expand Down
50 changes: 50 additions & 0 deletions test/reporters/fixtures/test-for-title.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { expect, test } from "vitest"

test.for([
{ 0: 'a', 1: {}, 2: { te: "st" } },
{ "0": 'b', "1": [], "2": ["test"] },
])('test.for object : 0 = $0, 2 = $2', () => {});

test.each([
{ 0: 'a', 1: {}, 2: { te: "st" } },
{ "0": 'b', "1": [], "2": ["test"] },
])('test.each object : 0 = $0, 2 = $2 ', () => {});

test.for([
['a', {}, { te: "st" }],
['b', [], [ "test" ]],
])('test.for array : 0 = $0, 2 = $2', () => {});

test.each([
['a', {}, { te: "st" }],
['b', [], [ "test" ]],
])('test.each array : 0 = $0, 2 = $2', () => {});

test.each([
{ a: 1, b: 1, expected: 2 },
{ a: 1, b: 2, expected: 3 },
{ a: 2, b: 1, expected: 3 },
])('object : add($a, $b) -> $expected', ({ a, b, expected }) => {
expect(a + b).toBe(expected)
})

test.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('array : add($0, $1) -> $2', (a, b, expected) => {
expect(a + b).toBe(expected)
})

test.for([
[{ k1: "v1" }, { k2: "v2" }],
])('first array element is object: 0 = $0, 1 = $1, k1 = $k1, k2 = $k2', () => {})

test.for([
["foo", "bar"],
])('first array element is not object: 0 = $0, 1 = $1, k = $k', () => {})

test.for([
{ k: "v1" },
{ k: "v2" },
])('not array: 0 = $0, 1 = $1, k = $k', () => {})
32 changes: 32 additions & 0 deletions test/reporters/tests/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,36 @@ describe('default reporter', async () => {
expect(stdout).toContain('1 passed')
expect(stdout).toContain('✓ repeat couple of times (repeat x3)')
})

test('test.each/for title format', async () => {
const { stdout } = await runVitest({
include: ['fixtures/test-for-title.test.ts'],
reporters: [['default', { isTTY: true, summary: false }]],
config: false,
})
expect(
[...stdout.matchAll(/(✓ .*)$/gm)].map(v => v[0]).filter(v => !v.includes('ms')),
).toMatchInlineSnapshot(`
[
"✓ test.for object : 0 = 'a', 2 = { te: 'st' }",
"✓ test.for object : 0 = 'b', 2 = [ 'test' ]",
"✓ test.each object : 0 = 'a', 2 = { te: 'st' } ",
"✓ test.each object : 0 = 'b', 2 = [ 'test' ] ",
"✓ test.for array : 0 = 'a', 2 = { te: 'st' }",
"✓ test.for array : 0 = 'b', 2 = [ 'test' ]",
"✓ test.each array : 0 = 'a', 2 = { te: 'st' }",
"✓ test.each array : 0 = 'b', 2 = [ 'test' ]",
"✓ object : add(1, 1) -> 2",
"✓ object : add(1, 2) -> 3",
"✓ object : add(2, 1) -> 3",
"✓ array : add(1, 1) -> 2",
"✓ array : add(1, 2) -> 3",
"✓ array : add(2, 1) -> 3",
"✓ first array element is object: 0 = { k1: 'v1' }, 1 = { k2: 'v2' }, k1 = 'v1', k2 = undefined",
"✓ first array element is not object: 0 = 'foo', 1 = 'bar', k = $k",
"✓ not array: 0 = { k: 'v1' }, 1 = undefined, k = 'v1'",
"✓ not array: 0 = { k: 'v2' }, 1 = undefined, k = 'v2'",
]
`)
})
}, 120000)