-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#15215 Reduce memory leak in node env by shredding global properties …
…in teardown
- Loading branch information
Showing
7 changed files
with
210 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
const NO_SHRED_AFTER_TEARDOWN = Symbol.for('$$jest-no-shred'); | ||
|
||
/** | ||
* Deletes all the properties from the given value (if it's an object), | ||
* unless the value was protected via {@link #setNotShreddable}. | ||
* | ||
* @param value the given value. | ||
*/ | ||
export function shred(value: unknown): void { | ||
if (isShreddable(value)) { | ||
const protectedProperties = Reflect.get(value, NO_SHRED_AFTER_TEARDOWN); | ||
if (!Array.isArray(protectedProperties) || protectedProperties.length > 0) { | ||
for (const key of Reflect.ownKeys(value)) { | ||
if (!protectedProperties?.includes(key)) { | ||
Reflect.deleteProperty(value, key); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Protects the given value from being shredded by {@link #shred}. | ||
* | ||
* @param value The given value. | ||
* @param properties If the array contains any property, | ||
* then only these properties will not be deleted; otherwise if the array is empty, | ||
* all properties will not be deleted. | ||
*/ | ||
export function setNotShreddable<T extends object>( | ||
value: T, | ||
properties: Array<keyof T> = [], | ||
): boolean { | ||
if (isShreddable(value)) { | ||
return Reflect.set(value, NO_SHRED_AFTER_TEARDOWN, properties); | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Whether the given value is possible to be shredded. | ||
* | ||
* @param value The given value. | ||
*/ | ||
export function isShreddable(value: unknown): value is object { | ||
return value !== null && ['object', 'function'].includes(typeof value); | ||
} |