user3728233 user3728233 - 1 month ago 19
Node.js Question

Node.js/Javascript: Failing mocha test with timeout on testing parsing XML to JSON

Can someone please help me figure out why am I failing the mocha test even after setting the timeout to 15000ms. I know it can be a memory leak etc but I cannot find it anywhere in my code. I used the chrome debugger and checked that the JS Heap uses 48MB memory at the max. (which is not too bad?) Here's the function I'm testing, it's in a file called

xmlParser.js


var fs = require('fs'),
xml2js = require('xml2js'),
parser = new xml2js.Parser();

/**
* [parse - parses a xml file to json]
* @param {[string]} filename [name of the file to parse]
* @param {Function} callback [callback function]
* @return {[Function]} [returning callback function]
*/
function parse(filename, callback){
fs.readFile(filename, function(err, data){
if(err){
console.log('cannot read file.');
return callback(err);
} else {
parser.parseString(data, function(err, result){
if(err) {
console.log('cannot parse file.');
return callback(err);
}
else {
return callback(null, result);
}
});
}
});
}

module.exports = {
parse
}


What does it do? Parses a xml file to JSON. As simple as that!
Here's how I'm testing it,

var assert = require('chai').assert,
mocha = require('mocha'),
xmlParser = require('../utils/xmlParser.js'),
extractInformation = require('../utils/extractInformation.js');

//data-structures for parsed XML data (lists)
var logJSON = [], listJSON = [];

describe('parse xml files', function(){
this.timeout(150000);
it('should parse correctly', function(done){
this.timeout(150000);
setTimeout(done, 150000);
xmlParser.parse(__dirname + "/../xml/svn_log_test.xml", function(err, log) {
if(err) {
return done(err);
// assert.equal(true, false);
}
this.logJSON = log["log"]["logentry"];
xmlParser.parse(__dirname + "/../xml/svn_list_test.xml", function(err, list) {
if(err) {
return done(err);
// assert.equal(true, false);
}
this.listJSON = list["lists"]["list"][0]["entry"];
});
});
});
});


Now, before you say that I'm not using any assert statements as of now, yes I'm not. But it should pass anyway, right? It's not passing, it gets a timeout error after 15000s

Answer

Why don't you try to decrease the setTimout to 14 seconds?

setTimeout(function () {
    done()
}, 140000);

The timers are not quite precise in javascript