Skip to content

Instantly share code, notes, and snippets.

@kirillgroshkov
Created April 20, 2020 20:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kirillgroshkov/d312f757da432b800bff5e2489a7e749 to your computer and use it in GitHub Desktop.
Save kirillgroshkov/d312f757da432b800bff5e2489a7e749 to your computer and use it in GitHub Desktop.
diff --git a/node_modules/jest-runtime/build/index.js b/node_modules/jest-runtime/build/index.js
index 0c9dd8c..58109b5 100644
--- a/node_modules/jest-runtime/build/index.js
+++ b/node_modules/jest-runtime/build/index.js
@@ -1189,7 +1189,38 @@ class Runtime {
return this._getMockedNativeModule();
}
- return require(moduleName);
+ // return require(moduleName);
+ if (!this._coreModulesProxyCache) {
+ this._coreModulesProxyCache = Object.create(null);
+ }
+
+ if (this._coreModulesProxyCache[moduleName]) {
+ return this._coreModulesProxyCache[moduleName];
+ }
+
+ const mod = require(moduleName);
+ const { JEST_WARN_ON_PATCH } = process.env
+ const forbidden = ['http', 'https'];
+ const warned = this._warned = this._warned || Object.create(null);
+
+ const set = (
+ target,
+ property,
+ value,
+ receiver,
+ ) => {
+ if (target !== mod || typeof value !== 'function' || value._isMockFunction || forbidden.some(s => s === moduleName))
+ return Reflect.set(target, property, value, receiver);
+ if (!warned[moduleName + '_' + property]){
+ if (JEST_WARN_ON_PATCH) {
+ console.warn('Patching module not allowed', moduleName, property);
+ }
+ warned[moduleName + '_' + property] = true;
+ }
+ return true;
+ };
+
+ return this._coreModulesProxyCache[moduleName] = new Proxy(mod, {set});
}
_importCoreModule(moduleName, context) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment