Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add teardown of jest-runtime #9906

Merged
merged 3 commits into from Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions packages/jest-runner/src/runTest.ts
Expand Up @@ -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();
}
Expand Down
27 changes: 27 additions & 0 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -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;
thymikee marked this conversation as resolved.
Show resolved Hide resolved
this._mockRegistry.clear();
Expand Down Expand Up @@ -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;
}
Expand Down