parametrized tests with Mocha

13,596

Solution 1

Take a look at async.each. It should enable you to call the same describe, it, and expect/should statements, and you can pass in the parameters to the closure.

var async = require('async')
var expect = require('expect.js')

async.each([1,2,3], function(itemNumber, callback) {
  describe('Test # ' + itemNumber, function () {
    it("should be a number", function (done) {
      expect(itemNumber).to.be.a('number')
      expect(itemNumber).to.be(itemNumber)
      done()
    });
  });
callback()
});

gives me:

$ mocha test.js -R spec
  Test # 1
    ✓ should be a number 
  Test # 2
    ✓ should be a number 
  Test # 3
    ✓ should be a number 
  3 tests complete (19 ms)

Here's a more complex example combining async.series and async.parallel: Node.js Mocha async test doesn't return from callbacks

Solution 2

You do not need async package. You can use forEach loop directly:

[1,2,3].forEach(function (itemNumber) {
    describe("Test # " + itemNumber, function () {
        it("should be a number", function (done) {
            expect(itemNumber).to.be.a('number')
            expect(itemNumber).to.be(itemNumber) 
        });
    });
});

Solution 3

I know this was posted a while ago but there is now a node module that makes this really easy!! mocha param

const itParam = require('mocha-param').itParam;
const myData = [{ name: 'rob', age: 23 }, { name: 'sally', age: 29 }];

describe('test with array of data', () => {
    itParam("test each person object in the array", myData, (person) =>   {
    expect(person.age).to.be.greaterThan(20);
  })
})

Solution 4

actually the mocha documentation specifies how to create what you want here

describe('add()', function() {
  var 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);
    });
  });
 });

so the answer provided Jacob is correct just you need to define the variable firs before iterating it.

Share:
13,596

Related videos on Youtube

Erel Segal-Halevi
Author by

Erel Segal-Halevi

I am a faculty member in Ariel University, computer science department. My research topic is Fair Division of Land. It is related to the classic problem of fair cake-cutting, which is a multi-disciplinary topic connecting mathematics, economics and computer science, I am always happy to discuss any topic related to land division or fair cake-cutting. If you have a new idea in these topics and need a partner for brain-storming, feel free to email me at [email protected]. The answers I receive in the Stack Exchange websites are very useful, and I often cite them in papers. See my website for examples.

Updated on September 14, 2022

Comments

  • Erel Segal-Halevi
    Erel Segal-Halevi over 1 year

    How can I create parametrized tests with Mocha?

    Sample use case: I have 10 classes, that are 10 different implementations of the same interface. I want to run the exact same suit of tests for each class. I can create a function, that takes the class as a parameter and runs all tests with that class, but then I will have all tests in a single function - I won't be able to separate them nicely to different "describe" clauses...

    Is there a natural way to do this in Mocha?

  • Dukeatcoding
    Dukeatcoding over 9 years
    is it really necessary to use async, would not array.every (foreach loop) do the same without the need of another third party tool ?
  • hellboy
    hellboy over 8 years
    With [1,4].forEach(function (itemNumber) { I got TypeError: Cannot call method 'forEach' of undefined
  • hellboy
    hellboy about 8 years
    Should I always use array instead of dictionary?
  • naoko
    naoko about 8 years
    @hellboy, might want to make sure terminate previous statement with semicolon
  • Alexander Mills
    Alexander Mills over 6 years
    async is a great lib, my fav in Node.js-land, but wrapping Mocha describe blocks with asynchronous calls is incorrect usage of Mocha. First of all, callbacks in the async library should always be called in the next tick of the event loop, always. Second, Mocha registers all the blocks and hooks synchronously - if you register a block asynchronously, there is no guarantee that it will run. See the delay functionality in Mocha: github.com/mochajs/mocha/issues/362