cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.69k stars 3.16k forks source link

Add support for `it.each` functionality #24173

Open JessicaSachs opened 1 year ago

JessicaSachs commented 1 year ago

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 commented 1 year ago

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);
      })
    }
  }
}
jdvegmond commented 7 months ago

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

bahmutov commented 7 months ago

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