in Education by
Problem I have several tests that do the same thing in mocha. This for me, it's duplication, and is the worst thing to do when you want your system to be maintenable. var exerciseIsPetitionActive = function (expected, dateNow) { var actual = sut.isPetitionActive(dateNow); chai.assert.equal(expected, actual); }; test('test_isPetitionActive_calledWithDateUnderNumSeconds_returnTrue', function () { exerciseIsPetitionActive(true, new Date('2013-05-21 13:11:34')); }); test('test_isPetitionActive_calledWithDateGreaterThanNumSeconds_returnFalse', function () { exerciseIsPetitionActive(false, new Date('2013-05-21 13:12:35')); }); What do I need I need a way of collapsing my duplicated mocha tests in only one. For example, in PhpUnit (and other test frameworks) you have dataProviders. In phpUnit a dataProvider works this way: <?php class DataTest extends PHPUnit_Framework_TestCase { /** * @dataProvider provider */ public function testAdd($a, $b, $c) { $this->assertEquals($c, $a + $b); } public function provider() { return array( array(0, 0, 0), array(0, 1, 1), array(1, 0, 1), array(1, 1, 3) ); } } The provider in here injects parameters to the test, and the test executes all the cases. Is perfect for duplicated test. I want to know if in mocha is there something similar, for example, something like this: var exerciseIsPetitionActive = function (expected, dateNow) { var actual = sut.isPetitionActive(dateNow); chai.assert.equal(expected, actual); }; @usesDataProvider myDataProvider test('test_isPetitionActive_calledWithParams_returnCorrectAnswer', function (expected, date) { exerciseIsPetitionActive(expected, date); }); var myDataProvider = function() { return { {true, new Date(..)}, {false, new Date(...)} }; }; What I have already looked at There is some tecnique that is called Shared Behaviours . But it does not solve the problem directly with a test suite, it just solve the problem with different components that have duplicated tests. The Question Do you know any way to implement dataProviders in mocha? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
A basic approach to run the same test with different data is to repeat the test in a loop providing the data: describe('my tests', function () { var runs = [ {it: 'options1', options: {...}}, {it: 'options2', options: {...}}, ]; before(function () { ... }); runs.forEach(function (run) { it('does sth with ' + run.it, function () { ... }); }); }); before runs, well, before all its in a describe. If you need to use some of the options in before, do not include it in the forEach loop because mocha will first run all befores and the all its, which is probably not wanted. You can either put the whole describe in the loop: var runs = [ {it: 'options1', options: {...}}, {it: 'options2', options: {...}}, ]; runs.forEach(function (run) { describe('my tests with ' + run.it, function () { before(function () { ... }); it('does sth with ' + run.it, function () { ... }); }); }); If you do not wish to pollute your tests with multiple describes, you can use the controversial module sinon for this matter: var sinon = require('sinon'); describe('my tests', function () { var runs = [ {it: 'options1', options: {...}}, {it: 'options2', options: {...}}, ]; // use a stub to return the proper configuration in `beforeEach` // otherwise `before` is called all times before all `it` calls var stub = sinon.stub(); runs.forEach(function (run, idx) { stub.onCall(idx).returns(run); }); beforeEach(function () { var run = stub(); // do something with the particular `run.options` }); runs.forEach(function (run, idx) { it('does sth with ' + run.it, function () { sinon.assert.callCount(stub, idx + 1); ... }); }); }); Sinon feels dirty but is effective. Several aid modules such as leche are based on sinon, but arguably introducing further complexity is not necessary.

Related questions

0 votes
    I have this array of objects. let links = [ { url: 'some url 1', status: 200 }, { url: ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I created an list. When you select each , there will be active classes each. Now, my goal is how ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Multiple variables can be created and initialized in a single JavaScript statement 1. False 2. True...
asked Feb 23, 2021 by JackTerrance
0 votes
    Multiple containers running on a single machine all share the same resources such as the operating system kernel for instant boot and ... ? 1. True 2. False 3. Doesn't make sense...
asked Jun 22, 2021 in Technology by JackTerrance
0 votes
    Different JavaScript Questions and Answers...
asked Sep 17, 2022 in Technology by Editorial Staff
0 votes
    The method adopted by block cipher modes to generate unique ciphertexts even if the same plaintext is encrypted multiple times ... Vector (2)Random Keys (3)Padding (4)Blockchain...
asked Mar 19, 2021 in Technology by JackTerrance
0 votes
    I've created a getSpectrum method using the getByteFrequencyData method on the Web Audio API's Analyser Node ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
0 votes
    hello I have the following code that maps through my text and prints typography, {['Example1:', 'Example2 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    hello I have the following code that maps through my text and prints typography, {['Example1:', 'Example2 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    I found found several question's answers similar to this, I tried some of them, but no luck. May ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I found found several question's answers similar to this, I tried some of them, but no luck. May ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
...