Skip to content

Instantly share code, notes, and snippets.

@voltrevo
Last active November 24, 2016 05:06
Show Gist options
  • Select an option

  • Save voltrevo/24b9bde34e045babe66937596990eba4 to your computer and use it in GitHub Desktop.

Select an option

Save voltrevo/24b9bde34e045babe66937596990eba4 to your computer and use it in GitHub Desktop.
'use strict';
var tickTestsToEnd = require('./tickTestsToEnd');
describe('tests', function() {
tickTestsToEnd(beforeEach, afterEach);
it('test', function(done) {
setTimeout(function() {
doLotsOfAsyncStuff().then(function(result) {
expect(result).toBe('expectedResult');
done();
});
}, 1000);
});
});
'use strict';
module.exports = function monitorPromises() {
bluebird.config({
monitoring: true
});
var eventSource = global.window || global.process;
var counter = 0;
var highestCounter = 0;
var done, callback;
var increment = function() {
counter++;
highestCounter = Math.max(counter, highestCounter);
};
var decrement = function() {
counter--;
if (counter === 0 && callback) {
done();
}
};
eventSource.addEventListener('promiseCreated', increment);
eventSource.addEventListener('promiseChained', increment);
eventSource.addEventListener('promiseResolved', decrement);
eventSource.addEventListener('promiseRejected', decrement);
var whenExhausted = function(cb) {
if (callback) {
throw new Error('callback already set');
}
callback = cb;
if (counter === 0) {
done();
}
};
done = function() {
eventSource.removeEventListener('promiseCreated', increment);
eventSource.removeEventListener('promiseChained', increment);
eventSource.removeEventListener('promiseResolved', decrement);
eventSource.removeEventListener('promiseRejected', decrement);
setTimeout(function() {
callback(highestCounter);
});
};
return whenExhausted;
}
'use strict';
var bluebird = require('bluebird');
module.exports = function tickTestsToEnd(installHook, uninstallHook) {
var testIndex = 0;
installHook(function() {
var currTestIndex = testIndex;
jasmine.clock().install();
var loop = function() {
if (testIndex !== currTestIndex) {
return;
}
jasmine.clock().tick(Infinity);
bluebird.resolve().then(loop);
};
loop();
});
uninstallHook(function() {
testIndex++;
jasmine.clock().uninstall();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment