cypress icon indicating copy to clipboard operation
cypress copied to clipboard

Add support for `it.each` functionality

Open JessicaSachs opened this issue 2 years ago • 3 comments

What would you like?

I'd like the functionality of mocha-each to be included in Cypress and its global types.

Honestly, while it'd be nice to be in parity with Jest or Vitest, I don't mind the differences. I'd just like to be able to run an array through Cypress to generate N tests without having to implement test-name unique-ness each time.

I also think it's fine for the Command Log to treat them as sibling tests. I don't think it's necessary to make custom UI to handle it.each in Cypress.

Why is this needed?

I'm switching some component tests away from Vitest/Jest at work and we use it.each in order to generate tests with distinctly unique names.

This is so that the output generated by a reporter is unique and contains information about the inputs given to the it.

@bahmutov has a POC for cypress-each that was created in the last 12 months which can be used as a starting point.

Other

No response

JessicaSachs avatar Oct 07 '22 19:10 JessicaSachs

Here's a POC. It needs:

  1. it.only support for types
  2. Types.
it.each = (cases: unknown[][]): any => {
  // Whenever `it.each` is invoked.
  return (testName: string, testItself: Function) => {
    // Iterating over each case.
    for (const myCase of cases) {
     // Register the it function with Cypress
     // TODO: Add string formatting that matches Jest || Vitest docs!
      it(testName, () => {
        // Cypress is actually invoking the test.
        testItself.call(null, myCase);
      })
    }
  }
}

JessicaSachs avatar Dec 28 '22 06:12 JessicaSachs

Just learned about this in a Jest course. Would really appreciate this as well in Cypress.

jdvegmond avatar Feb 16 '24 12:02 jdvegmond

@jdvegmond https://github.com/bahmutov/cypress-each

bahmutov avatar Feb 16 '24 13:02 bahmutov