jest
jest copied to clipboard
Allow wildcards when mocking modules
🚀 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.
Any updates on this?
Also interested!
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.
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
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.
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.
I'm still interested in this option - can we reopen?
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.