From 0424c11f6dbb0e786dea63d53d15af870f5a8f21 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 27 Apr 2020 23:02:35 +0200 Subject: [PATCH 1/3] chore: add teardown of jest-runtime --- CHANGELOG.md | 1 + packages/jest-runner/src/runTest.ts | 2 ++ packages/jest-runtime/src/index.ts | 27 +++++++++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07c630555d7e..30e02ef4c957 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - `[jest-resolve]` Update `resolve` to a version using native `realpath`, which is faster than the default JS implementation ([#9872](https://github.com/facebook/jest/pull/9872)) - `[jest-resolve]` Pass custom cached `realpath` function to `resolve` ([#9873](https://github.com/facebook/jest/pull/9873)) +- `[jest-runtime]` Add `teardown` method to clear any caches when tests complete ([#9906](https://github.com/facebook/jest/pull/9906)) ## 25.4.0 diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index dd8735be8b57..80c5ae5dc067 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -300,6 +300,8 @@ async function runTestInternal( }); } finally { await environment.teardown(); + // TODO: this function might be missing, remove ? in Jest 26 + runtime.teardown?.(); sourcemapSupport.resetRetrieveHandlers(); } diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index ecb2d25f3ef8..ed8788229d85 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -718,12 +718,16 @@ class Runtime { try { fn(); } finally { + this._isolatedModuleRegistry.clear(); + this._isolatedMockRegistry.clear(); this._isolatedModuleRegistry = null; this._isolatedMockRegistry = null; } } resetModules(): void { + this._isolatedModuleRegistry?.clear(); + this._isolatedMockRegistry?.clear(); this._isolatedModuleRegistry = null; this._isolatedMockRegistry = null; this._mockRegistry.clear(); @@ -833,6 +837,29 @@ class Runtime { this._moduleMocker.clearAllMocks(); } + teardown(): void { + this.restoreAllMocks(); + this.resetAllMocks(); + this.resetModules(); + + this._internalModuleRegistry.clear(); + this._mockFactories.clear(); + this._mockMetaDataCache = {}; + this._shouldMockModuleCache = {}; + this._shouldUnmockTransitiveDependenciesCache = {}; + this._transitiveShouldMock = {}; + this._virtualMocks = {}; + this._cacheFS = {}; + + this._sourceMapRegistry = {}; + + this._fileTransforms.clear(); + this.jestObjectCaches.clear(); + + this._v8CoverageResult = []; + this._moduleImplementation = undefined; + } + private _resolveModule(from: Config.Path, to?: string) { return to ? this._resolver.resolveModule(from, to) : from; } From d5e155ffdf7325f19da12766597250a2eb0c6924 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 28 Apr 2020 12:26:33 +0200 Subject: [PATCH 2/3] why aren't these type errors :sob: --- packages/jest-runtime/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index ed8788229d85..e5cf0d4fe5d8 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -843,7 +843,7 @@ class Runtime { this.resetModules(); this._internalModuleRegistry.clear(); - this._mockFactories.clear(); + this._mockFactories = {}; this._mockMetaDataCache = {}; this._shouldMockModuleCache = {}; this._shouldUnmockTransitiveDependenciesCache = {}; From 52409e39b5ed9968ca55c36e2033b648464357bd Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 28 Apr 2020 12:44:41 +0200 Subject: [PATCH 3/3] thank you tests --- packages/jest-runtime/src/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index e5cf0d4fe5d8..59b48d66bc04 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -718,8 +718,9 @@ class Runtime { try { fn(); } finally { - this._isolatedModuleRegistry.clear(); - this._isolatedMockRegistry.clear(); + // might be cleared within the callback + this._isolatedModuleRegistry?.clear(); + this._isolatedMockRegistry?.clear(); this._isolatedModuleRegistry = null; this._isolatedMockRegistry = null; }