> ## Content Index
> Fetch the complete content index at: https://muratcorlu.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Mocha.js equivalent  of it.each
- URL: https://muratcorlu.com/mocha-js-equivalent-of-it-each/
- Published: 2023-03-08T15:22:50.000Z
- Updated: 2023-03-08T15:22:50.000Z
- Description: Mochajs doesn't have `it.each` but it's still possible to easily write repeating tests
- Author: Murat Çorlu
- Tags: testing, Notes

I was working with [web-test-runner](https://modern-web.dev/guides/test-runner/getting-started/?ref=muratcorlu.com) to write some tests for our a Web Component in our Baklava Design System. There was some repeating tests checking similar use-cases with different key codes. I wanted to use `it.each` to simplify the test like below:

```js
it.each(["Space", "Enter", "ArrowDown", "ArrowUp"])(
  "should open popover with %i key",
  async (keyCode) => {
    // given
    await sendKeys({
      press: "Tab",
    });
    await sendKeys({
      press: keyCode,
    });

    // then
    expect(blSelect?.opened).to.equal(true);
  }
);

```

But, [WTR uses Mochajs](https://modern-web.dev/docs/test-runner/test-frameworks/mocha/?ref=muratcorlu.com) as the testing framework and Mocha doesn't have `it.each`. I tried using [a plugin](https://www.npmjs.com/package/mocha-each?ref=muratcorlu.com) and then tried to use Jest with WTR but both had some drawbacks. Then I realized it's actually [easier than expected](https://mochajs.org/?ref=muratcorlu.com#dynamically-generating-tests):

```js
["Space", "Enter", "ArrowDown", "ArrowUp"].forEach((keyCode) => {
  it(`should open popover with ${keyCode} key`, async () => {
    // given
    await sendKeys({
      press: "Tab",
    });
    await sendKeys({
      press: keyCode,
    });

    // then
    expect(blSelect?.opened).to.equal(true);
  });
});

```

This took more than one hour for me. I hope this will help for some people to not loose that time again.