Skip to content

jakedetels/loader.js

 
 

Repository files navigation

loader.js Build Status

Minimal AMD loader mostly stolen from @wycats.

Tests

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

Mocking module dependencies for unit testing

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') );
});

License

loader.js is MIT Licensed.

About

minimal amd loader mostly stolen from wycats.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 98.3%
  • HTML 1.7%