There are multiple issues in your code:
Don't put
.js
at the end of module names. Adding.js
tells RequireJS to bypass the normal module name resolution. (There are some cases where you want the extension but these should be cases where you can say why you need it.)RequireJS in Node can take a configuration, just like when you use it in a browser. This eliminates the need to duplicate paths all over the place.
RequireJS can load packages with the
packages
setting.Don't load Node modules with RequireJS when there's no need for it. (For instance,
chai
does not need to be loaded with RequireJS.)
If I modify bar.js
to drop the .js
extension from its lone dependency and use the following test file, it works:
var requirejs = require('requirejs');
requirejs.config({
baseUrl: __dirname + "/../src",
packages: [
{
name: "squirejs",
location: "../node_modules/squirejs",
main: "src/Squire"
}
]
});
var chai = require("chai");
var should = chai.should();
var Squire = requirejs("squirejs");
describe('when calling foo.foo()', function () {
it('should return "foo"', function() {
var foo = requirejs("foo");
foo.foo().should.equal("foo");
});
});
describe('when calling bar.bar()', function () {
var bar = requirejs("bar");
it('should return "bar"', function() {
bar.bar().should.equal("bar");
});
});
describe('when calling bar.bar() with async requirejs', function () {
it('should return "bar"', function(done) {
requirejs(["bar"], function(bar) {
bar.bar().should.equal("bar");
done();
});
});
});
describe('when mocking foo.foo() and calling bar.bar()', function () {
it('should return "barbar"', function(done) {
var injector = new Squire();
var fooMock = {
foo : function() {
return "foofoo"; /* instead of just foo */
}
};
injector
.mock('./foo', fooMock)
.require(["bar"], function(bar) {
bar.bar().should.equal("barfoo");
done();
});
});
});