From b8ea7f6aba2e675b85826b0bee1f21ddd7b866a5 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Fri, 24 May 2013 11:00:14 -0700 Subject: feat(ngError): add error message compression and better error messages - add toThrowNg matcher --- test/AngularSpec.js | 16 ++++---- test/BinderSpec.js | 2 +- test/auto/injectorSpec.js | 31 +++++++-------- test/loaderSpec.js | 2 +- test/matchers.js | 6 ++- test/ng/animatorSpec.js | 2 +- test/ng/cacheFactorySpec.js | 2 +- test/ng/compileSpec.js | 41 ++++++++++++++------ test/ng/controllerSpec.js | 10 +++++ test/ng/directive/inputSpec.js | 14 +++---- test/ng/directive/ngRepeatSpec.js | 14 +++---- test/ng/directive/selectSpec.js | 4 +- test/ng/interpolateSpec.js | 4 +- test/ng/locationSpec.js | 8 ++-- test/ng/parseSpec.js | 10 ++--- test/ng/rootScopeSpec.js | 12 +++--- test/ngErrorSpec.js | 80 +++++++++++++++++++++++++++++++++++++++ 17 files changed, 187 insertions(+), 71 deletions(-) create mode 100644 test/ngErrorSpec.js (limited to 'test') diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 0e5017ad..de029623 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -84,19 +84,21 @@ describe('angular', function() { }); it('should throw an exception if a Scope is being copied', inject(function($rootScope) { - expect(function() { copy($rootScope.$new()); }).toThrow("Can't copy Window or Scope"); + expect(function() { copy($rootScope.$new()); }). + toThrow("[NgErr43] Can't copy! Making copies of Window or Scope instances is not supported."); })); it('should throw an exception if a Window is being copied', function() { - expect(function() { copy(window); }).toThrow("Can't copy Window or Scope"); + expect(function() { copy(window); }). + toThrow("[NgErr43] Can't copy! Making copies of Window or Scope instances is not supported."); }); it('should throw an exception when source and destination are equivalent', function() { var src, dst; src = dst = {key: 'value'}; - expect(function() { copy(src, dst); }).toThrow("Can't copy equivalent objects or arrays"); + expect(function() { copy(src, dst); }).toThrow("[NgErr44] Can't copy! Source and destination are identical."); src = dst = [2, 4]; - expect(function() { copy(src, dst); }).toThrow("Can't copy equivalent objects or arrays"); + expect(function() { copy(src, dst); }).toThrow("[NgErr44] Can't copy! Source and destination are identical."); }); it('should not copy the private $$hashKey', function() { @@ -580,7 +582,7 @@ describe('angular', function() { expect(function() { angularInit(appElement, bootstrap); - }).toThrow('No module: doesntexist'); + }).toThrow("[NgErr47] Module 'doesntexist' is not available! You either misspelled the module name or forgot to load it."); }); }); @@ -724,7 +726,7 @@ describe('angular', function() { expect(function() { angular.bootstrap(element, ['doesntexist']); - }).toThrow('No module: doesntexist'); + }).toThrow("[NgErr47] Module 'doesntexist' is not available! You either misspelled the module name or forgot to load it."); expect(element.html()).toBe('{{1+2}}'); dealoc(element); @@ -783,7 +785,7 @@ describe('angular', function() { expect(function() { element.injector().get('foo'); - }).toThrow('Unknown provider: fooProvider <- foo'); + }).toThrow('[NgErr1] Unknown provider: fooProvider <- foo'); expect(element.injector().get('$http')).toBeDefined(); }); diff --git a/test/BinderSpec.js b/test/BinderSpec.js index b6a7a00d..2a1b205d 100644 --- a/test/BinderSpec.js +++ b/test/BinderSpec.js @@ -175,7 +175,7 @@ describe('Binder', function() { $rootScope.error['throw'] = function() {throw 'MyError';}; errorLogs.length = 0; $rootScope.$apply(); - expect(errorLogs.shift().message).toBe('Error while interpolating: {{error.throw()}}\nMyError'); + expect(errorLogs.shift().message).toBe("[NgErr48] $interpolate error! Can't interpolate: {{error.throw()}}\nMyError"); $rootScope.error['throw'] = function() {return 'ok';}; $rootScope.$apply(); diff --git a/test/auto/injectorSpec.js b/test/auto/injectorSpec.js index 8fd03be4..b59a344f 100644 --- a/test/auto/injectorSpec.js +++ b/test/auto/injectorSpec.js @@ -70,7 +70,7 @@ describe('injector', function() { it('should provide useful message if no provider', function() { expect(function() { injector.get('idontexist'); - }).toThrow("Unknown provider: idontexistProvider <- idontexist"); + }).toThrow("[NgErr1] Unknown provider: idontexistProvider <- idontexist"); }); @@ -79,7 +79,7 @@ describe('injector', function() { providers('b', function(a) {return 2;}); expect(function() { injector.get('b'); - }).toThrow("Unknown provider: idontexistProvider <- idontexist <- a <- b"); + }).toThrow("[NgErr1] Unknown provider: idontexistProvider <- idontexist <- a <- b"); }); @@ -127,10 +127,10 @@ describe('injector', function() { it('should fail with errors if not function or array', function() { expect(function() { injector.invoke({}); - }).toThrow("Argument 'fn' is not a function, got Object"); + }).toThrow("[NgErr45] Argument 'fn' is not a function, got Object"); expect(function() { injector.invoke(['a', 123], {}); - }).toThrow("Argument 'fn' is not a function, got number"); + }).toThrow("[NgErr45] Argument 'fn' is not a function, got number"); }); }); @@ -268,7 +268,8 @@ describe('injector', function() { it('should error on invalid module name', function() { expect(function() { createInjector(['IDontExist'], {}); - }).toThrow("No module: IDontExist"); + }).toThrow("[NgErr47] Module 'IDontExist' is not available! You either misspelled the module name or forgot to load it."); + }); @@ -551,7 +552,7 @@ describe('injector', function() { createInjector([ {} ], {}); - }).toThrow("Argument 'module' is not a function, got Object"); + }).toThrow("[NgErr45] Argument 'module' is not a function, got Object"); }); @@ -568,7 +569,7 @@ describe('injector', function() { angular.module('TestModule', [], function(xyzzy) {}); expect(function() { createInjector(['TestModule']); - }).toThrow('Unknown provider: xyzzy from TestModule'); + }).toThrow('[NgErr1] Unknown provider: xyzzy from TestModule'); }); @@ -576,7 +577,7 @@ describe('injector', function() { function myModule(xyzzy){} expect(function() { createInjector([myModule]); - }).toThrow('Unknown provider: xyzzy from ' + myModule); + }).toThrow('[NgErr1] Unknown provider: xyzzy from ' + myModule); }); @@ -584,7 +585,7 @@ describe('injector', function() { function myModule(xyzzy){} expect(function() { createInjector([['xyzzy', myModule]]); - }).toThrow('Unknown provider: xyzzy from ' + myModule); + }).toThrow('[NgErr1] Unknown provider: xyzzy from ' + myModule); }); @@ -594,7 +595,7 @@ describe('injector', function() { $provide.factory('service', function(service){}); return function(service) {} }]) - }).toThrow('Circular dependency: service'); + }).toThrow("[NgErr4] Circular dependency found: service"); }); @@ -605,7 +606,7 @@ describe('injector', function() { $provide.factory('b', function(a){}); return function(a) {} }]) - }).toThrow('Circular dependency: b <- a'); + }).toThrow('[NgErr4] Circular dependency found: b <- a'); }); }); }); @@ -695,7 +696,7 @@ describe('injector', function() { it('should throw usefull error on wrong argument type]', function() { expect(function() { $injector.invoke({}); - }).toThrow("Argument 'fn' is not a function, got Object"); + }).toThrow("[NgErr45] Argument 'fn' is not a function, got Object"); }); }); @@ -782,7 +783,7 @@ describe('injector', function() { }]); expect(function() { $injector.get('nameProvider'); - }).toThrow("Unknown provider: nameProviderProvider <- nameProvider"); + }).toThrow("[NgErr1] Unknown provider: nameProviderProvider <- nameProvider"); }); @@ -790,7 +791,7 @@ describe('injector', function() { var $injector = createInjector([]); expect(function() { $injector.get('$provide').value('a', 'b'); - }).toThrow("Unknown provider: $provideProvider <- $provide"); + }).toThrow("[NgErr1] Unknown provider: $provideProvider <- $provide"); }); @@ -800,7 +801,7 @@ describe('injector', function() { createInjector([function($provide) { $provide.value('name', 'angular') }, instanceLookupInModule]); - }).toThrow('Unknown provider: name from ' + String(instanceLookupInModule)); + }).toThrow('[NgErr1] Unknown provider: name from ' + String(instanceLookupInModule)); }); }); }); diff --git a/test/loaderSpec.js b/test/loaderSpec.js index b2341a71..802d5c1d 100644 --- a/test/loaderSpec.js +++ b/test/loaderSpec.js @@ -68,6 +68,6 @@ describe('module loader', function() { it('should complain of no module', function() { expect(function() { window.angular.module('dontExist'); - }).toThrow('No module: dontExist'); + }).toThrow("[NgErr47] Module 'dontExist' is not available! You either misspelled the module name or forgot to load it."); }); }); diff --git a/test/matchers.js b/test/matchers.js index 8e4be118..13d284e6 100644 --- a/test/matchers.js +++ b/test/matchers.js @@ -147,8 +147,12 @@ beforeEach(function() { return this.actual.hasClass ? this.actual.hasClass(clazz) : angular.element(this.actual).hasClass(clazz); - } + }, + toThrowNg: function(expected) { + return jasmine.Matchers.prototype.toThrow.call(this, new RegExp('\\[NgErr\\d*\\] ' + + expected.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"))); + } }); }); diff --git a/test/ng/animatorSpec.js b/test/ng/animatorSpec.js index d4d7c0ec..cf5667d2 100644 --- a/test/ng/animatorSpec.js +++ b/test/ng/animatorSpec.js @@ -719,6 +719,6 @@ describe("$animator", function() { expect(function() { var animate = $animator($rootScope, { ngAnimate: ':' }); animate.enter(); - }).toThrow("Syntax Error: Token ':' not a primary expression at column 1 of the expression [:] starting at [:]."); + }).toThrow("[NgErr24] Syntax Error: Token ':' not a primary expression at column 1 of the expression [:] starting at [:]."); })); }); diff --git a/test/ng/cacheFactorySpec.js b/test/ng/cacheFactorySpec.js index ddfadbbc..4b15fd7b 100644 --- a/test/ng/cacheFactorySpec.js +++ b/test/ng/cacheFactorySpec.js @@ -15,7 +15,7 @@ describe('$cacheFactory', function() { it('should complain if the cache id is being reused', inject(function($cacheFactory) { $cacheFactory('cache1'); expect(function() { $cacheFactory('cache1'); }). - toThrow('cacheId cache1 taken'); + toThrow("[NgErr10] CacheId 'cache1' is already taken!"); })); diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 26f61357..bf3d0b77 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -632,11 +632,11 @@ describe('$compile', function() { inject(function($compile) { expect(function() { $compile('
'); - }).toThrow('Template must have exactly one root element. was: dada'); + }).toThrow("[NgErr12] Template for directive 'noRootElem' must have exactly one root element."); expect(function() { $compile(''); - }).toThrow('Template must have exactly one root element. was: '); + }).toThrow("[NgErr12] Template for directive 'multiRootElem' must have exactly one root element."); // ws is ok expect(function() { @@ -985,7 +985,7 @@ describe('$compile', function() { expect(function() { $httpBackend.flush(); - }).toThrow('Failed to load template: hello.html'); + }).toThrow('[NgErr17] Failed to load template: hello.html'); expect(sortedHtml(element)).toBe('