Minimal AMD loader mostly stolen from @wycats.
To run the test you'll need to have
testem installed. Install it with npm install -g testem
.
(You'll also have to install the bower components, which you can do by running
bower install
)
You may run them with:
testem ci
You can also launch testem development mode with:
testem
The optional second argument to require
accepts an object for one or more module dependencies to mock. The object's keys should be the name (path) of the dependency modules to mock, and the values should be the exports value of the mocked module.
define('foo', ['./bar/baz', './buzz'], function(baz, buzz) {
return function() {
if ( baz() ) {
alert('Hello ' + buzz);
}
};
});
var foo = require('foo', {
'./bar/baz': function() { return true; },
'./buzz': 'World!'
});
foo(); // alerts 'Hello World!'
Below is a simple example of mocking a module dependency for a QUnit styled unit test:
// quiz.js
define('quiz', ['./answer-key'], function(AnswerKey) {
function Quiz() {}
Quiz.prototype.checkAnswer = function(question, answer) {
return AnswerKey[question] === answer;
};
return Quiz;
});
// tests.js
test('checkAnswer returns boolean', function() {
var Quiz = require('quiz', {
'./answer-key': { foo: 'bar' }
});
var quiz = new Quiz();
ok( quiz.checkAnswer('foo', 'bar') );
ok( ! quiz.checkAnswer('foo', 'baz') );
});
loader.js is MIT Licensed.