Skip to content

Commit c1b3d6d

Browse files
committed
#15215 Rename 'shredder' API to something closer to garbage collection
1 parent bf5d709 commit c1b3d6d

File tree

7 files changed

+41
-34
lines changed

7 files changed

+41
-34
lines changed

packages/jest-circus/src/state.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import type {Circus, Global} from '@jest/types';
9-
import {setGlobal, setNotShreddable} from 'jest-util';
9+
import {protectProperties, setGlobal} from 'jest-util';
1010
import eventHandler from './eventHandler';
1111
import formatNodeAssertErrors from './formatNodeAssertErrors';
1212
import {EVENT_HANDLERS, STATE_SYM} from './types';
@@ -44,7 +44,7 @@ export const getState = (): Circus.State =>
4444
(globalThis as Global.Global)[STATE_SYM] as Circus.State;
4545
export const setState = (state: Circus.State): Circus.State => {
4646
setGlobal(globalThis, STATE_SYM, state);
47-
setNotShreddable(state, [
47+
protectProperties(state, [
4848
'hasFocusedTests',
4949
'hasStarted',
5050
'includeTestLocationInResult',

packages/jest-environment-node/src/index.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import {LegacyFakeTimers, ModernFakeTimers} from '@jest/fake-timers';
1515
import type {Global} from '@jest/types';
1616
import {ModuleMocker} from 'jest-mock';
1717
import {
18+
canDeleteProperties,
19+
deleteProperties,
1820
installCommonGlobals,
19-
isShreddable,
20-
setNotShreddable,
21-
shred,
21+
protectProperties,
2222
} from 'jest-util';
2323

2424
type Timer = {
@@ -235,8 +235,8 @@ export const TestEnvironment = NodeEnvironment;
235235
* Creates a new empty global object and wraps it with a {@link Proxy}.
236236
*
237237
* The purpose is to register any property set on the global object,
238-
* and {@link #shred} them at environment teardown, to clean up memory and
239-
* prevent leaks.
238+
* and {@link #deleteProperties} on them at environment teardown,
239+
* to clean up memory and prevent leaks.
240240
*/
241241
class GlobalProxy implements ProxyHandler<typeof globalThis> {
242242
private global: typeof globalThis = Object.create(
@@ -257,16 +257,16 @@ class GlobalProxy implements ProxyHandler<typeof globalThis> {
257257

258258
/**
259259
* Marks that the environment setup has completed, and properties set on
260-
* the global object from now on should be shredded at teardown.
260+
* the global object from now on should be deleted at teardown.
261261
*/
262262
envSetupCompleted(): void {
263263
this.isEnvSetup = true;
264264
}
265265

266266
/**
267-
* Shreds any property that was set on the global object, except for:
267+
* Deletes any property that was set on the global object, except for:
268268
* 1. Properties that were set before {@link #envSetupCompleted} was invoked.
269-
* 2. Properties protected by {@link #setNotShreddable}.
269+
* 2. Properties protected by {@link #protectProperties}.
270270
*/
271271
clear(): void {
272272
for (const {property, value} of [
@@ -278,10 +278,10 @@ class GlobalProxy implements ProxyHandler<typeof globalThis> {
278278
]) {
279279
/*
280280
* react-native invoke its custom `performance` property after env teardown.
281-
* its setup file should use `setNotShreddable` to prevent this.
281+
* its setup file should use `protectProperties` to prevent this.
282282
*/
283283
if (property !== 'performance') {
284-
shred(value);
284+
deleteProperties(value);
285285
}
286286
}
287287
this.propertyToValue.clear();
@@ -332,8 +332,8 @@ class GlobalProxy implements ProxyHandler<typeof globalThis> {
332332
private register(property: string | symbol, value: unknown) {
333333
const currentValue = this.propertyToValue.get(property);
334334
if (value !== currentValue) {
335-
if (!this.isEnvSetup && isShreddable(value)) {
336-
setNotShreddable(value);
335+
if (!this.isEnvSetup && canDeleteProperties(value)) {
336+
protectProperties(value);
337337
}
338338
if (currentValue) {
339339
this.leftovers.push({property, value: currentValue});

packages/jest-repl/src/cli/runtime-cli.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ export async function run(
9999
},
100100
{console: customConsole, docblockPragmas: {}, testPath: filePath},
101101
);
102-
setGlobal(environment.global, 'console', customConsole, false);
103-
setGlobal(environment.global, 'jestProjectConfig', projectConfig, false);
104-
setGlobal(environment.global, 'jestGlobalConfig', globalConfig, false);
102+
setGlobal(environment.global, 'console', customConsole, 'retain');
103+
setGlobal(environment.global, 'jestProjectConfig', projectConfig, 'retain');
104+
setGlobal(environment.global, 'jestGlobalConfig', globalConfig, 'retain');
105105

106106
const runtime = new Runtime(
107107
projectConfig,

packages/jest-runner/src/runTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ async function runTestInternal(
184184
? new LeakDetector(environment)
185185
: null;
186186

187-
setGlobal(environment.global, 'console', testConsole, false);
187+
setGlobal(environment.global, 'console', testConsole, 'retain');
188188

189189
const runtime = new Runtime(
190190
projectConfig,

packages/jest-util/src/shredder.ts renamed to packages/jest-util/src/garbage-collection-utils.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
const NO_SHRED_AFTER_TEARDOWN = Symbol.for('$$jest-no-shred');
8+
const PROTECT_PROPERTY = Symbol.for('$$jest-protect-from-deletion');
99

1010
/**
1111
* Deletes all the properties from the given value (if it's an object),
12-
* unless the value was protected via {@link #setNotShreddable}.
12+
* unless the value was protected via {@link #protectProperties}.
1313
*
1414
* @param value the given value.
1515
*/
16-
export function shred(value: unknown): void {
17-
if (isShreddable(value)) {
18-
const protectedProperties = Reflect.get(value, NO_SHRED_AFTER_TEARDOWN);
16+
export function deleteProperties(value: unknown): void {
17+
if (canDeleteProperties(value)) {
18+
const protectedProperties = Reflect.get(value, PROTECT_PROPERTY);
1919
if (!Array.isArray(protectedProperties) || protectedProperties.length > 0) {
2020
for (const key of Reflect.ownKeys(value)) {
2121
if (!protectedProperties?.includes(key)) {
@@ -27,28 +27,28 @@ export function shred(value: unknown): void {
2727
}
2828

2929
/**
30-
* Protects the given value from being shredded by {@link #shred}.
30+
* Protects the given value from being deleted by {@link #deleteProperties}.
3131
*
3232
* @param value The given value.
3333
* @param properties If the array contains any property,
3434
* then only these properties will not be deleted; otherwise if the array is empty,
3535
* all properties will not be deleted.
3636
*/
37-
export function setNotShreddable<T extends object>(
37+
export function protectProperties<T extends object>(
3838
value: T,
3939
properties: Array<keyof T> = [],
4040
): boolean {
41-
if (isShreddable(value)) {
42-
return Reflect.set(value, NO_SHRED_AFTER_TEARDOWN, properties);
41+
if (canDeleteProperties(value)) {
42+
return Reflect.set(value, PROTECT_PROPERTY, properties);
4343
}
4444
return false;
4545
}
4646

4747
/**
48-
* Whether the given value is possible to be shredded.
48+
* Whether the given value has properties that can be deleted (regardless of protection).
4949
*
5050
* @param value The given value.
5151
*/
52-
export function isShreddable(value: unknown): value is object {
52+
export function canDeleteProperties(value: unknown): value is object {
5353
return value !== null && ['object', 'function'].includes(typeof value);
5454
}

packages/jest-util/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ export {default as tryRealpath} from './tryRealpath';
2929
export {default as requireOrImportModule} from './requireOrImportModule';
3030
export {default as invariant} from './invariant';
3131
export {default as isNonNullable} from './isNonNullable';
32-
export {isShreddable, setNotShreddable, shred} from './shredder';
32+
export {
33+
canDeleteProperties,
34+
protectProperties,
35+
deleteProperties,
36+
} from './garbage-collection-utils';

packages/jest-util/src/setGlobal.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
*/
77

88
import type {Global} from '@jest/types';
9-
import {isShreddable, setNotShreddable} from './shredder';
9+
import {
10+
canDeleteProperties,
11+
protectProperties,
12+
} from './garbage-collection-utils';
1013

1114
export default function setGlobal(
1215
globalToMutate: typeof globalThis | Global.Global,
1316
key: string | symbol,
1417
value: unknown,
15-
shredAfterTeardown = true,
18+
afterTeardown: 'clean' | 'retain' = 'clean',
1619
): void {
1720
Reflect.set(globalToMutate, key, value);
18-
if (!shredAfterTeardown && isShreddable(value)) {
19-
setNotShreddable(value);
21+
if (afterTeardown === 'retain' && canDeleteProperties(value)) {
22+
protectProperties(value);
2023
}
2124
}

0 commit comments

Comments
 (0)