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

Commit b75c0d8

Browse files
andyroogerpetebacondarwin
authored andcommitted
feat(ngMock): destroy $rootScope after each test
Previously $rootScope would be new for each test, but old $rootScopes would never be destroyed. Now that we are able to destroy the $rootScope, doing so provides an opportunity for code to clean up things like long-lived event handlers between tests. Closes #13433
1 parent df6fade commit b75c0d8

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/ngMock/angular-mocks.js

+1
Original file line numberDiff line numberDiff line change
@@ -2480,6 +2480,7 @@ if (window.jasmine || window.mocha) {
24802480

24812481
if (injector) {
24822482
injector.get('$rootElement').off();
2483+
injector.get('$rootScope').$destroy();
24832484
}
24842485

24852486
// clean up jquery's fragment cache

test/ngMock/angular-mocksSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,25 @@ describe('ngMock', function() {
16501650
});
16511651

16521652

1653+
describe('$rootScope', function() {
1654+
var destroyed = false;
1655+
var oldRootScope;
1656+
1657+
it('should destroy $rootScope after each test', inject(function($rootScope) {
1658+
$rootScope.$on('$destroy', function() {
1659+
destroyed = true;
1660+
});
1661+
oldRootScope = $rootScope;
1662+
}));
1663+
1664+
it('should have destroyed the $rootScope from the previous test', inject(function($rootScope) {
1665+
expect(destroyed).toBe(true);
1666+
expect($rootScope).not.toBe(oldRootScope);
1667+
expect(oldRootScope.$$destroyed).toBe(true);
1668+
}));
1669+
});
1670+
1671+
16531672
describe('$rootScopeDecorator', function() {
16541673

16551674
describe('$countChildScopes', function() {

0 commit comments

Comments
 (0)