Skip to content

Commit df34770

Browse files
authored
feat: Introduce %$ option to add number of the test to its title (#7412)
1 parent 8ea9e14 commit df34770

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

docs/api/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ You can inject parameters with [printf formatting](https://nodejs.org/api/util.h
310310
- `%f`: floating point value
311311
- `%j`: json
312312
- `%o`: object
313-
- `%#`: index of the test case
313+
- `%#`: 0-based index of the test case
314+
- `%$`: 1-based index of the test case
314315
- `%%`: single percent sign ('%')
315316

316317
```ts

packages/runner/src/suite.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,11 +791,12 @@ function formatName(name: string | Function) {
791791
}
792792

793793
function formatTitle(template: string, items: any[], idx: number) {
794-
if (template.includes('%#')) {
794+
if (template.includes('%#') || template.includes('%$')) {
795795
// '%#' match index of the test case
796796
template = template
797797
.replace(/%%/g, '__vitest_escaped_%__')
798798
.replace(/%#/g, `${idx}`)
799+
.replace(/%\$/g, `${idx + 1}`)
799800
.replace(/__vitest_escaped_%__/g, '%%')
800801
}
801802
const count = template.split('%').length - 1

test/core/test/each.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,28 @@ test.each([
9898
expect(result).toBe(expected)
9999
})
100100

101+
test.each([
102+
[1, 1, 2],
103+
[1, 2, 3],
104+
[2, 1, 3],
105+
])('the number of the test case is %$', (a, b, expected) => {
106+
expect(a + b).toBe(expected)
107+
})
108+
109+
test.each([
110+
[1, 2, 3],
111+
[4, 5, 9],
112+
])('return a promise like result %$', async (a, b, expected) => {
113+
const promiseResolver = (first: number, second: number) => {
114+
return new Promise((resolve) => {
115+
setTimeout(() => resolve(first + second), 1)
116+
})
117+
}
118+
119+
const result = await promiseResolver(a, b)
120+
expect(result).toBe(expected)
121+
})
122+
101123
describe('context on test and describe - todo/skip', () => {
102124
let count = 0
103125

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { describe, expect, test } from "vitest";
2+
3+
describe('passed', () => {
4+
test.each([4, 5, 6])('0-based index of the test case is %#', (d) => {
5+
expect(d).toBe(d)
6+
})
7+
8+
test.each([4, 5, 6])('1-based index of the test case is %$', (d) => {
9+
expect(d).toBe(d)
10+
})
11+
})

test/reporters/tests/default.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,20 @@ describe('default reporter', async () => {
177177
expect(stdout).toContain('1 passed')
178178
expect(stdout).toContain('✓ repeat couple of times (repeat x3)')
179179
})
180+
181+
test('prints 0-based index and 1-based index of the test case', async () => {
182+
const { stdout } = await runVitest({
183+
include: ['print-index.test.ts'],
184+
root: 'fixtures/default',
185+
reporters: 'none',
186+
})
187+
188+
expect(stdout).toContain('✓ passed > 0-based index of the test case is 0')
189+
expect(stdout).toContain('✓ passed > 0-based index of the test case is 1')
190+
expect(stdout).toContain('✓ passed > 0-based index of the test case is 2')
191+
192+
expect(stdout).toContain('✓ passed > 1-based index of the test case is 1')
193+
expect(stdout).toContain('✓ passed > 1-based index of the test case is 2')
194+
expect(stdout).toContain('✓ passed > 1-based index of the test case is 3')
195+
})
180196
}, 120000)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy