Skip to content

Making Server side Tests

ADSKLowenthal edited this page Oct 28, 2015 · 11 revisions

Server Side Tests

Routes and Controllers

To test routes and controllers together it's necessary to issue requests and read the responses.

Currently using SuperTest, some examples will follow soon.

Mocking Modules

Using Proxyquire allows you to mock just part of a module. Anything unmocked will behave as normal. However, it only works if you're directly require the file you want to mock for.

Testing with a request through SuperTest, it may not be possible to mock because it's communicating with a different instance of the code.

Database

If your tests interact with the database, it may be necessary to return your collections to a zero state.

This can remove all entries from an existing collection:

describe('tests', function(){
    beforeEach(function(){
        return Model.remove({}).exec();
    });
});

But be careful, if you've made changes to the schema (specifically to indexes), then that will not recreate indexes. So in most cases it will be better to drop the collection first:

describe('tests', function(){
    before(function(done){
        mongoose.connection.collections['collection'].drop(done);
    });

    beforeEach(function(){
        return Model.remove({}).exec();
    });
});
Clone this wiki locally