Skip to content

Commit

Permalink
chore: add teardown of jest-runtime (#9906)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 28, 2020
1 parent 80dde6f commit 5db005f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions packages/jest-runner/src/runTest.ts
Expand Up @@ -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();
}
Expand Down
28 changes: 28 additions & 0 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 5db005f

Please sign in to comment.