I am trying to use RequireJS in node, and found difficulties with path issues.
Here is a simple foo method that returns "foo"
$ cat src/foo.js
define([], function() {
var foo = function() {
return "foo";
};
return { foo:foo};
});
Here is bar that requires foo, but it works only when specifying relative path. Is that how it's supposed to be?
$ cat src/bar.js
define(['./foo.js'], function(foo) {
var bar = function() {
return foo.foo().replace("foo","bar");
};
return { bar : bar };
});
Things get much trickier in the mocha test:
- Loading foo and bar requires __dirname workarounds.
- The async loading of bar fails (see test 3 and 4).
- Importing Squire needs exact path, since it is installed using npm install, but does not conform to the standard node require syntax and does not include the amdefine workaround:
Here is the test code:
$ cat test/footests.js
var requirejs = require('requirejs');
var chai = requirejs("chai");
var should = chai.should();
var Squire = requirejs(__dirname + "/../node_modules/squirejs/src/Squire.js");
describe('when calling foo.foo()', function () {
it('should return "foo"', function() {
var foo = requirejs(__dirname + "/../src/foo.js");
foo.foo().should.equal("foo");
});
});
describe('when calling bar.bar()', function () {
var bar = requirejs(__dirname + "/../src/bar.js");
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(__dirname + "/../src/bar.js", 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.js', fooMock)
.require(__dirname + "/../src/bar.js", function(bar) {
bar.bar().should.equal("barbar");
done();
});
});
});
I've setup a reproduction on github https://github.com/itaifrenkel/node-requirejs-example