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

[Feature]: it.each should take a function that takes the test inputs as parameters and returns a string #15444

Open
iway1 opened this issue Jan 11, 2025 · 1 comment

Comments

@iway1
Copy link

iway1 commented Jan 11, 2025

🚀 Feature Proposal

Right now to generate dynamic test names based on test input when using it.each jest has invented its own string replacement syntax which is non idiomatic to javascript and clunky to use. It lacks type safety and requires new devs to learn the syntax which is not ideal. Instead, it.each could just take a function that returns a string and receives the same input parameters as the test function itself:

const testCases = [{name: "my name"}];

// old
it.each(testCases)("testing $name", (obj)=>{

})

// new
it.each(testCases)((obj)=>`testing ${obj.name}`, (obj)=>{
  
})

AFAIK the above old version does not support passing arrays in any way so doing something like [[{name: "my name"}]] isn't possible either, which can be really annoying some times in cases where you want to pass one object containing all of your test data rather than a bunch of parameters and have many test cases:

const testCases = [[{name: "name1"}], [{name: "name2"}]]; // How do I get name from this??

And even if it does support it and I missed that in the docs, imagine how much easier it would have been for me and others if it was just a function returning a string in nice idiomatic javascript? I've seen the template string solution which I think possibly supports the above but at that point the api for this is just getting wacky in my opinion. Just use a function and there are 50 less things to learn and it supports all use cases.

Motivation

Above suggested API is clearly more intuitive and easier to learn IMO, and has much greater flexibility as JS code can run in the function in cases where more advanced formatting is useful. AFAIK the above does not support passing arrays in any way so doing something like [[{name: "my name"}]] isn't possible either, which can be really annoying some times in cases where you want to pass one object containing all of your test data rather than a bunch of parameters and have many test cases.

Example

No response

Pitch

It will reduce the learning curve while improving type safety and flexibility of the API. It just makes the library easier to use and to learn in a non-trivial way. Initial implementation can allow using the old syntax or optionally passing a function returning a string with the new syntax so it's backwards compatible

@iway1
Copy link
Author

iway1 commented Jan 12, 2025

lmk if this is something the maintainers would be open too I can work on a PR for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant