Angular + Mocha memory leak

后端 未结 3 1828
失恋的感觉
失恋的感觉 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:15

    I don't see you cleaning up your mocked http backend. I have no idea how it functions, but I would expect that all your requests and responses are being stored in memory. That would cause it to increase gradually, test by test.

    If this was using Sinon I would expect that you would create a stub/spy before the tests and clean it up afterwards (or run it in a sandbox).

    Try taking a heap snapshot before and after running a test and diff the snapshots to find which objects keep increasing in numbers.

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2021-01-13 09:30

    Not sure but this could be related to the memory leak in Mocha that has been fixed recently. Make sure to update your local mocha to 2.4.5 or newer to get the fix and try running the test.

    0 讨论(0)
提交回复
热议问题