it-each is a JavaScript module designed to extend Mocha's testing capabilities by enabling asynchronous test looping. It allows developers to iterate over data sets, executing a test case for each item with configurable titles and data extraction. The package, currently at version 0.5.0, explicitly states it is below v1.0.0 due to an unclear roadmap, implying potential breaking changes, though stability is aimed for. It operates by modifying Mocha's global `it` handler and works with Mocha v2.1.0 and likely newer versions, though no guarantees are made for future compatibility. Key features include automatic adjustment of `timeout` and `slow` values for collective tests, and an option (`testPerIteration`) to generate a separate test entry for each iteration, which helps in isolating failures. Its primary differentiator is its simplicity and direct integration into Mocha's existing `it` interface, making it easy to adapt existing tests for data-driven scenarios.
npm install it-eachVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `it-each` in a Mocha test file, iterating over an array of user data. It shows both an asynchronous `process` function using `next()` and a synchronous one, along with how to enable `testPerIteration` for granular test reporting.
Monitor the project's GitHub repository for updates and test thoroughly when upgrading to new patch versions, as they might introduce unexpected behavior changes given the pre-1.0.0 status.
Ensure you test your `it-each` based tests thoroughly when upgrading your Mocha version. If compatibility issues arise, consider pinning an older Mocha version or exploring alternatives like `mocha-each` or `jest-each` (if migrating to Jest).
Always call `next()` when using asynchronous operations within your `process` function, typically after all async tasks are resolved, similar to how Mocha's `done` callback is used. If `next` is not in the parameters, the function is treated as synchronous.
For clearer feedback on individual test performance and easier debugging of specific iterations, consider setting `testPerIteration: true` in `require('it-each')({ testPerIteration: true })`. This generates a separate test entry for each iteration.Ensure you are calling `require('it-each')()` as a function, rather than just `require('it-each')`. For example: `require('it-each')();`Verify that your `process` function, if it contains asynchronous operations (e.g., Promises, `async/await`), correctly calls the `next()` callback after all operations have finished.
Ensure that Mocha is properly installed and your test files are executed using the Mocha test runner (e.g., `mocha your-test-file.js`). Also, ensure `require('it-each')()` is called after Mocha has been 'instantiated' or loaded in your test environment.