Skip to content

Commit 9f2dd41

Browse files
docs: add example of generating tests with a closure (#4494)
1 parent 9122909 commit 9f2dd41

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

docs/index.md

+26-11
Original file line numberDiff line numberDiff line change
@@ -679,30 +679,28 @@ describe('retries', function() {
679679

680680
## Dynamically Generating Tests
681681

682-
Given Mocha's use of `Function.prototype.call` and function expressions to define suites and test cases, it's straightforward to generate your tests dynamically. No special syntax is required — plain ol' JavaScript can be used to achieve functionality similar to "parameterized" tests, which you may have seen in other frameworks.
682+
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 ol' JavaScript can be used to achieve functionality similar to "parameterized" tests, which you may have seen in other frameworks.
683683

684684
Take the following example:
685685

686686
```js
687-
var assert = require('chai').assert;
687+
const assert = require('chai').assert;
688688

689-
function add() {
690-
return Array.prototype.slice.call(arguments).reduce(function(prev, curr) {
691-
return prev + curr;
692-
}, 0);
689+
function add(args) {
690+
return args.reduce((prev, curr) => prev + curr, 0);
693691
}
694692

695693
describe('add()', function() {
696-
var tests = [
694+
const tests = [
697695
{args: [1, 2], expected: 3},
698696
{args: [1, 2, 3], expected: 6},
699697
{args: [1, 2, 3, 4], expected: 10}
700698
];
701699

702-
tests.forEach(function(test) {
703-
it('correctly adds ' + test.args.length + ' args', function() {
704-
var res = add.apply(null, test.args);
705-
assert.equal(res, test.expected);
700+
tests.forEach(({args, expected}) => {
701+
it(`correctly adds ${args.length} args`, function() {
702+
const res = add(args);
703+
assert.equal(res, expected);
706704
});
707705
});
708706
});
@@ -719,6 +717,23 @@ $ mocha
719717
✓ correctly adds 4 args
720718
```
721719

720+
Tests added inside a `.forEach` handler often don't play well with editor plugins, especially with "right-click run" features.
721+
Another way to parameterize tests is to generate them with a closure. This following example is equivalent to the one above:
722+
723+
```js
724+
describe('add()', function() {
725+
const testAdd = ({args, expected}) =>
726+
function() {
727+
const res = add(args);
728+
assert.equal(res, expected);
729+
};
730+
731+
it('correctly adds 2 args', testAdd({args: [1, 2], expected: 3}));
732+
it('correctly adds 3 args', testAdd({args: [1, 2, 3], expected: 6}));
733+
it('correctly adds 4 args', testAdd({args: [1, 2, 3, 4], expected: 10}));
734+
});
735+
```
736+
722737
<h2 id="test-duration">Test duration</h2>
723738

724739
Many reporters will display test duration and flag tests that are slow (default: 75ms), as shown here with the SPEC reporter:

0 commit comments

Comments
 (0)