Skip to content

Commit f500bfb

Browse files
committed
Run tests in JSDOM env with force GC
JSDOM is default environment in Jest Here we only extend this environment to be able to start V8 garbadge collection at the end of each suite testing This allows to show clearer picture of heap usage by each test suite as GC now runs exactly between test suites (before it had some lazy behaviour about when to run GC)
1 parent 4bf3aa1 commit f500bfb

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

jest-jsdom-env.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const vm = require('vm');
2+
const v8 = require('v8');
3+
4+
const JSDomEnvironment = require('jest-environment-jsdom');
5+
6+
class JSDomEnvironmentWithForceGC extends JSDomEnvironment {
7+
teardown() {
8+
return super.teardown().then(() => {
9+
const isGlobaGCHidden = !global.gc;
10+
11+
v8.setFlagsFromString('--expose-gc');
12+
vm.runInNewContext('gc')();
13+
if (isGlobaGCHidden) {
14+
v8.setFlagsFromString('--no-expose-gc');
15+
}
16+
});
17+
}
18+
}
19+
20+
module.exports = JSDomEnvironmentWithForceGC;

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
22
setupTestFrameworkScriptFile: require.resolve('./setupJest'),
3+
testEnvironment: require.resolve('./jest-jsdom-env'),
34
};

0 commit comments

Comments
 (0)