How can I mock out all the icons in React `@material-ui/icons` using Jest?

Viewed 537

This question could also be generalised as - how can I mock out all properties on an imported module to return React components?

1 Answers

The best solution I have so far is:

jest.mock('@material-ui/icons', () => {
  const icons = {
    __esModule: true,
  };

  const handler = {
    get: function (_, prop) {
      return () => <div className={`mock_${prop}Icon`} />;
    },
  };

  return new Proxy(icons, handler);
});
Related