Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
simonsmith opened this issue Oct 18, 2013 · 4 comments

Comments

@simonsmith
Copy link

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!

@scottcorgan
Copy link
Owner

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

@simonsmith
Copy link
Author

Ah wonderful, thanks

@scottcorgan
Copy link
Owner

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

@simonsmith
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants