Injector icon indicating copy to clipboard operation
Injector copied to clipboard

Error: Tried to overwrite <moduleName>: Cannot have two modules with the same name.

Open simonsmith opened this issue 12 years ago • 4 comments

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!

simonsmith avatar Oct 18 '13 19:10 simonsmith

Looks like an issue that only exists for multiple directories. Should have a fix out shortly.

scottcorgan avatar Oct 21 '13 16:10 scottcorgan

Ah wonderful, thanks

simonsmith avatar Oct 21 '13 20:10 simonsmith

@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.

scottcorgan avatar Oct 22 '13 15:10 scottcorgan

Nope, just the one. It was working until I added the lang directory to the Injector test configuration:

screen shot 2013-10-22 at 17 05 08

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

simonsmith avatar Oct 22 '13 16:10 simonsmith