Injector
Injector copied to clipboard
Error: Tried to overwrite <moduleName>: Cannot have two modules with the same name.
Hey,
Just following up from our twitter discussion: Code snippet below:
// userService - the module I am testing (condensed version)
// inject
exports.userService = function(userRepository, validationService, passwordService, deferred, mongojs, lang) {
return {
// methods
}
};
// My test for userService, condensed again but removing all the expectations still causes a failure
var Injector = require('injector');
var expect = require('chai').expect;
var sinon = require('sinon');
var _ = require('lodash');
var repositoryMock = require('../mocks/userRepoMock');
var passwordServiceMock = require('../mocks/passwordServiceMock');
describe('userService', function() {
var injector, service, clock, lang;
beforeEach(function(done) {
Injector.create('tripul', {
directory: [
process.cwd() + '/modules/services/',
process.cwd() + '/lang/'
],
mock: {
userRepository: repositoryMock,
passwordService: passwordServiceMock
}
}, function(err, _injector) {
injector = _injector;
service = injector.inject('userService');
lang = injector.inject('lang');
done();
});
clock = sinon.useFakeTimers();
});
afterEach(function() {
clock.restore();
});
describe('getting a user', function() {
it('should get a user by email', function() {
expect(true).to.be.equal(true);
});
});
// The above was working fine before I added a new folder and module called lang. Once I added that as a second member of the directory array it now throws an error:
userService "before each" hook:
Uncaught Error: Tried to overwrite "passwordService". Cannot have two modules with the same name.
at Injector.register (/Users/simon/Sites/tripul/node_modules/injector/index.js:87:15)
at Injector.module (/Users/simon/Sites/tripul/node_modules/injector/index.js:193:23)
at /Users/simon/Sites/tripul/node_modules/injector/index.js:55:30
at /Users/simon/Sites/tripul/node_modules/injector/node_modules/async/lib/async.js:108:13
at Array.forEach (native)
at _each (/Users/simon/Sites/tripul/node_modules/injector/node_modules/async/lib/async.js:32:24)
at Object.async.each (/Users/simon/Sites/tripul/node_modules/injector/node_modules/async/lib/async.js:107:9)
at /Users/simon/Sites/tripul/node_modules/injector/index.js:52:34
at fs.js:266:14
at Object.oncomplete (fs.js:107:15)
Shout if you need anything else code wise
Thanks!
Looks like an issue that only exists for multiple directories. Should have a fix out shortly.
Ah wonderful, thanks
@simonsmith it says that you tried to create the module passwordService twice. Do you have a module in each directory with that name?
If so you, you can't. They the injection works is by module name. No matter which directory the module is in, it must be uniquely named.
Nope, just the one. It was working until I added the lang directory to the Injector test configuration:

My mock is just a normal module.exports as well.
A quick search reveals only one instance of exports.passwordService in the whole project too, just to be sure.
If it helps I can try and replicate this on a smaller scale and then share the broken code