Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: mock stealthy-require in tests #9855

Merged
merged 3 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class Runtime {
private _virtualMocks: BooleanObject;
private _moduleImplementation?: typeof nativeModule.Module;
private jestObjectCaches: Map<string, Jest>;
private _hasWarnedAboutRequireCacheModification = false;

constructor(
config: Config.ProjectConfig,
Expand Down Expand Up @@ -1302,7 +1303,13 @@ class Runtime {
moduleRequire.resolve = resolve;
moduleRequire.cache = (() => {
const notPermittedMethod = () => {
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<RequireCache>(Object.create(null), {
Expand Down
3 changes: 3 additions & 0 deletions testSetupFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ yarn why stealthy-require
=> Found "[email protected]"
info Reasons this module exists
   - "_project_#jest-environment-jsdom#jsdom#request-promise-native" depends on it
   - Hoisted from "_project_#jest-environment-jsdom#jsdom#request-promise-native#stealthy-require"

jsdom/jsdom#2792

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimenB should it be a best practice recommended for everyone?