diff --git a/docs/api/index.md b/docs/api/index.md index 729ca7576b3d..1fb03128b102 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -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 diff --git a/packages/runner/src/suite.ts b/packages/runner/src/suite.ts index 698763319279..1c30481739b9 100644 --- a/packages/runner/src/suite.ts +++ b/packages/runner/src/suite.ts @@ -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 diff --git a/test/core/test/each.test.ts b/test/core/test/each.test.ts index d122e7c81aae..6592edcd31ba 100644 --- a/test/core/test/each.test.ts +++ b/test/core/test/each.test.ts @@ -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 diff --git a/test/reporters/fixtures/default/print-index.test.ts b/test/reporters/fixtures/default/print-index.test.ts new file mode 100644 index 000000000000..266bd0967025 --- /dev/null +++ b/test/reporters/fixtures/default/print-index.test.ts @@ -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) + }) +}) diff --git a/test/reporters/tests/default.test.ts b/test/reporters/tests/default.test.ts index 52eb3e23fdbb..4386a98e28c3 100644 --- a/test/reporters/tests/default.test.ts +++ b/test/reporters/tests/default.test.ts @@ -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)