question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`test.each` should perform string interpolation with object properties

See original GitHub issue

🚀 Feature Proposal

When giving an array of objects to the test.each overload that accepts an array, and not a template strings table, jest should interpolate the object’s properties into the test name.

Motivation

The current interpolation of template strings works great, but it doesn’t provide any hook for type-safety. This is problematic when scaling up to large and complex test cases, and in TypeScript, it allows any to sneak into your test functions. It also is more intuitive, it left me wondering for a while why a transformation to a type-safe version of a test wasn’t causing interpolation of the test case.

Example

We can do something like this right now:

test.each`
  a    | b    | expectedResult
  ${1} | ${1} | ${2}
  ${2} | ${2} | ${4}
`('add($a, $b) === $expectedResult', ({ a, b, expectedResult }) => {
  expect(a + b).toEqual(expectedResult);
});

Which will output the test cases: √ add(1, 1) === 2 √ add(2, 2) === 4

In order to achieve type-safety on the same test, I would make this transformation:

type AddExample = {
  a: number;
  b: number;
  expectedResult: number;
};

test.each<AddExample>([
  { a: 1, b: 1, expectedResult: 2 },
  { a: 2, b: 2, expectedResult: 4 },
])('add($a, $b) === $expectedResult', ({ a, b, expectedResult }) => {
  expect(a + b).toEqual(expectedResult);
});

Which I would expect should output the same test cases as above, but instead: √ add($a, $b) === $expectedResult √ add($a, $b) === $expectedResult

Pitch

It is isomorphic to functionality in the core library, but I believe it cannot be implemented as an extension.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mattphillipscommented, Aug 13, 2020

This has been something I’ve wanted to add for a while but not got around to it and I think we should add parity with the tagged template table with $ syntax for sure 👍

0reactions
github-actions[bot]commented, Jun 8, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jest .each name access object key - javascript - Stack Overflow
Jest describe.each expects an array of arrays in the first parameter. If you pass in a 1D array, internally it will be mapped...
Read more >
Reduce boilerplate test code with Jest it.each | by E.Y. - Medium
As we can compare, the differences in this way are : Instead of passing an array, it passes in a table using string...
Read more >
String Interpolation in JavaScript - Dmitri Pavlutin
Let's see in more detail, with examples and best practices, how to use template strings to perform string interpolation in JavaScript.
Read more >
string interpolation - format string output - Microsoft Learn
To identify a string literal as an interpolated string, prepend it with the $ symbol. You can't have any white space between the...
Read more >
Python String Interpolation - Programiz
For instance, if you have a template for saying hello to a person like "Hello {Name of person}, nice to meet you!", you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found