Given Mocha’s use of function expressions to define suites and test cases, it’s straightforward to generate your tests dynamically.
No special syntax is required — plain old JavaScript can be used to achieve functionality similar to “parameterized” tests, which you may have seen in other frameworks.
Take the following example:
The above code will produce a suite with three specs:
Tests added inside a .forEach handler often don’t play well with editor plugins, especially with “right-click run” features.
Another way to parameterize tests is to generate them with a closure.
This following example is equivalent to the one above:
With top-level await you can collect your test data in a dynamic and asynchronous way while the test file is being loaded.
See also --delay for CommonJS modules without top-level await.