diff --git a/test/executor.test.js b/test/executor.test.js index 0ad2827..575236b 100644 --- a/test/executor.test.js +++ b/test/executor.test.js @@ -38,8 +38,31 @@ function testThrowInCallback (d, done) { } +// Test that operations are executed in the right order +// We prevent Mocha from catching the exception we throw on purpose by remembering all current handlers, remove them and register them back after test ends +function testRightOrder (d, done) { + var currentUncaughtExceptionHandlers = process.listeners('uncaughtException'); - + process.removeAllListeners('uncaughtException'); + + process.on('uncaughtException', function (err) { + // Do nothing with the error which is only there to test we stay on track + }); + + d.find({}, function (err) { + process.nextTick(function () { + d.insert({ bar: 1 }, function (err) { + for (var i = 0; i < currentUncaughtExceptionHandlers.length; i += 1) { + process.on('uncaughtException', currentUncaughtExceptionHandlers[i]); + } + + done(); + }); + }); + + throw 'Some error'; + }); +} @@ -84,28 +107,14 @@ describe('Executor', function () { var d; beforeEach(function (done) { - d = new Datastore({ filename: testDb }); - d.filename.should.equal(testDb); - d.inMemoryOnly.should.equal(false); + d = new Datastore({ inMemoryOnly: true }); + d.inMemoryOnly.should.equal(true); - async.waterfall([ - function (cb) { - Persistence.ensureDirectoryExists(path.dirname(testDb), function () { - fs.exists(testDb, function (exists) { - if (exists) { - fs.unlink(testDb, cb); - } else { return cb(); } - }); - }); - } - , function (cb) { - d.loadDatabase(function (err) { - assert.isNull(err); - d.getAllData().length.should.equal(0); - return cb(); - }); - } - ], done); + d.loadDatabase(function (err) { + assert.isNull(err); + d.getAllData().length.should.equal(0); + return cb(); + }); }); it('A throw in a callback doesnt prevent execution of next operations', function(done) {