jest icon indicating copy to clipboard operation
jest copied to clipboard

Allow wildcards when mocking modules

Open AdamTReineke opened this issue 6 years ago • 4 comments

🚀 Feature Proposal

Allow wildcards when mocking modules.

Motivation

We mock out modules so we only import them for tests that are actually using the modules. For a library like office-ui-fabric-react, we import from deep within the library since their root export includes everything. We would like a way to define a mock that gets matched for any child of the module as well.

Example

Today we do this:

// Don't disable warnings for the icons unless the test is using Fabric.
jest.mock("office-ui-fabric-react", () => {
	jest.unmock("office-ui-fabric-react");

	// See https://github.com/OfficeDev/office-ui-fabric-react/wiki/Using-icons#disabling-generated-warnings
	const { setIconOptions } = require("office-ui-fabric-react/lib/Styling");
	setIconOptions({ disableWarnings: true });

	const fabric = require("office-ui-fabric-react");
	return fabric;
});

This requires a perfect match on module name, so it doesn't match deeper imports like:

import { ActionButton } from "office-ui-fabric-react/lib/components/Button/ActionButton/ActionButton";

We want to do this:

// Don't disable warnings for the icons unless the test is using Fabric.
jest.mock("office-ui-fabric-react/*", (moduleBeingMatched) => {
	jest.unmock(moduleBeingMatched);

	// See https://github.com/OfficeDev/office-ui-fabric-react/wiki/Using-icons#disabling-generated-warnings
	const { setIconOptions } = require("office-ui-fabric-react/lib/Styling");
	setIconOptions({ disableWarnings: true });

	const fabric = require(moduleBeingMatched);
	return fabric;
});

This would mock office-ui-fabric-react/lib/components/Button/ActionButton/ActionButton (along with all other imports under office-ui-fabric-react, then the handler, called when imported, can unmock that specific module, disable the icons warnings, and require the original.

Pitch

This belongs in the core platform because mocking is fully handled by the core platform, I think.

cc @JoshuaKGoldberg, for making me propose this feature.

AdamTReineke avatar Jun 26 '18 00:06 AdamTReineke

Any updates on this?

TK95 avatar May 28 '20 00:05 TK95

Also interested!

JeromeGill avatar Jun 28 '21 15:06 JeromeGill

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Jun 28 '22 15:06 github-actions[bot]

This would be really helpful for mocking @mui/icons-material, which can be imported like:

import KeyboardArrowLeft from '@mui/icons-material/KeyboardArrowLeft'
import KeyboardArrowRight from '@mui/icons-material/KeyboardArrowRight'

with a wildcard they could all be mocked

TSMMark avatar Aug 06 '22 21:08 TSMMark

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Aug 06 '23 22:08 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar Sep 05 '23 22:09 github-actions[bot]

I'm still interested in this option - can we reopen?

aarowman avatar Sep 06 '23 17:09 aarowman

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar Oct 07 '23 00:10 github-actions[bot]