{"id":11342,"library":"mocha-each","title":"Parameterized Tests for Mocha","description":"mocha-each is a utility for the Mocha testing framework that simplifies the creation of parameterized tests. It allows developers to define a single test case that runs multiple times with varying sets of input data, significantly reducing code duplication and enhancing test coverage. The current stable version is 2.0.1. While specific release cadence isn't explicitly stated, the project appears to be stable and maintained, with the latest version indicating ongoing support. Its primary differentiator is the seamless integration of data-driven testing within Mocha's existing `it` and `describe` syntax, making it straightforward to iterate over arrays of test data. A key feature is its guarantee that all parameterized test cases are executed, even if some individual cases fail, which is crucial for comprehensive test reporting. It also supports parameterization at the `describe` block level and handles asynchronous test cases effectively.","status":"active","version":"2.0.1","language":"javascript","source_language":"en","source_url":"git://github.com/ryym/mocha-each","tags":["javascript","parameterized","test","mocha","each"],"install":[{"cmd":"npm install mocha-each","lang":"bash","label":"npm"},{"cmd":"yarn add mocha-each","lang":"bash","label":"yarn"},{"cmd":"pnpm add mocha-each","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This package is a utility that extends the Mocha testing framework, and Mocha must be installed as a peer dependency for it to function.","package":"mocha","optional":false}],"imports":[{"note":"The package primarily uses CommonJS `require` as shown in documentation, but a default ESM import may work in some environments. Named imports for CommonJS are incorrect.","wrong":"const { forEach } = require('mocha-each');","symbol":"forEach","correct":"import forEach from 'mocha-each';"},{"note":"This is the documented and most reliable way to import `mocha-each` in Node.js environments, especially for older projects or those not configured for ESM.","symbol":"forEach (CommonJS)","correct":"const forEach = require('mocha-each');"}],"quickstart":{"code":"const assert = require('assert');\nconst forEach = require('mocha-each');\n\nfunction add(a, b) {\n  return parseInt(a) + parseInt(b);\n}\n\ndescribe('add()', () => {\n  forEach([\n    [1, 1, 2],\n    [2, -2, 0],\n    [140, 48, 188]\n  ])\n  .it('adds %d and %d then returns %d', (left, right, expected) => {\n    assert.equal(add(left, right), expected);\n  });\n\n  context('with invalid arguments', () => {\n    forEach([\n      [1, 'foo'],\n      [null, 10],\n      [{}, []]\n    ])\n    .it('adds %j and %j then returns NaN', (left, right) => {\n      const value = add(left, right);\n      assert(isNaN(value));\n    });\n  });\n});\n","lang":"javascript","description":"Demonstrates how to use `mocha-each` to run a simple parameterized test for an `add` function with different input sets, verifying both valid and invalid arguments within a Mocha test suite."},"warnings":[{"fix":"Use `.only()` sparingly and verify that all intended tests are executed. For isolating specific tests, consider running individual test files or using other Mocha filtering options.","message":"When using `.only()` or `.skip()` on `forEach` blocks, be aware that Mocha's native behavior for these modifiers applies. If multiple `forEach` blocks are marked with `.only()`, only the last one encountered by Mocha might actually run, potentially leading to missed tests. `mocha-each` wraps `.only()` in a nameless `describe` block to ensure its parameters are exclusive, but global interaction needs care.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For consistent behavior, especially in Node.js environments, stick to `const forEach = require('mocha-each');`. If you need full ESM support, check for a newer major version of `mocha-each` or use a different parameterized testing library that explicitly supports ESM.","message":"`mocha-each` version 2.x is primarily designed for CommonJS (`require`). While some environments might allow ESM `import forEach from 'mocha-each'`, it's not explicitly documented or guaranteed. Using named imports for CommonJS modules (e.g., `import { forEach } from 'mocha-each';`) will likely fail.","severity":"gotcha","affected_versions":"<3.0.0"},{"fix":"Ensure your async test case function accepts `done` as its last argument (e.g., `(arg1, arg2, done) => { ... }`) and calls `done()` upon completion, or alternatively, ensure the function returns a Promise that resolves when the asynchronous operation is complete.","message":"When defining parameterized tests with asynchronous code, it's crucial to explicitly call the `done()` callback or return a Promise within your test function. Failing to do so will result in tests timing out or completing prematurely without proper assertion, leading to false positives or test failures.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement to `const forEach = require('mocha-each');` which is the officially documented and most reliable way to import the package.","cause":"Attempting to use a default ESM import (`import forEach from 'mocha-each';`) when the module is primarily CommonJS and transpilation/bundling does not correctly handle it.","error":"TypeError: (0, _mochaEach.default) is not a function"},{"fix":"Ensure you have `const forEach = require('mocha-each');` at the top of your test file or within the appropriate scope.","cause":"The `forEach` function was not correctly imported or required into the test file before being used.","error":"ReferenceError: forEach is not defined"},{"fix":"For callback-style async tests, add `done` as the last argument to your test function and call `done()` when the async operation finishes. For Promise-based async tests, ensure your test function returns the Promise.","cause":"An asynchronous parameterized test did not signal its completion, either by not calling the `done()` callback or by not returning a Promise that resolves.","error":"Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure \"done()\" is called; if returning a Promise, ensure it resolves."}],"ecosystem":"npm"}