From a9599fc5a72f5f41623de9c23d9a69e15891abe8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 21 Apr 2020 23:31:39 +0200 Subject: [PATCH 1/2] chore: mock stealthy-require in tests --- packages/jest-runtime/src/index.ts | 4 +++- testSetupFile.js | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 001de2f98ec9..f44fc26a33bc 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1273,7 +1273,9 @@ class Runtime { moduleRequire.resolve = resolve; moduleRequire.cache = (() => { const notPermittedMethod = () => { - console.warn('`require.cache` modification is not permitted'); + this._environment.global.console.warn( + '`require.cache` modification is not permitted', + ); return true; }; return new Proxy(Object.create(null), { diff --git a/testSetupFile.js b/testSetupFile.js index ef9ed18680f6..7f73aec1ef96 100644 --- a/testSetupFile.js +++ b/testSetupFile.js @@ -8,3 +8,6 @@ // Some of the `jest-runtime` tests are very slow and cause // timeouts on travis jest.setTimeout(70000); + +// this module does some funky stuff with `require.cache`, flooding the terminal with output +jest.mock('stealthy-require', () => (_, m) => m()); From 81ae1088d441544a67aefd0c5ea117b274968a84 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Apr 2020 11:01:40 +0200 Subject: [PATCH 2/2] chore: only warn once about cache manipulation --- packages/jest-runtime/src/index.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index f44fc26a33bc..100ad097eeb7 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -148,6 +148,7 @@ class Runtime { private _unmockList: RegExp | undefined; private _virtualMocks: BooleanObject; private _moduleImplementation?: typeof nativeModule.Module; + private _hasWarnedAboutRequireCacheModification = false; constructor( config: Config.ProjectConfig, @@ -1273,9 +1274,13 @@ class Runtime { moduleRequire.resolve = resolve; moduleRequire.cache = (() => { const notPermittedMethod = () => { - this._environment.global.console.warn( - '`require.cache` modification is not permitted', - ); + if (!this._hasWarnedAboutRequireCacheModification) { + this._environment.global.console.warn( + '`require.cache` modification is not permitted', + ); + + this._hasWarnedAboutRequireCacheModification = true; + } return true; }; return new Proxy(Object.create(null), {