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: Introduce %$ option to add number of the test to its title #7412

Open
wants to merge 4 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
3 changes: 2 additions & 1 deletion docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ You can inject parameters with [printf formatting](https://nodejs.org/api/util.h
- `%f`: floating point value
- `%j`: json
- `%o`: object
- `%#`: index of the test case
- `%#`: 0-based index of the test case
- `%$`: 1-based index of the test case
- `%%`: single percent sign ('%')

```ts
Expand Down
3 changes: 2 additions & 1 deletion packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,11 +788,12 @@ function formatName(name: string | Function) {
}

function formatTitle(template: string, items: any[], idx: number) {
if (template.includes('%#')) {
if (template.includes('%#') || template.includes('%$')) {
// '%#' match index of the test case
template = template
.replace(/%%/g, '__vitest_escaped_%__')
.replace(/%#/g, `${idx}`)
.replace(/%\$/g, `${idx + 1}`)
.replace(/__vitest_escaped_%__/g, '%%')
}
const count = template.split('%').length - 1
Expand Down
22 changes: 22 additions & 0 deletions test/core/test/each.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ test.each([
expect(result).toBe(expected)
})

test.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('the number of the test case is %$', (a, b, expected) => {
expect(a + b).toBe(expected)
})

test.each([
[1, 2, 3],
[4, 5, 9],
])('return a promise like result %$', async (a, b, expected) => {
const promiseResolver = (first: number, second: number) => {
return new Promise((resolve) => {
setTimeout(() => resolve(first + second), 1)
})
}

const result = await promiseResolver(a, b)
expect(result).toBe(expected)
})

describe('context on test and describe - todo/skip', () => {
let count = 0

Expand Down
11 changes: 11 additions & 0 deletions test/reporters/fixtures/default/print-index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, expect, test } from "vitest";

describe('passed', () => {
test.each([4, 5, 6])('0-based index of the test case is %#', (d) => {
expect(d).toBe(d)
})

test.each([4, 5, 6])('1-based index of the test case is %$', (d) => {
expect(d).toBe(d)
})
})
16 changes: 16 additions & 0 deletions test/reporters/tests/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,20 @@ describe('default reporter', async () => {
expect(stdout).toContain('1 passed')
expect(stdout).toContain('✓ repeat couple of times (repeat x3)')
})

test('prints 0-based index and 1-based index of the test case', async () => {
const { stdout } = await runVitest({
include: ['print-index.test.ts'],
root: 'fixtures/default',
reporters: 'none',
})

expect(stdout).toContain('✓ passed > 0-based index of the test case is 0')
expect(stdout).toContain('✓ passed > 0-based index of the test case is 1')
expect(stdout).toContain('✓ passed > 0-based index of the test case is 2')

expect(stdout).toContain('✓ passed > 1-based index of the test case is 1')
expect(stdout).toContain('✓ passed > 1-based index of the test case is 2')
expect(stdout).toContain('✓ passed > 1-based index of the test case is 3')
})
}, 120000)