From bbe4137d817f51e0ae46cc071f57f7e55af64280 Mon Sep 17 00:00:00 2001 From: Eric Mc Sween Date: Wed, 3 Feb 2021 07:36:39 -0500 Subject: [PATCH] Merge pull request #3613 from overleaf/em-unit-tests-leaks Fix memory leaks in unit tests GitOrigin-RevId: e2b366dd9f277823db4c7c00c209da2b3774a0fe --- services/web/test/unit/bootstrap.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/services/web/test/unit/bootstrap.js b/services/web/test/unit/bootstrap.js index 8d254a2691..2bdf4744a4 100644 --- a/services/web/test/unit/bootstrap.js +++ b/services/web/test/unit/bootstrap.js @@ -52,3 +52,17 @@ LIBRARIES.forEach(lib => { SandboxedModule.configure({ requires: GLOBAL_REQUIRE_CACHE_FOR_SANDBOXED_MODULES }) + +// sandboxed-module somehow registers every fake module it creates in this +// module's children array, which uses quite a big amount of memory. We'll take +// a copy of the module children array and restore it after each test so that +// the garbage collector has a chance to reclaim the fake modules. +let initialModuleChildren +before('record initial module children', function() { + initialModuleChildren = module.children.slice() +}) + +afterEach('restore module children', function() { + // Delete leaking sandboxed modules + module.children = initialModuleChildren.slice() +})