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

Add %_ format to skip a parameter in test.each/for #7511

Open
4 tasks done
hermit99 opened this issue Feb 18, 2025 · 1 comment · May be fixed by #7522
Open
4 tasks done

Add %_ format to skip a parameter in test.each/for #7511

hermit99 opened this issue Feb 18, 2025 · 1 comment · May be fixed by #7522

Comments

@hermit99
Copy link

Clear and concise description of the problem

The current formats do not provide a way to skip some parameters in the test name output when test data is array.

For example,

it.each([
  ['row1', { ...params1 }, 'expected1'],
  ['row2', { ...params2 }, 'expected2'],
])('%#. %s => %s %s', (row, param, expected) => tester())

// This will return
// 0. row1 => {...params1} expected1
// 1. row2 => {...params2} expected2
// The middle params is always displayed

Although we can work around

  1. by using object for each test data, but that's much more verbose
  2. by adjusting the order of parameters, e.g. make the 'expected' before the actual params, but that doesn't flow elegantly either.

I'm hoping to have more flexibility to control which param is shown in test name.

Suggested solution

Format %_ indicates that parameter is skipped in the test name

it.each([
  ['row1', { ...params1 }, 'expected1'],
  ['row2', { ...params2 }, 'expected2'],
])('%#. %s => %_%s', (row, param, expected) => tester())

// This will return
// 0. row1 => expected1
// 1. row2 => expected2
// The middle params is omitted

Alternative

No response

Additional context

No response

Validations

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Feb 18, 2025

How about supporting element index of array like object? https://stackblitz.com/edit/vitest-dev-vitest-wvqa9r6r?file=test%2Fbasic.test.ts

// this works
it.each([
  { 0: 'row1', 1: {}, 2: 'expected1' },
  { 0: 'row2', 1: {}, 2: 'expected2' },
])('%#. $0 => $2', () => {});
// ✓ 0. 'row1' => 'expected1'
// ✓ 1. 'row2' => 'expected2'


// but this doesn't currently
it.each([
  ['row1', {}, 'expected1'],
  ['row2', {}, 'expected2'],
])('%#. $0 => $2', () => {});
// ✓ 0. $0 => $2
// ✓ 1. $0 => $2

(Currently object key $x gets quoted but that'll be fixed by #5946)

@hi-ogawa hi-ogawa moved this to P2 - 2 in Team Board Feb 18, 2025
@hi-ogawa hi-ogawa linked a pull request Feb 19, 2025 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: P2 - 2
Development

Successfully merging a pull request may close this issue.

2 participants