Craft By Zen

Curation  /  The Stream  /  Setting up SVGR with Jest

Setting up SVGR with Jest

Published

I was getting some errors in my unit test output because I converted my SVGs to React components using SVGR. As such, I needed to re-configure Jest accordingly.

  1. Create the mock file
export default "svgr-div"; // This is the kebab case that jest is looking for
export const ReactComponent = "div";

// @see https://react-svgr.com/docs/jest/ for setup
  1. Configure jest in package.json
{
  "jest": {
    "moduleNameMapper": {
      "\\.svg": "<rootDir>/PATH_TO_FOLDER/svg.js"
    }
  }
}