简体   繁体   English

如何在每个业力/茉莉花测试文件的开头引导角度?

[英]How to bootstrap angular at the start of each karma/jasmine test file?

Is it possible to have angular bootstrap the app at the beginning of each test file? 是否可以在每个测试文件的开头使用角度引导应用程序? Instead of the current behavior, which has the app bootstrap at the beginning of the series of tests, and then the same instance is used throughout all the test files (we have about 600 tests throughout ~60 files right now)? 而不是当前行为,在一系列测试开始时有app bootstrap,然后在所有测试文件中使用相同的实例(我们现在大约有600个测试~60个文件)?

We have beforeEach statements to handle clean-up and that doesn't help. 我们之前有一些声明来处理清理工作并没有帮助。 In fact, seems sometimes the beforeEach statements are altogether skipped for no apparent reason (possible memory leak with test runner). 实际上,有时看起来有些beforeEach语句完全没有明显的原因(测试运行器可能存在内存泄漏)。

So the route I would like to take this is to have each test file bootstrap the angular app, so that the state is completely reset, instead of reusing dependency injection (ie. services) that had state set by a different test. 因此,我想采取的路线是让每个测试文件引导角度应用程序,以便状态完全重置,而不是重用具有由不同测试设置的状态的依赖注入(即服务)。

You don't need to bootstrap the app for the tests. 您无需为测试引导应用程序。 That's why we have angular-mock . 这就是为什么我们有angular-mock With angular.module('app.module') we load the module we need for our test and that module contains the component we want to test. 使用angular.module('app.module')我们加载了测试所需的模块,该模块包含我们要测试的组件。 Since angular-mock is not the cause for the memory leaks, there can be several reasons for them. 由于angular-mock不是内存泄漏的原因,因此可能有几个原因。 One of the most common reasons for memory leaks is jasmine itself and the way we usually write the tests. 内存泄漏的最常见原因之一是茉莉本身以及我们通常编写测试的方式。 The variables we use for the dependencies we inject in our test and are defined on the describe scope and cannot be collected by the GC when the tests finish. 我们在测试中注入的依赖项使用的变量是在describe范围内定义的,并且在测试完成时不能由GC收集。 This is because there are references of those variables in the it blocks that cannot be garbage collected because the variables still live in some other scope of the tests tree. 这是因为在对这些变量的引用it不能被垃圾收集,因为变量仍住在测试树的一些其他范围块。 Another problem can be the compiled elements that should also be cleaned up after each test. 另一个问题可能是在每次测试后也应该清理的编译元素。 So you probably need to clean-up the following things: 所以你可能需要清理以下内容:

  • compiled element when using $compile for testing directives 使用$ compile进行测试指令时编译的元素
  • all variables in the describe functions scope describe函数范围内的所有变量

You can do something like this: 你可以这样做:

 describe('testSuite', function () { var suite = {}; beforeEach(module('app')); beforeEach(inject(function ($rootScope, $compile, heavyLoad) { suite.$rootScope = $rootScope; suite.$compile = $compile; suite.heavyLoad = heavyLoad; suite.$scope = $rootScope.$new(); spyOn(suite.heavyLoad, 'getHeavyString').and.callThrough(); spyOn(suite.heavyLoad, 'getHeavyObject').and.callThrough(); spyOn(suite.heavyLoad, 'getHeavyList').and.callThrough(); })); // NOTE: cleanup afterEach(function () { // NOTE: prevents DOM elements leak suite.element.remove(); }); afterAll(function () { // NOTE: prevents memory leaks because of JavaScript closures created for // jasmine syntax (beforeEach, afterEach, beforeAll, afterAll, it..). suite = null; }); suite.compileDirective = function (template) { suite.element = suite.$compile(template)(suite.$scope); suite.directiveScope = suite.element.isolateScope(); suite.directiveController = suite.element.controller('heavyLoad'); }; it('should compile correctly', function () { // given var givenTemplate = '<div heavy-load></div>'; // when suite.compileDirective(givenTemplate); // then expect(suite.directiveScope.title).toBeDefined(); expect(suite.directiveScope.items).toBeDefined(); expect(suite.heavyLoad.getHeavyString).toHaveBeenCalled(); expect(suite.heavyLoad.getHeavyList).toHaveBeenCalled(); }); }); 

Taken from here . 取自这里

This should reduce your memory leaks significantly. 这应该可以显着减少内存泄漏。 You should also take a look at your module structure and the dependency graph your modules have, cause you might have some modules that are not needed for some tests but they are loaded anyway. 您还应该查看模块结构和模块所具有的依赖关系图,因为您可能拥有某些测试不需要的模块,但无论如何都会加载它们。 They can be heavy on memory or contain memory leaks in them and can make you additional problems. 它们可能会占用大量内存或包含内存泄漏,并可能使您遇到其他问题。 You can also take a look at this github project . 你也可以看看这个github项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM