From 9f2dd41292615ae7798ac47d37202e37f02a266e Mon Sep 17 00:00:00 2001 From: Maxwell Gerber Date: Fri, 5 Feb 2021 08:19:39 -0800 Subject: [PATCH] docs: add example of generating tests with a closure (#4494) --- docs/index.md | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/docs/index.md b/docs/index.md index b039931a16..7147a6c11e 100644 --- a/docs/index.md +++ b/docs/index.md @@ -679,30 +679,28 @@ describe('retries', function() { ## Dynamically Generating Tests -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. +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. Take the following example: ```js -var assert = require('chai').assert; +const assert = require('chai').assert; -function add() { - return Array.prototype.slice.call(arguments).reduce(function(prev, curr) { - return prev + curr; - }, 0); +function add(args) { + return args.reduce((prev, curr) => prev + curr, 0); } describe('add()', function() { - var tests = [ + const tests = [ {args: [1, 2], expected: 3}, {args: [1, 2, 3], expected: 6}, {args: [1, 2, 3, 4], expected: 10} ]; - tests.forEach(function(test) { - it('correctly adds ' + test.args.length + ' args', function() { - var res = add.apply(null, test.args); - assert.equal(res, test.expected); + tests.forEach(({args, expected}) => { + it(`correctly adds ${args.length} args`, function() { + const res = add(args); + assert.equal(res, expected); }); }); }); @@ -719,6 +717,23 @@ $ mocha ✓ correctly adds 4 args ``` +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: + +```js +describe('add()', function() { + const testAdd = ({args, expected}) => + function() { + const res = add(args); + assert.equal(res, expected); + }; + + it('correctly adds 2 args', testAdd({args: [1, 2], expected: 3})); + it('correctly adds 3 args', testAdd({args: [1, 2, 3], expected: 6})); + it('correctly adds 4 args', testAdd({args: [1, 2, 3, 4], expected: 10})); +}); +``` +

Test duration

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