Skip to content

Commit

Permalink
Tests WIP 36
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jul 10, 2016
1 parent cd6dabd commit 2157441
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 48 deletions.
44 changes: 44 additions & 0 deletions test/support/catchRejections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* cls-bluebird tests
* Add handler for unhandled rejections.
* Registers handler for unhanded rejections and adds mocha `after` block to throw if any encountered.
*/

/* global after */

// Exports

var unhandledRejectionHandlerApplied = false;

module.exports = function() {
// Exit if handler already registered
if (unhandledRejectionHandlerApplied) return;

// Add handler
var uncaughtErrors = [],
uncaughtPromises = [],
errorId = 1;

process.on('unhandledRejection', function(err, promise) {
console.log('Uncaught rejection (ID ' + errorId + '): ' + (err instanceof Error ? err.stack : err));
err.__id = errorId;
errorId++;

uncaughtErrors.push(err);
uncaughtPromises.push(promise);
});

process.on('rejectionHandled', function(promise) {
var index = uncaughtPromises.indexOf(promise);
if (index !== -1) {
var err = uncaughtErrors[index];
console.log('Handled rejection ID ' + err.__id);
}
});

after(function() {
if (uncaughtErrors.length) throw new Error('Uncaught rejections (' + uncaughtErrors.length + ')');
});

unhandledRejectionHandlerApplied = true;
};
38 changes: 17 additions & 21 deletions test/support/describeSets.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

/* global describe */

// Modules
var _ = require('lodash');

// Exports
module.exports = {
/**
Expand Down Expand Up @@ -90,6 +93,7 @@ module.exports = {
* @param {Function} testFn - Function to call for each `describe`. Called with `makePromise` function.
* @param {Object} [options] - Options object
* @param {boolean} [options.noUndefined=false] - true if method does not accept undefined value
* @param {boolean} [options.suppressRejections=false] - true to suppress unhandled rejections
* @returns {undefined}
*/
describeArrays: function(testFn, options) {
Expand All @@ -107,6 +111,13 @@ module.exports = {
var makeArray = function() {
var value = [makeValue(), makeValue(), makeValue()];
u.inheritRejectStatus(value, makeValue);

if (options.suppressRejections && u.getRejectStatus(makeValue)) {
value.forEach(function(p) {
u.suppressUnhandledRejections(p);
});
}

return value;
};
u.inheritRejectStatus(makeArray, makeValue);
Expand Down Expand Up @@ -150,15 +161,12 @@ module.exports = {

describe('with', function() {
u.describeArrays(function(makeArray) {
// TODO Remove this wrapping once issue with unhandled rejections is solved
makeArray = wrapMakeArrayToFixUnhandledRejections(makeArray, makeValue, u);

var makePromise = function() {
return makeValue(makeArray);
};

testFn(makePromise);
}, options);
}, _.defaults({suppressRejections: true}, options));
});
}, u.Promise, {continues: true, catches: true});
},
Expand Down Expand Up @@ -195,16 +203,13 @@ module.exports = {
if (makeValue === u.makeUndefined || u.getRejectStatus(makeValue)) return testFn(makeValue);

u.describeArrays(function(makeArray) {
// TODO Remove this wrapping once issue with unhandled rejections is solved
makeArray = wrapMakeArrayToFixUnhandledRejections(makeArray, makeValue, u);

var makePromise = function() {
return makeValue(makeArray);
};
u.inheritRejectStatus(makePromise, makeArray);

testFn(makePromise);
}, options);
}, _.defaults({suppressRejections: true}, options));
}, options);
},

Expand Down Expand Up @@ -334,17 +339,6 @@ module.exports = {
// If promise chaining onto is rejecting, suppress unhandled rejections
if (u.getRejectStatus(p)) u.suppressUnhandledRejections(p);

// If promise chaining onto resolves to array,
// suppress unhandled rejections on any promises in the array.
if (p.isFulfilled && p.isFulfilled()) {
var value = p.value();
if (Array.isArray(value)) {
value.forEach(function(item) {
if (isPromise(item)) u.suppressUnhandledRejections(item);
});
}
}

// Await promise's resolution before calling back
u.awaitPromise(p, fn);
});
Expand All @@ -356,9 +350,10 @@ module.exports = {
* All code below relates to working around bug in bluebird where unhandled rejections are thrown
* inncorrectly in some cases.
* https://github.com/petkaantonov/bluebird/issues/1158
* TODO Remove this once issue with unhandled rejections is solved
* No longer used - all rejected promises in arrays are suppressed from becoming unhandled rejections
* TODO Remove this!
*/

/*
function wrapMakeArrayToFixUnhandledRejections(makeArray, makeValue, u) {
var makeArrayWrapped = function() {
var array = makeArray();
Expand Down Expand Up @@ -425,3 +420,4 @@ function isBluebird2Ctor(Promise) {
if (!isBluebirdCtor(Promise)) return false;
return Promise.version.slice(0, 2) === '2.';
}
*/
32 changes: 8 additions & 24 deletions test/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
// Modules
var Bluebird2 = require('bluebird2'),
Bluebird3 = require('bluebird3'),
clsBluebird = require('../../lib'),
mochaShim = require('./mochaShim');
clsBluebird = require('../../lib');

// Imports
var ns = require('./ns'),
Utils = require('./utils');
Utils = require('./utils'),
mochaShim = require('./mochaShim'),
catchRejections = require('./catchRejections');

// Get bluebird version to test from environment vars
var bluebirdVersion = process.env.BLUEBIRD_VERSION * 1;
Expand All @@ -22,10 +23,6 @@ var bluebirdVersion = process.env.BLUEBIRD_VERSION * 1;
var PatchedBluebird2 = patch(Bluebird2);
var PatchedBluebird3 = patch(Bluebird3);

// Add unhandled rejection handlers to alternative bluebird version
addUnhandledRejectionHandler(Bluebird2);
addUnhandledRejectionHandler(Bluebird3);

// Get bluebird version to use for these tests
var Promise, UnpatchedPromise, versionName, altPromises;
if (bluebirdVersion === 2) {
Expand Down Expand Up @@ -70,7 +67,10 @@ var utils = new Utils(Promise, UnpatchedPromise, ns, altPromises, bluebirdVersio
*/
module.exports = function(name, testFn) {
// Apply mocha shim to collapse single `it` statements into parent `describe`
if (!describe.__shimmed) mochaShim();
mochaShim();

// Catch unhandled rejections
catchRejections();

// Run tests
describe(name + ' (' + versionName + ')', function() {
Expand All @@ -92,22 +92,6 @@ function patch(Promise) {
// Patch bluebird with cls-bluebird
clsBluebird(ns, Promise);

// Add unhandled rejection handler
addUnhandledRejectionHandler(Promise);

// Return bluebird constructor
return Promise;
}

/*
* Add unhandled rejection handler on this instance of Bluebird.
* Process will exit with error if there is an unhandled promise rejection.
* @param {Function} Promise - bluebird constructor
* @returns {undefined}
*/
function addUnhandledRejectionHandler(Promise) {
Promise.onPossiblyUnhandledRejection(function(err) {
console.log('Unhandled rejection:', err);
process.exit(1);
});
}
5 changes: 4 additions & 1 deletion test/support/mochaShim.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* cls-bluebird tests
* Shim mocha to treat `describe` with single `it` inside as an `it`.
* i.e. `describe('foo', function() { it('bar', function() {}); })`
* is treated as `it('foo bar', function() {})`
* is treated as `it('foo bar', function() {})`.
*/

/* global describe, it, before, beforeEach, after, afterEach */
Expand All @@ -23,6 +23,9 @@ var originals = {
// Exports

module.exports = function() {
// Exit if methods already shimmed
if (describe.__shimmed) return;

// Init call tracker var
var calls = null;

Expand Down
8 changes: 6 additions & 2 deletions test/support/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,16 @@ Utils.prototype = {
},

/**
* Attach empty catch handler to promise to prevent unhandled rejections
* Attach empty catch handler to promise to prevent unhandled rejections.
* Only catches test errors - all other errors are re-thrown.
* @param {Promise} promise - Promise to attach catch handler to
* @returns {undefined}
*/
suppressUnhandledRejections: function(promise) {
promise.catch(function() {});
var u = this;
promise.catch(function(err) {
if (!(err instanceof u.TestError)) throw err;
});
}
};

Expand Down

0 comments on commit 2157441

Please sign in to comment.