Mocha.js equivalent of it.each
Mochajs doesn't have `it.each` but it's still possible to easily write repeating tests
I was working with web-test-runner 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:
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 as the testing framework and Mocha doesn't have it.each
. I tried using a plugin and then tried to use Jest with WTR but both had some drawbacks. Then I realized it's actually easier than expected:
["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.