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] support for (it|test|describe).each(...) #120

Closed
atk opened this issue Dec 13, 2021 · 9 comments
Closed

[Feature] support for (it|test|describe).each(...) #120

atk opened this issue Dec 13, 2021 · 9 comments
Labels
enhancement New feature or request

Comments

@atk
Copy link
Contributor

atk commented Dec 13, 2021

Jest supports it.each: https://jestjs.io/docs/api#testeachtablename-fn-timeout with an array or a template table.

@patak-dev
Copy link
Member

patak-dev commented Dec 13, 2021

This was also requested by @userquin in the EPIC issue. We were talking with Anthony that .each could add a lot of complexity (#15 (comment)). But given that the output is formatted as a table and people seem to be keen on using it, I think it would be good to have a compat story here. See also comment in twitter about .concurrent.each that is an interesting case (we could do the same with a nested describe.concurrent though)

What I told to @userquin, and maybe someone else wants to take this one, is that 1. it may be good to wait until the internals are more stable to avoid the need to refactor this feature while the code is stabilizing and 2. we could discuss with a PR implementing it, because maybe the added complexity isn't that big at the end.

In case we don't want to support tabular output in the reporter, maybe we could implement .each with nested describes and keep a simple tree output, but at least users could migrate from Jest without changing their code (but taking the hit of losing the table at the CLI)

@patak-dev patak-dev added the enhancement New feature or request label Dec 19, 2021
@kirillgroshkov
Copy link

kirillgroshkov commented Dec 27, 2021

but at least users could migrate from Jest without changing their code

Would be awesome! 🙏

I've just tested migrating some of my smaller repos from Jest to Vitest, and found [only!] 2 broken things:

  1. lack of test.each
  2. lack of expect().rejects support (but this one is somewhat easier to mitigate/refactor in userland)

@kirillgroshkov
Copy link

15 minutes after writing last comment, managed to "money-patch" the missing test.each support by using this quick&dirty function (and my tests pass now 🚀 )

export function testEach(cases: any[][]): (name: string, fn: AnyFunction) => void {
  return (name, fn) => {
    cases.forEach(items => {
      test(name, () => fn(...items))
    })
  }
}

@oliverzy
Copy link

+1 for this feature.

image

@joshacheson
Copy link

joshacheson commented Jan 16, 2022

I tried the solution shared by @kirillgroshkov and found that though my tests passed, they weren't actually running. I tried using really basic sanity checks like expect(true).toBe(false) and got no failures. The callback of testEach(set)(name, callback) was running for each member of set, but describe, it and test calls inside the callback were not actually resulting in the tests being run.

stackblitz here

Two thoughts on my mind:

  1. There might have actually been a change in the behaviour of vitest for this situation in the 20 days since this solution was suggested.
  2. The behaviour I'm describing above may be intuitive for folks who know more about how vitest works.

EDIT: I got this to work. In my case, I had to switch test to describe in my implementation of testEach. Which uhhhh, makes sense, because I was writing a suit in the callback, not just a plain test. Leaving this comment in case anyone else finds the journey helpful. I'm editing the stackblitz link to include the fix.

@kirillgroshkov
Copy link

testEach returns a function, maybe you forgot to call it?

e.g testEach(...)()

That would explain why you go no failures of basic checks.

@privatenumber
Copy link

Would like this request to cover describe.each() too.

@Demivan Demivan changed the title [Feature] support for (it|test).each(...) [Feature] support for (it|test|describe).each(...) Jan 26, 2022
antfu added a commit that referenced this issue Jan 27, 2022
@antfu antfu closed this as completed in 9abb557 Jan 27, 2022
@hmt
Copy link

hmt commented Feb 1, 2022

Thank you very much! I was asking for this feature just today on Discord. Highly appreciated!!!

@sidharthv96
Copy link
Contributor

I was migrating MermaidJS to vitest and needed template literal support.
Wrote a simple function as a hack to convert template literals to Object array.

https://gist.github.com/sidharthv96/80cc25016d94b6f6590dec89e6586c64

@github-actions github-actions bot locked and limited conversation to collaborators Jun 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants