Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.

Commit 59f43a5

Browse files
committed
fix(service): fix missing promise rejection handlers
Credits for a GREAT debugging help via angular-ui/ui-router#2889 (comment)
1 parent ad7ab24 commit 59f43a5

5 files changed

+57
-14
lines changed

karma.conf.js

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ module.exports = function (config) {
146146
files : [
147147
injectByScope(scope, 'angular/angular.js'),
148148
injectByScope(scope, 'angular-mocks/angular-mocks.js'),
149+
'test/unit/util/unhandledRejectionTracing.js',
149150
'node_modules/babel-polyfill/browser.js',
150151
'test/unit/test_index.js',
151152
],

test/unit/binding/vertxEventBus.defaultHeaders.spec.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/* jshint camelcase: false, undef: true, unused: true, browser: true */
22
/* global module: false, describe: false, it: false, expect: false, beforeEach: false, inject: false, SockJS: false */
33

4-
var SockJS = require('sockjs-client');
4+
const SockJS = require('sockjs-client');
5+
const enableUnhandledRejectionTracing = require('../util/unhandledRejectionTracing.js');
56
require('../../../src/module.js');
67

78
describe('integration of module::vertxEventBus (defaultHeaders)', function () {
89

910
beforeEach(angular.mock.module('knalli.angular-vertxbus'));
1011

1112
beforeEach(angular.mock.module('knalli.angular-vertxbus', function ($provide) {
13+
enableUnhandledRejectionTracing(angular, $provide);
1214
$provide.value('$log', {
1315
log : function () {
1416
},
@@ -27,7 +29,7 @@ describe('integration of module::vertxEventBus (defaultHeaders)', function () {
2729

2830
var vertxEventBus, $timeout, $rootScope, $log;
2931

30-
beforeEach(angular.mock.module('knalli.angular-vertxbus', function (vertxEventBusProvider) {
32+
beforeEach(angular.mock.module('knalli.angular-vertxbus', function ($provide, vertxEventBusProvider) {
3133
// Override (improve test running time)
3234
vertxEventBusProvider.useDebug(true).useSockJsReconnectInterval(2000);
3335
}));
@@ -177,7 +179,8 @@ describe('integration of module::vertxEventBus (defaultHeaders)', function () {
177179

178180
var vertxEventBus, $timeout, $rootScope, $log;
179181

180-
beforeEach(angular.mock.module('knalli.angular-vertxbus', function (vertxEventBusProvider) {
182+
beforeEach(angular.mock.module('knalli.angular-vertxbus', function ($provide, vertxEventBusProvider) {
183+
enableUnhandledRejectionTracing(angular, $provide);
181184
// Override (improve test running time)
182185
vertxEventBusProvider.useDebug(true).useSockJsReconnectInterval(2000);
183186
}));

test/unit/binding/vertxEventBus.spec.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/* jshint camelcase: false, undef: true, unused: true, browser: true */
22
/* global module: false, describe: false, it: false, expect: false, beforeEach: false, inject: false, SockJS: false */
33

4-
var SockJS = require('sockjs-client');
4+
const SockJS = require('sockjs-client');
5+
const enableUnhandledRejectionTracing = require('../util/unhandledRejectionTracing.js');
56
require('../../../src/module.js');
67

78
describe('integration of module::vertxEventBus', function () {
89

910
beforeEach(angular.mock.module('knalli.angular-vertxbus'));
1011

1112
beforeEach(angular.mock.module('knalli.angular-vertxbus', function ($provide) {
13+
enableUnhandledRejectionTracing(angular, $provide);
1214
$provide.value('$log', {
1315
log: function () {},
1416
debug: function () {},

test/unit/binding/vertxEventBusService.spec.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/* jshint camelcase: false, undef: true, unused: true, browser: true */
22
/* global module: false, describe: false, it: false, expect: false, beforeEach: false, inject: false, SockJS: false */
33

4-
var SockJS = require('sockjs-client');
4+
const SockJS = require('sockjs-client');
5+
const enableUnhandledRejectionTracing = require('../util/unhandledRejectionTracing.js');
56
require('../../../src/module.js');
67

78
describe('integration of module::vertxEventBusService', function () {
89

910
beforeEach(angular.mock.module('knalli.angular-vertxbus'));
1011

1112
beforeEach(angular.mock.module('knalli.angular-vertxbus', function ($provide) {
13+
enableUnhandledRejectionTracing(angular, $provide);
1214
$provide.value('$log', {
1315
log: function () {},
1416
debug: function () {},
@@ -229,10 +231,10 @@ describe('integration of module::vertxEventBusService', function () {
229231
describe('should replay queued items', function () {
230232
it('when eventbus is reopened', function (done) {
231233
setTimeout(function () {
232-
vertxEventBusService.send('xyz', {data : 0});
233-
vertxEventBusService.send('xyz', {data : 1});
234-
vertxEventBusService.send('xyz', {data : 2});
235-
vertxEventBusService.send('xyz', {data : 3});
234+
vertxEventBusService.send('xyz', {data : 0}).then(null, angular.noop);
235+
vertxEventBusService.send('xyz', {data : 1}).then(null, angular.noop);
236+
vertxEventBusService.send('xyz', {data : 2}).then(null, angular.noop);
237+
vertxEventBusService.send('xyz', {data : 3}).then(null, angular.noop);
236238

237239
// fake connect
238240
vertxEventBus.readyState = function () {
@@ -562,13 +564,13 @@ describe('integration of module::vertxEventBusService', function () {
562564
expect(typeof promise.finally).to.be('function');
563565
promise.then(function () {
564566
results.then++;
565-
});
567+
}, angular.noop); // ignore error (because error is expected)
566568
promise.catch(function () {
567569
results.catch++;
568570
});
569571
promise.finally(function () {
570572
results.finally++;
571-
});
573+
}).then(null, angular.noop); // ignore error (because error is expected)
572574
$rootScope.$apply();
573575
setTimeout(function () {
574576
expect(results.then).to.be(0);
@@ -618,16 +620,16 @@ describe('integration of module::vertxEventBusService', function () {
618620
expect(typeof promise.finally).to.be('function');
619621
promise.then(function () {
620622
results.then++;
621-
});
623+
}, angular.noop); // ignore error (because error is expected)
622624
promise.catch(function () {
623625
results.catch++;
624626
});
625627
promise.finally(function () {
626628
results.finally++;
627-
});
629+
}).then(null, angular.noop); // ignore error (because error is expected)
628630
$rootScope.$apply();
629631
setTimeout(function () {
630-
window.console.log(results);
632+
window.console.warn(results);
631633
expect(results.then).to.be(0);
632634
expect(results.catch).to.be(1);
633635
expect(results.finally).to.be(1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// https://github.com/angular-ui/ui-router/issues/2889#issuecomment-273944742
2+
module.exports = function (angular, module) {
3+
4+
if (angular.version.minor < 3) {
5+
// will work w/ AngularJS 1.3+
6+
return;
7+
}
8+
9+
/* jshint ignore:start */
10+
// Decorate the $q service when app starts
11+
module.decorator('$q', ['$delegate', function ($delegate) {
12+
// Create a new promise object
13+
var promise = $delegate.when();
14+
15+
// Access the `Promise` prototype (nonstandard, but works in Chrome)
16+
var proto = promise.__proto__;
17+
18+
// Define a setter for `$$state` that creates a stacktrace
19+
// (string) and assigns it as a property of the internal `$$state` object.
20+
Object.defineProperty(proto, '$$state', {
21+
configurable : true,
22+
enumerable : true,
23+
set : function (val) {
24+
val.stack = new Error().stack;
25+
this._$$state = val;
26+
},
27+
get : function () {
28+
return this._$$state;
29+
}
30+
});
31+
32+
return $delegate;
33+
}]);
34+
/* jshint ignore:end */
35+
};

0 commit comments

Comments
 (0)