mardi 27 janvier 2015

Assert Is Breaking Async Function in Mocha Test

I'm building a node module and am trying to do my best to unit test the heck out of it. I've setup mocha and chai to do the test handling. I'm having a problem testing my async methods (methods that return promises).


In the following test I am testing a method on an "Upgrade" object.



it('Should return a list of versions for the default git repo', function (done) {
fs.writeFileSync(appSetup.CONFIG_FILENAME, JSON.stringify(appSetup.DEFAULT_CONFIG));

var upgrade = new Upgrade({
quiet: true
});

upgrade.getVersions().then(function (versions) {
assert(versions && versions.length > 0, 'Should have at least one version.');
assert.equal(1, 2); // this throws the exception which causes the test case not even exist
done();
}, done);
});


The getVersions() call returns a promise as the method is async. When the promise resolves I want to test the value returned in the versions variable.


The assert(versions && versions.length > 0, 'Should have at least one version.'); is the actual test. I added assert.equal(1, 2); because I noticed that when the test should fail the test case wouldn't even show up in the test list.


I'm assuming that the assert call is throwing an exception that Mocha should pickup. However it is getting trapped in the promises then handler function.


What is going on here? Why when the assert is going to fail in that method does it not show the test case in the list (it doesn't show as failed; like it doesn't exist)?


Aucun commentaire:

Enregistrer un commentaire