ARTICLE AD BOX
I have a confusing issue with karma tests - once in hundreds of runs my tests fail with Chrome Headless 141.0.0.0 (Linux 0.0.0) RangeError: Maximum call stack size exceeded at Array.sort (<anonymous>). I have a recursive function in JS controller (React app). The idea is described below, the same as the tests. The problem is almost unreproducible locally, in Jenkins builds it happens a bit more often, but still rarely.
Maybe I messed something up in tests, as the app is running smoothly - no problems with recursion even on frequent resizes, no such error message. Do you have any ideas why this is happening? My best guess was maybe to reduce the
jasmine.Clock.tick(1010);to 210, but still, I can't even check if I'm right due to the rareness of this situation.
Code example
renderFunction: function() { ... this._resizeFix(); } _resizeFix: function() { var self = this; if (this._fixApplied) return; var isInProgress = false; var waitTimer; $(window).off("resize.myElement").on("resize.myElement", function() { if (isInProgress) return; clearTimeout(waitTimer); ... waitTimer = setTimeout(function() { isInProgress = true; self.renderFunction(); ... setTimeout(function() { isInProgress = false; }, 1000); }, 200); }); this._fixApplied = true; }Tests example (sometimes the first is failing, sometimes the second one):
describe('resize processing', function() { var controller; beforeEach(function() { $(window).off("resize.myElement"); jasmine.Clock.useMock(); controller = createMockController(); if (controller) { controller._fixApplied = false; } }); afterEach(function() { $(window).off("resize.myElement"); if (controller) { controller._fixApplied = false; } }); it("should not rerender during resize fix too frequently", function() { spyOn(controller, 'renderFunction'); controller._resizeFix(); for (var i = 0; i < 5; i++) { $(window).trigger('resize'); } jasmine.Clock.tick(1010); expect(controller.renderFunction.calls.length).toEqual(1); }); it("resize fix should be called", function() { spyOn(controller, '_resizeFix'); controller.renderFunction(); expect(controller._resizeFix.calls.length).toEqual(1); }); });