Last active
November 24, 2016 05:06
-
-
Save voltrevo/24b9bde34e045babe66937596990eba4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '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); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '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