mardi 6 octobre 2015

Problems sharing object instance with mocha.js

I've been struggling for a couple hours with this problem and I just can't find a solution.

Lets say I have this file(main.js) exporting an object:

var MyModule = require('./module');
var dependency = require('./moduleDependency');

exports.start = function start(app){
    var myObject = new MyModule(dependency);

    module.exports.myObject = myObject; //sharing this object
};

And then, in a controller I require it (controller.js) modifying that object:

var myObject = require('./main').myObject;

exports.routeLogic = function(req, res, next){
    myObject.setName('newName');
    res.sendStatus(200);
};

the problem is in my test-spec when I require myObject, those modifications are missing:

var myObject = require('./main').myObject;

describe('test spec', function(){
    before(function(done){
        //executing request in order to use that controller
    });
    it('should pass', function(){
        res.name.should.equal('newName');
    });
});

I'm using supertest and mocha for this tests

Thanks in advance ;]

Aucun commentaire:

Enregistrer un commentaire