Skip to content

Instantly share code, notes, and snippets.

@spoike
Created June 26, 2014 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spoike/6c1500985cc516e175dc to your computer and use it in GitHub Desktop.
Save spoike/6c1500985cc516e175dc to your computer and use it in GitHub Desktop.
Utility method for your Mocha tests that loads in your modules with RequireJS.
// Needs RequireJS's `require` and mocha's `before`
/**
* Works like mocha's `before` function but enables you to load in dependencies
* as well.
*
* @param deps [Array] the dependencies passed on to RequireJS
* @param callback [Function] the callback with loaded dependencies as arguments
*/
requireBefore = function(deps, callback) {
before(function(done) {
require(deps, function() {
callback.apply(undefined, arguments);
done();
});
});
};
// Example Usage:
requireBefore(['dep1', 'dep2'], function(dep1, dep2) {
// dep1 and dep2 are available here asynchonously but subsequent tests are not
// run until after the dependencies are loaded and this callback is executed
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment