From adf78d583903e9e692e7250d75e7edcc73f1c5f8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 30 Apr 2020 16:03:06 +0200 Subject: [PATCH 1/3] chore: test for memory leak using circus --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index fcee3ee8e014..578043e864e6 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -58,7 +58,7 @@ jobs: - run: *install - save-cache: *save-cache - run: - command: JEST_CIRCUS=1 yarn test-ci-partial + command: JEST_CIRCUS=1 yarn test-ci-partial && JEST_CIRCUS=1 yarn test-leak - store_test_results: path: reports/junit From d3151aba5cb405e5c9d0ea4cba662e0598d51773 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 30 Apr 2020 16:17:20 +0200 Subject: [PATCH 2/3] fix: plug memory leak in jest-circus when running in band --- CHANGELOG.md | 2 ++ .../src/legacy-code-todo-rewrite/jestAdapter.ts | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8052d1334837..9e4741b83665 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ ### Performance +- `[jest-circus]` Fix memory leak when running in band ([#9934](https://github.com/facebook/jest/pull/9934)) + ## 25.5.2 ### Fixes diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts index 4630f16c56bb..5591ec98f2e6 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts @@ -11,6 +11,7 @@ import type {JestEnvironment} from '@jest/environment'; import type {TestResult} from '@jest/test-result'; import type {RuntimeType as Runtime} from 'jest-runtime'; import type {SnapshotStateType} from 'jest-snapshot'; +import {deepCyclicCopy} from 'jest-util'; const FRAMEWORK_INITIALIZER = path.resolve(__dirname, './jestAdapterInit.js'); const EXPECT_INITIALIZER = path.resolve(__dirname, './jestExpect.js'); @@ -101,7 +102,10 @@ const jestAdapter = async ( globalConfig, testPath, }); - return _addSnapshotData(results, snapshotState); + + _addSnapshotData(results, snapshotState); + + return deepCyclicCopy(results); }; const _addSnapshotData = ( @@ -131,7 +135,6 @@ const _addSnapshotData = ( results.snapshot.unchecked = !status.deleted ? uncheckedCount : 0; // Copy the array to prevent memory leaks results.snapshot.uncheckedKeys = Array.from(uncheckedKeys); - return results; }; export = jestAdapter; From 44f77db809141ae2b03da774ba02ba6c53d93488 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 30 Apr 2020 16:45:59 +0200 Subject: [PATCH 3/3] comment --- .../jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts index 5591ec98f2e6..f5653ebf5927 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts @@ -105,7 +105,10 @@ const jestAdapter = async ( _addSnapshotData(results, snapshotState); - return deepCyclicCopy(results); + // We need to copy the results object to ensure we don't leaks the prototypes + // from the VM. Jasmine creates the result objects in the parent process, we + // should consider doing that for circus as well. + return deepCyclicCopy(results, {keepPrototype: false}); }; const _addSnapshotData = (