mocha-repeat

0.1.0 • Public • Published

mocha-repeat

Loop tests through diferrent variations of data in mocha.js. Perfect for testing against multiple versions of 3rd-party libraries.

var mdescribe = require('mocha-repeat');
 
var libs = {
  'jquery-1.9': require('../vendor/jquery-1.9.js'),
  'jquery-2.0': require('../vendor/jquery-2.0.js'),
  'jquery-2.1': require('../vendor/jquery-2.1.js'),
};
 
mdescribe("Tests", libs, function (jQuery) {
 
  /* ..tests.. */
 
});

This will be expanded to:

describe("Tests (jquery-1.9)", function () {
  var jQuery = require('../vendor/jquery-1.9.js');
  /* ..tests.. */
});
 
describe("Tests (jquery-2.0)", function () {
  var jQuery = require('../vendor/jquery-2.0.js');
  /* ..tests.. */
});
 
describe("Tests (jquery-2.1)", function () {
  var jQuery = require('../vendor/jquery-2.0.js');
  /* ..tests.. */
});

Install

$ npm i --save-dev mocha-repeat

Why?

This effect is easily achievable in plain JavaScript, though the resulting code will be more verbose.

libs = { ... };
for (var version in libs) {
  if (libs.hasOwnProperty(version)) {
    var jQuery = libs[version];
 
    (function (jQuery, version) {
      describe("Test (" + version + ")", function () {
        /* ..tests.. */
      });
    })(jQuery, version);
  }
}

It's easier to write it this way:

libs = { ... };
mdescribe("Test", libs, function (jQuery, version) {
  /* ..tests.. */
});

Splatting

If the values are arrays, they will be spread across the function's arguments. In this example, the function's 2 arguments (jQuery, options) will be populated by the array items.

var libs = {
  'jquery-1.9': [ require('../vendor/jquery-1.9.js'), { legacy: true } ],
  'jquery-2.0': [ require('../vendor/jquery-2.0.js'), { } ],
  'jquery-2.1': [ require('../vendor/jquery-2.1.js'), { future: true } ],
};

mdescribe("Tests", stubs, function (jQuery, options) {

  if (options.legacy) {
  }

});

Permutations

You can nest calls to mocha-repeat. This is great for testing combinations of multiple library versions. In this example, it tests against every possble combination of underscore [1.0..1.2] with backbone [1.0..1.2].

var libs = {
  underscore: {
    '1.0': '../vendor/underscore/1.0.0.js',
    '1.1': '../vendor/underscore/1.1.0.js',
    '1.2': '../vendor/underscore/1.2.0.js',
  },
  backbone: {
    '1.0': '../vendor/backbone/1.0.0.js',
    '1.1': '../vendor/backbone/1.1.0.js',
    '1.2': '../vendor/backbone/1.2.0.js',
  },
};
 
var _, Backbone;
 
mdescribe("Underscore", libs.underscore, function (upath) {
  mdescribe("Backbone", libs.backbone, function (bpath) {
 
    // load the libraries
    // ...tip: https://www.npmjs.org/package/proxyquire
    before(function () {
      _ = require(upath);
      Backbone = proxyquire(bpath, { underscore: _ });
    });
 
    it ('loads without errors', function () {
      expect(_).is.a('function');
      expect(Backbone).is.a('object');
    });
  });
})
 

Output:

$ mocha -R list
 
  . Underscore (1.0) Backbone (1.0) loads without errors
  . Underscore (1.0) Backbone (1.1) loads without errors
  . Underscore (1.0) Backbone (1.2) loads without errors
  . Underscore (1.1) Backbone (1.0) loads without errors
  . Underscore (1.1) Backbone (1.1) loads without errors
  . Underscore (1.1) Backbone (1.2) loads without errors
  . Underscore (1.2) Backbone (1.0) loads without errors
  . Underscore (1.2) Backbone (1.1) loads without errors
  . Underscore (1.2) Backbone (1.2) loads without errors
 
  9 passing (120ms)

Thanks

mocha-repeat © 2014+, Rico Sta. Cruz. Released under the MIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).

ricostacruz.com  ·  GitHub @rstacruz  ·  Twitter @rstacruz

Readme

Keywords

none

Package Sidebar

Install

npm i mocha-repeat

Weekly Downloads

124

Version

0.1.0

License

MIT

Last publish

Collaborators

  • rstacruz