Skip to content

Commit 63db097

Browse files
committed
style(testability): throw a more informative error when getting testability
The angular.getTestability method requires an element parameter to determine which Angular application to use. Currently, if the element provided is undefined or outside of an Angular app, the error message is 'cannot read property get of undefined'. Improving to a more relevant error message.
1 parent a097aa9 commit 63db097

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

docs/content/error/ng/test.ngdoc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@ngdoc error
2+
@name ng:test
3+
@fullName Testability Not Found
4+
@description
5+
6+
Angular's testability helper, getTestability, requires a root element to be
7+
passed in. This helps differentiate between different Angular apps on the same
8+
page. This error is thrown when no injector is found for root element. It is
9+
often because the root element is outside of the ng-app.

src/Angular.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,12 @@ function reloadWithDebugInfo() {
14281428
* @param {DOMElement} element DOM element which is the root of angular application.
14291429
*/
14301430
function getTestability(rootElement) {
1431-
return angular.element(rootElement).injector().get('$$testability');
1431+
var injector = angular.element(rootElement).injector();
1432+
if (!injector) {
1433+
throw ngMinErr('test',
1434+
'no injector found for element argument to getTestability');
1435+
}
1436+
return injector.get('$$testability');
14321437
}
14331438

14341439
var SNAKE_CASE_REGEXP = /[A-Z]/g;

0 commit comments

Comments
 (0)