Skip to content

Enhance test.each message format #6413

@stropho

Description

@stropho

Enhance test.each message format:

  1. positional placeholders such as %2$s
  2. index of test set to clarify which test set (table row) was used in a failed test

Motivation

  1. Easier way to customize test messages
  2. Easier way to find which test set (table row) failed

Example

There are a few ways I can think of
A/ smarter message format like in sprintf-js package

// not a real world example
test.each([[1, 1, 2], [1, 2, 3], [2, 1, 3]])(
  '`%3$i` created using .add(%1$i, %2$i)',
  (a, b, expected) => {
    expect(a + b).toBe(expected);
  },
)

Still not sure how to specify optional test set (table row) index

B/ message as a function - more customizable, and in my opinion easier to implement

test.each([[1, 1, 2], [1, 2, 3], [2, 1, 3]])(
 (a, b, expected, index) => // or in a similar way
   `${index}: ${expectedResult} created using .add(${a}, ${b}) `,
  (a, b, expected) => {
    expect(a + b).toBe(expected);
  },
)

Is this something that could be considered as a helpful feature and implemented in jest?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions