vitest icon indicating copy to clipboard operation
vitest copied to clipboard

`%%` in a `test.each` / `describe.each` / `test.for` title consumes a parameter

Open trackoor opened this issue 3 weeks ago • 1 comments

Describe the bug

The docs list %% as "single percent sign ('%')", but in a parameterized title it also consumes one row value, so every placeholder after it gets the next value and the last placeholder prints undefined.

Reproduction

// a.test.js
import { describe, test } from 'vitest'

test.each([[50, 'half']])('%d%% is %s', () => {})
test.each([['a']])('100%% of %s', () => {})
describe.each([[3]])('%s%% group', () => {
  test('t', () => {})
})
test.for([[7, 'x']])('%d%% %s', () => {})
npx vitest run --reporter=verbose

Actual test names:

50% half is undefined
100% a of undefined
3% undefined group > t
7% x undefined

Expected:

50% is half
100% of a
3% group > t
7% x

Jest prints the expected names for the same file.

In formatTitle (packages/vitest/src/runtime/runner/suite.ts), formatRegExp matches %% and calls format([match[0], items[i++]], inspectOptions), which formats ['%%', item] as '% ' + item and increments i.

System Info

System:
  OS: Linux 6.1 Debian GNU/Linux 12 (bookworm)
  CPU: (4) x64 Intel(R) Xeon(R) CPU @ 2.20GHz
Binaries:
  Node: 26.13.0
  npm: 11.20.0
npmPackages:
  vitest: 5.1.0 (also on main f21f224bad11)

Used Package Manager

npm

Validations

Credit: https://ucsbplse.github.io/

trackoor avatar Nov 07 '26 20:11 trackoor

I reproduced this and have a fix ready locally.

The problem sits in formatTitle (packages/vitest/src/runtime/runner/suite.ts). It walks the title with formatRegExp and pulls one row value for every match before calling format(), and %% is one of the matches. format() itself treats %% as an escape that takes no argument, so the value pulled by formatTitle ends up printed after the % and every placeholder after it shifts by one, with the last one getting undefined. That matches the reported output exactly (50% half is undefined).

My fix makes a %% match print a literal % and take no value. While testing this I also found a neighbour bug in the same function: the -0 handling for %f looks the value up by counting only %f placeholders, so %s %f with a -0 value renders a 0 and loses the minus. It now looks the value up by the placeholder's actual position. I have an e2e test covering the reported cases plus these two, failing before and passing after, and the related suites (each, display utils, default/verbose reporters) pass.

Happy to open a PR with this if that's welcome. I saw new-contributor PRs need the issue labelled approved first, so flagging here before I push anything.

0xABCD01 avatar Nov 08 '26 07:11 0xABCD01