mardi 23 février 2021

Mock object of SVGs loaded with SVGR for Jest

I load some SVG icons (using SVGR) in my app with the following code:

const iconsContext = require.context("./", true, /\.svg$/)
const icons = iconsContext.keys().reduce((icons, file) => {
  const Icon = iconsContext(file).default
  const label = file.slice(2, -4) // strip './' and '.svg'
  icons[label] = Icon
  return icons
}, {})
export default icons // <- throws an error with Jest

This works fine in my app, but testing with Jest fails as it can't load the SVGs. Unfortunately, I export an object of SVGs and not only an SVG (so the official workaround does not work). What can I do to make it work with Jest correctly?

Aucun commentaire:

Enregistrer un commentaire