MCPcopy
hub / github.com/jestjs/jest / clearMocksOnScope

Method clearMocksOnScope

packages/jest-mock/src/index.ts:1404–1423  ·  view source on GitHub ↗

* Walks the own keys of `scope` and calls `.mockClear()` on each value that * is a Jest mock function. Used by `resetModules` to clear mocks that user * code installed directly on the test environment's global object (where * the mock-fn registry doesn't see them).

(scope: object)

Source from the content-addressed store, hash-verified

1402 * the mock-fn registry doesn't see them).
1403 */
1404 clearMocksOnScope(scope: object): void {
1405 for (const key of Object.keys(scope)) {
1406 const value = (scope as Record<string, unknown>)[key];
1407 // Gate on `'_isMockFunction' in value` first: that uses the `has` trap
1408 // and won't fire a throwing getter (e.g. a user Proxy on `globalThis`).
1409 // `isMockFunction` reads `._isMockFunction` directly, so we only call
1410 // it once we know the property exists. The extra `typeof mockClear`
1411 // guard rejects forged values that set the marker but aren't real Jest
1412 // mocks.
1413 if (
1414 value != null &&
1415 (typeof value === 'object' || typeof value === 'function') &&
1416 '_isMockFunction' in value &&
1417 this.isMockFunction(value) &&
1418 typeof (value as Mock).mockClear === 'function'
1419 ) {
1420 (value as Mock).mockClear();
1421 }
1422 }
1423 }
1424
1425 resetAllMocks(): void {
1426 this._mockConfigRegistry = new WeakMap();

Callers 2

resetModulesMethod · 0.80
index.test.tsFile · 0.80

Calls 2

isMockFunctionMethod · 0.95
mockClearMethod · 0.80

Tested by

no test coverage detected