Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 2b97854

Browse files
DavidSoutherpetebacondarwin
authored andcommitted
feat(ngMock/$exceptionHandler): log errors when rethrowing
Now the `rethrow` mode will also record a log of the error in the same way as the `log` mode. Closes #10540 Closes #10564
1 parent 316ee8f commit 2b97854

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/ngMock/angular-mocks.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -243,31 +243,31 @@ angular.mock.$ExceptionHandlerProvider = function() {
243243
*
244244
* @param {string} mode Mode of operation, defaults to `rethrow`.
245245
*
246-
* - `rethrow`: If any errors are passed to the handler in tests, it typically means that there
247-
* is a bug in the application or test, so this mock will make these tests fail.
248246
* - `log`: Sometimes it is desirable to test that an error is thrown, for this case the `log`
249247
* mode stores an array of errors in `$exceptionHandler.errors`, to allow later
250248
* assertion of them. See {@link ngMock.$log#assertEmpty assertEmpty()} and
251249
* {@link ngMock.$log#reset reset()}
250+
* - `rethrow`: If any errors are passed to the handler in tests, it typically means that there
251+
* is a bug in the application or test, so this mock will make these tests fail.
252+
* For any implementations that expect exceptions to be thrown, the `rethrow` mode
253+
* will also maintain a log of thrown errors.
252254
*/
253255
this.mode = function(mode) {
256+
254257
switch (mode) {
255-
case 'rethrow':
256-
handler = function(e) {
257-
throw e;
258-
};
259-
break;
260258
case 'log':
259+
case 'rethrow':
261260
var errors = [];
262-
263261
handler = function(e) {
264262
if (arguments.length == 1) {
265263
errors.push(e);
266264
} else {
267265
errors.push([].slice.call(arguments, 0));
268266
}
267+
if (mode === "rethrow") {
268+
throw e;
269+
}
269270
};
270-
271271
handler.errors = errors;
272272
break;
273273
default:

test/ngMock/angular-mocksSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,19 @@ describe('ngMock', function() {
605605
});
606606
});
607607

608+
it('should log and rethrow exceptions', function() {
609+
module(function($exceptionHandlerProvider) {
610+
$exceptionHandlerProvider.mode('rethrow');
611+
});
612+
inject(function($exceptionHandler) {
613+
expect(function() { $exceptionHandler('MyError'); }).toThrow('MyError');
614+
expect($exceptionHandler.errors).toEqual(['MyError']);
615+
616+
expect(function() { $exceptionHandler('MyError', 'comment'); }).toThrow('MyError');
617+
expect($exceptionHandler.errors[1]).toEqual(['MyError', 'comment']);
618+
});
619+
});
620+
608621
it('should throw on wrong argument', function() {
609622
module(function($exceptionHandlerProvider) {
610623
expect(function() {

0 commit comments

Comments
 (0)