From 5db005f15960e75f54c963fd9ac6419c8cf603da Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 28 Apr 2020 13:15:13 +0200 Subject: [PATCH] chore: add teardown of `jest-runtime` (#9906) --- CHANGELOG.md | 1 + packages/jest-runner/src/runTest.ts | 2 ++ packages/jest-runtime/src/index.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5d5f1571221..99e5f01ac925 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,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 e57c63539196..5d753434d398 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -301,6 +301,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..59b48d66bc04 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -718,12 +718,17 @@ class Runtime { try { fn(); } finally { + // might be cleared within the callback + 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 +838,29 @@ class Runtime { this._moduleMocker.clearAllMocks(); } + teardown(): void { + this.restoreAllMocks(); + this.resetAllMocks(); + this.resetModules(); + + this._internalModuleRegistry.clear(); + this._mockFactories = {}; + 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; }