1

I have a function called connectToMongo that connects to a locally-running mongodb database myapp. I have another function which invokes connectToMongo. I would like to test this other function with mocha. However, I would like to make it so that, while testing, any calls to connectToMongo will automatically connect to my myapp_test database instead of my normal one.

Ideally (I think) I'd like to do something like this (where connectToMongoTestingDatabase connects to myapp_test):

var connectToMongo = require('./connectToMongo'),
    connectToMongoTestingDatabase = require('./connectToMongoTestingDatabase')
    registerUser = require('./registerUser');

testingLibrary.configureAlternateBehavior(connectToMongo, connectToMongoTestingDatabase);

it('should register a user', function (done) {

    registerUser({name: 'bob', password: 'password1'}, function (error, response) {
        connectToMongoTestingDatabase(function (error, database) {
            database.collection('users').findOne({name: 'bob'}, function (error, user) {
                expect(user).not.to.be.null;
                done();
            });
        });
    });

});

I have been trying to make sense of the Sinon.JS library. I can't tell whether spies, stubs or mocks would fulfill this need, nor what the equivalent of testingLibrary.configureAlternateBehavior would be.

I suppose I could also create some global variable called global.MYAPP_TESTING, set that to true before I start my tests, and in the implementation of connectToMongo I could connect to the myapp_test database if global.MYAPP_TESTING is true. That seems bad though.

What is the best way to substitute the behavior of one function for another in JavaScript unit testing?

1
  • This is often what the DI (Dependency injection) pattern is used for. Make a single-use set of functions you just require() in place of the mongo business. Commented Aug 23, 2014 at 16:40

1 Answer 1

1

If you are running a unit test, then you should mock all external dependencies (functions, objects) used in your function and just check that they are called or got desired values.

To make possible integration testing in your ConnectToMongo function you should get the connection string to db from environment depending config. So when you'll run the test you'll set environment to 'test' and get one config object, during development you may have another config object (which different settings) and on live environment a 3d config. Check out this for more info on making environment dependent config: Node.js setting up environment specific configs to be used with everyauth

Sign up to request clarification or add additional context in comments.

1 Comment

Ended up doing process.env.NODE_ENV = 'test' before running mocha, and then using a config script that sets the correct mongo url based on the value of NODE_ENV, and requiring that config in connectToMongo when connecting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.