
Company News
Socket Joins the OpenJS Foundation
Socket is proud to join the OpenJS Foundation as a Silver Member, deepening our commitment to the long-term health and security of the JavaScript ecosystem.
mocha-repeat
Advanced tools
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.. */
});
$ npm i --save-dev mocha-repeat
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.. */
});
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) {
}
});
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)
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
FAQs
Loop tests through diferrent variations of data.
The npm package mocha-repeat receives a total of 129 weekly downloads. As such, mocha-repeat popularity was classified as not popular.
We found that mocha-repeat demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket is proud to join the OpenJS Foundation as a Silver Member, deepening our commitment to the long-term health and security of the JavaScript ecosystem.

Security News
npm now links to Socket's security analysis on every package page. Here's what you'll find when you click through.

Security News
A compromised npm publish token was used to push a malicious postinstall script in cline@2.3.0, affecting the popular AI coding agent CLI with 90k weekly downloads.