diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index dd8735be8b57..d386b03ff87a 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(); + // this function might be missing + runtime.teardown?.(); sourcemapSupport.resetRetrieveHandlers(); } diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index d5d0448cb23f..14634329e73a 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -843,6 +843,30 @@ class Runtime { this._moduleMocker.clearAllMocks(); } + teardown(): void { + this.resetAllMocks(); + this.resetAllMocks(); + + this._cacheFS.clear(); + this._internalModuleRegistry.clear(); + this._mockFactories.clear(); + this._mockMetaDataCache.clear(); + this._mockRegistry.clear(); + this._isolatedMockRegistry?.clear(); + this._isolatedModuleRegistry?.clear(); + this._moduleRegistry.clear(); + this._esmoduleRegistry.clear(); + this._shouldMockModuleCache.clear(); + this._shouldUnmockTransitiveDependenciesCache.clear(); + this._sourceMapRegistry.clear(); + this._fileTransforms.clear(); + this._v8CoverageResult = []; + this._transitiveShouldMock.clear(); + this._virtualMocks.clear(); + this._moduleImplementation = undefined; + this.jestObjectCaches.clear(); + } + private _resolveModule(from: Config.Path, to?: string) { return to ? this._resolver.resolveModule(from, to) : from; }