From 224c565f78636788de617aae82dcec5edd586cbc Mon Sep 17 00:00:00 2001 From: Maxwell Gerber Date: Mon, 2 Nov 2020 10:40:51 -0700 Subject: [PATCH 1/5] docs: Add example of generating tests with a HOF --- docs/index.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/index.md b/docs/index.md index b039931a16..24f959e324 100644 --- a/docs/index.md +++ b/docs/index.md @@ -719,6 +719,26 @@ $ 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 paramaterize tests is by generating them with a higher order function. This example is equivalent to the one above. + +```js +describe('add()', function() { + function testAdd(test) { + return function () { + var res = add.apply(null, test.args); + assert.equal(res, test.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: From cda8eaabf7fd88c0af3c4bdeeef74d4a94771bd2 Mon Sep 17 00:00:00 2001 From: Maxwell Gerber Date: Mon, 2 Nov 2020 18:26:22 -0700 Subject: [PATCH 2/5] Update docs/index.md Co-authored-by: Christopher Hiller --- docs/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index 24f959e324..22b6fcb55c 100644 --- a/docs/index.md +++ b/docs/index.md @@ -720,7 +720,7 @@ $ mocha ``` Tests added inside a `.forEach` handler often don't play well with editor plugins, especially with "right-click run" features. -Another way to paramaterize tests is by generating them with a higher order function. This example is equivalent to the one above. +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() { From 28ad8166cd8c64f80986b258be34974b7da43cb4 Mon Sep 17 00:00:00 2001 From: Maxwell Gerber Date: Mon, 2 Nov 2020 18:31:01 -0700 Subject: [PATCH 3/5] es6-ify all the things --- docs/index.md | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/docs/index.md b/docs/index.md index 22b6fcb55c..57fbe8c3cf 100644 --- a/docs/index.md +++ b/docs/index.md @@ -684,25 +684,23 @@ Given Mocha's use of `Function.prototype.call` and function expressions to defin 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); + return Array.prototype.slice.call(arguments).reduce((prev, curr) => prev + curr, 0); } -describe('add()', function() { - var tests = [ +describe('add()', () => { + 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', () => { + const res = add(...args); + assert.equal(res, expected); }); }); }); @@ -723,13 +721,11 @@ Tests added inside a `.forEach` handler often don't play well with editor plugin 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() { - function testAdd(test) { - return function () { - var res = add.apply(null, test.args); - assert.equal(res, test.expected); - }; - } +describe('add()', () => { + const testAdd = ({args, expected}) => () => { + const res = add(...args); + assert.equal(res, expected); + }; it('correctly adds 2 args', testAdd({args: [1, 2], expected: 3})); From d19cb72317d61dfca83b4db3c01c77d92c235002 Mon Sep 17 00:00:00 2001 From: Maxwell Gerber Date: Wed, 4 Nov 2020 15:25:51 -0700 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: Christopher Hiller --- docs/index.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/index.md b/docs/index.md index 57fbe8c3cf..fd6bd6e02f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -686,8 +686,8 @@ Take the following example: ```js const assert = require('chai').assert; -function add() { - return Array.prototype.slice.call(arguments).reduce((prev, curr) => prev + curr, 0); +function add(...args) { + return args.reduce((prev, curr) => prev + curr, 0); } describe('add()', () => { @@ -698,7 +698,7 @@ describe('add()', () => { ]; tests.forEach(({args, expected}) => { - it('correctly adds ' + args.length + ' args', () => { + it(`correctly adds ${args.length} args`, function() { const res = add(...args); assert.equal(res, expected); }); @@ -721,8 +721,8 @@ Tests added inside a `.forEach` handler often don't play well with editor plugin Another way to parameterize tests is to generate them with a closure. This following example is equivalent to the one above: ```js -describe('add()', () => { - const testAdd = ({args, expected}) => () => { +describe('add()', function() { + const testAdd = ({args, expected}) => function() { const res = add(...args); assert.equal(res, expected); }; From a5cccfed6eea480758d7e8b053607e86c7bea18a Mon Sep 17 00:00:00 2001 From: Maxwell Gerber Date: Thu, 5 Nov 2020 15:40:27 -0700 Subject: [PATCH 5/5] Update docs/index.md --- docs/index.md | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/docs/index.md b/docs/index.md index fd6bd6e02f..7147a6c11e 100644 --- a/docs/index.md +++ b/docs/index.md @@ -679,18 +679,18 @@ 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 const assert = require('chai').assert; -function add(...args) { +function add(args) { return args.reduce((prev, curr) => prev + curr, 0); } -describe('add()', () => { +describe('add()', function() { const tests = [ {args: [1, 2], expected: 3}, {args: [1, 2, 3], expected: 6}, @@ -699,7 +699,7 @@ describe('add()', () => { tests.forEach(({args, expected}) => { it(`correctly adds ${args.length} args`, function() { - const res = add(...args); + const res = add(args); assert.equal(res, expected); }); }); @@ -722,15 +722,14 @@ Another way to parameterize tests is to generate them with a closure. This follo ```js describe('add()', function() { - const testAdd = ({args, expected}) => function() { - const res = add(...args); - assert.equal(res, expected); - }; + 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})); }); ```