I am using mocha to run my unit test cases for my nodejs code. However I got surprised when I found that one of the module doesn't load its dependent module when run all the test cases together, however it works perfectly fine when I run these test file separately. I did explore each files but couldn't why it creates problem when I try to run all test files? Following are details.
Server.js (This is the problematic file)
var constants = require("./../utilities/Constants");
var log = require('./../utilities/Logger');
var validator = require('validator');
var Utilities = require('./../../common/Utilities');
var documentOperationModule = require('./../persistence/DocumentOperation');
module.exports = {
loadAppConfig: function (callback) {
//load application config from database and export as module.
documentOperationModule.getAllDocuments(constants.APPLICATION_CONFIG_COLLECTION, "_id", -1, function (err, appConfigs) {
if (!err) {
global.appConfig = appConfigs[0];
callback();
}
else
{
callback(new Error("Unable to load app config data error " + err));
}
});
}
};
It throws following error when run all test cases together
TypeError: documentOperationModule.getAllDocuments is not a function
However it works perfectly file when i run each test case file separately.
Profile.test.js
var expect = require('chai').expect;
var sinon = require('sinon');
var Utilities = require('app-root-path').require('/src/common/Utilities');
var Workout = require('app-root-path').require('/src/server/modules/Workout');
var UnitTestUtils = require('app-root-path').require('/src-test/unit/utils/UnitTestUtils');
var constants = require('app-root-path').require('/src/server/utilities/Constants');
var Server = require('app-root-path').require('/src/server/modules/Server');
//Test suit for getUserSchedule()
describe("getUserSchedule()", function () {
it("should get user schedule", function (done) {
var userId = "sddfgsdgf";
//load global config first
Server.loadAppConfig(function () {
expect(global.appConfig.defaultroutine).to.be.not.undefined;
//generate user schedule
Workout.getUserSchedule(userId, function (err, schedule) {
expect(schedule.sequences[0]).to.include("w");
//exit from test
done();
});
});
});
});
DocumentOperation.js
//Load database connection
var DBModule = require('./../persistence/DBModule.js');
var constants = require("./../utilities/Constants");
var AppUtilities = require('./../utilities/AppUtilities');
var Server = require('./../modules/Server');
var Utilities = require('./../../common/Utilities');
var log = require('./../utilities/Logger');
var Crypto = require('./../modules/Crypto');
module.exports = {
// Get all docuemnts for given collection.
getAllDocuments: function (collection, sortby, order, callback) {
//If sortby is not given then use _id as sort order
if (Utilities.isEmpty(sortby)) {
sortby = "_id";
}
//If order direction is not given then use old item to display first
if (Utilities.isEmpty(order)) {
order = -1; //note, -1 means to display old item at last and +1 means to display old item at top
} else {
order = parseInt(order);
}
log.debug({collection: collection}, "Finally doing sort by " + sortby + " and order as " + order);
var sortObj = {};
sortObj[sortby] = order;
//Filter deleted document if any
DBModule.db.collection(collection).find({isDeleted: {$ne: true}}, {password: 0, temppassword: 0}).sort(sortObj).toArray(function (err, docuemnts) {
if (!err) {
/callback(null, docuemnts);
} else {
callback(err, null);
}
});
}
};
I am surprised and confused bcz of difference in behavior when run one test file and when run all test file together.
Please help.
Aucun commentaire:
Enregistrer un commentaire