Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parametrized tests with Mocha

Tags:

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?

like image 899
Erel Segal-Halevi Avatar asked Jul 10 '13 11:07

Erel Segal-Halevi


People also ask

Does Mocha run tests in parallel?

Mocha does not run individual tests in parallel. That means if you hand Mocha a single, lonely test file, it will spawn a single worker process, and that worker process will run the file. If you only have one test file, you'll be penalized for using parallel mode. Don't do that.

What is Mocha chai testing?

Mocha logo. Mocha is a JavaScript test framework running on Node. js and in the browser. Mocha allows asynchronous testing, test coverage reports, and use of any assertion library. Chai is a BDD / TDD assertion library for NodeJS and the browser that can be delightfully paired with any javascript testing framework.

What are pending tests in Mocha?

A pending test in many test framework is test that the runner decided to not run. Sometime it's because the test is flagged to be skipped. Sometime because the test is a just a placeholder for a TODO. For Mocha, the documentation says that a pending test is a test without any callback.


2 Answers

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)          });     }); }); 
like image 147
Jacob Jedryszek Avatar answered Oct 14 '22 07:10

Jacob Jedryszek


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);   }) }) 
like image 37
Mike Avatar answered Oct 14 '22 06:10

Mike