Angular + Mocha memory leak

后端 未结 3 1829
失恋的感觉
失恋的感觉 2021-01-13 08:49

I\'m getting the following error when running the entire suite of tests:

timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-13 09:26

    It looks like ui-router leaks memory in unit tests. I added the following code in the run block of my application:

    $rootScope.$on('$destroy', function() {
        var clearAllMembers = function(obj, depth) { // prevent infinite recursion because of circular references
            depth = depth || 0;
            if (depth > 10) {
                return;
            }
            for (var i in obj) {
                if (obj.hasOwnProperty(i)) {
                    if (typeof(obj[i]) == 'object') {
                        clearAllMembers(obj[i], ++depth);
                    }
                    if (obj) {
                        if (obj[i] && (obj[i] instanceof Array) && obj[i].length) {
                            obj[i].splice(0);
                        }
                        delete obj[i];
                    }
                }
            }
        }
    
        setTimeout(function() {
            clearAllMembers($stateRegistry); // ui-router 1.0+
            clearAllMembers($urlRouter);
            clearAllMembers($urlService); // ui-router 1.0+
            clearAllMembers($state);
        });
    });
    

    and the memory consumption in unit tests is 7-8 times smaller (I have around 350 states).

提交回复
热议问题