question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

$rootScope reference has been destroyed by angular-mocks

See original GitHub issue

TLDR;

Unit tests don’t run ngOnChanges correctly if they are run after another test because the $rootScope reference was destroy.

Details

In my unit tests I am using the $compile service to create my component and check the resulting DOM. After each test angular-mocks destroys the $rootScope service to clean up the test (introduced in angular/angular.js#13433). However, angular does not invoke the directiveControllerFactory again so when future tests run $apply() in the _flushOnChangesQueue() function, it is calling a noop. This is evidenced by the ngOnChanges() not be called again.

When debugging this issue, I can run two copies of the same exact test and the first instance would pass and the second would fail. I will try creating an example unit test to demonstrate.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
otijhuiscommented, Apr 3, 2017

We’re running into the same problem. For now we’re working around it by doing $rootScope.$destroy = () => angular.noop();. Not the best solution but at least it saves us from writing specific test code to work around the issue.

1reaction
Hotellcommented, Oct 15, 2016

sorry was busy with non Angular things lately. Need take a look. Sry for delay @aciccarello

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit testing AngularJS Directives with Jest - Stack Overflow
Use the global version of `angular` that has been augmented by angular-mocks. */ var angular = window.angular; export var app ...
Read more >
$rootScope.Scope - AngularJS: API
$destroy() must be called on a scope when it is desired for the scope and its child scopes to be permanently detached from...
Read more >
angular-mocks JavaScript and Node.js code examples | Tabnine
Best JavaScript code snippets using angular-mocks(Showing top 15 results out of 315) ... cache references $compile = _$compile_; $rootScope = _$rootScope_; ...
Read more >
Unit testing AngularJS applications - AirPair
npm install angular-mocks --save-dev ... Angular can be tested using any JavaScript unit testing framework out there, but Jasmine is ...
Read more >
ng-scope-aware - npm
'node_modules/angular-mocks/angular-mocks.js', ... Safari (only Mac; has to be installed with `npm install karma-safari-launcher`).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found