Registry / testing / mocha-each

mocha-each

JSON →
library2.0.1jsnpmunverified

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.

npm install mocha-each
INSTALL
IMPORT
SIG · MOCHA-EACH
M
mocha-each
testingjavascriptv2.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

forEach
import forEach from 'mocha-each';
const { forEach } = require('mocha-each');
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.
forEach (CommonJS)
const forEach = require('mocha-each');
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.

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.

const assert = require('assert'); const forEach = require('mocha-each'); function add(a, b) { return parseInt(a) + parseInt(b); } describe('add()', () => { forEach([ [1, 1, 2], [2, -2, 0], [140, 48, 188] ]) .it('adds %d and %d then returns %d', (left, right, expected) => { assert.equal(add(left, right), expected); }); context('with invalid arguments', () => { forEach([ [1, 'foo'], [null, 10], [{}, []] ]) .it('adds %j and %j then returns NaN', (left, right) => { const value = add(left, right); assert(isNaN(value)); }); }); });
Debug
Known issues
gotchaWhen 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.
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.
affects: >=1.0.0
gotcha`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.
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.
affects: <3.0.0
gotchaWhen 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.
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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: (0, _mochaEach.default) is not a function
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.
fix
Change your import statement to `const forEach = require('mocha-each');` which is the officially documented and most reliable way to import the package.
ReferenceError: forEach is not defined
The `forEach` function was not correctly imported or required into the test file before being used.
fix
Ensure you have `const forEach = require('mocha-each');` at the top of your test file or within the appropriate scope.
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
An asynchronous parameterized test did not signal its completion, either by not calling the `done()` callback or by not returning a Promise that resolves.
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.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
mocharequiredThis package is a utility that extends the Mocha testing framework, and Mocha must be installed as a peer dependency for it to function.
Agent activity
4 hits · last 30 days
node
4
Resources
mocha-each — npm install mocha-each · libregistry