Denis
Denis

Reputation: 101

React.jsx: type is invalid when testing a React component with an svg with Jest and React Testing Library

I have a Next.js 12.1.4 project using Typescript, React Testing Library and SVGR for importing icons like this: import ChevronLeftIcon from './chevron-left.svg'

The issue I have is that when I run a test of a component that include an SVG import, I get the following error:

console.error
    Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.
    
    Check the render method of `Loader`.
        at Loader (.../src/components/atoms/Loader/Loader.tsx:11:36)
        at div
        at button
        at Button (.../src/components/atoms/buttons/Button/Button.tsx:19:5)

      15 |         }`}
      16 |     >
    > 17 |         <LoaderIcon />
         |                       ^
      18 |     </div>
      19 | )
      20 |

      at printWarning (node_modules/react/cjs/react-jsx-runtime.development.js:117:30)
      at error (node_modules/react/cjs/react-jsx-runtime.development.js:93:5)
      at jsxWithValidation (node_modules/react/cjs/react-jsx-runtime.development.js:1152:7)
      at Object.jsxWithValidationDynamic [as jsx] (node_modules/react/cjs/react-jsx-runtime.development.js:1209:12)
      at Loader (src/components/atoms/Loader/Loader.tsx:17:23)
      at renderWithHooks (node_modules/react-dom/cjs/react-dom.development.js:14985:18)
      at mountIndeterminateComponent (node_modules/react-dom/cjs/react-dom.development.js:17811:13)

I run tests with yarn test which matches the following script from my package.json: "test": "TZ=UTC ./node_modules/jest/bin/jest.js",

Here is what I have in next.config.js for SVGR:

webpack(config) {
    config.module.rules.push({
        test: /\.svg$/,
        use: ['@svgr/webpack'],
    })

    return config
},

Here is my jest.config.js:

const nextJest = require('next/jest')
const createJestConfig = nextJest({
  dir: './',
})

const customJestConfig = {
  moduleDirectories: ['node_modules', '<rootDir>/src/'],
  testEnvironment: 'jest-environment-jsdom',
  moduleNameMapper: {
    '\\.svg$': '<rootDir>/src/__mocks__/svgrMock.tsx',
  },
}

module.exports = createJestConfig(customJestConfig)

and src/__mocks__/svgrMock.tsx

import React, { SVGProps } from 'react'

const SvgrMock = React.forwardRef<SVGSVGElement, SVGProps<SVGSVGElement>>(
    (props, ref) => <svg ref={ref} {...props} />,
)

SvgrMock.displayName = 'SvgrMock'

export const ReactComponent = SvgrMock
export default SvgrMock

I also tried to use jest-svg-transformer without much success.

Here are the versions of the libraries involved:

Thanks for your help :)

Upvotes: 3

Views: 3117

Answers (1)

Denis
Denis

Reputation: 101

After a few days of trials and errors, I was able to fix this issue by using the Jest with Babel configuration from the Next.js documentation, instead of using the next/jest plugin powered by the Rust compiler: https://nextjs.org/docs/testing#setting-up-jest-with-babel

I also had to defined a jest transformer for the svg files, jestSvgTransformer.js:

const path = require('path')

module.exports = {
  process(src, filePath) {
    if (path.extname(filePath) !== '.svg') {
      return src
    }

    const name = `svg-${path.basename(filePath, '.svg')}`
      .split(/\W+/)
      .map((x) => `${x.charAt(0).toUpperCase()}${x.slice(1)}`)
      .join('')

    return `
const React = require('react');
function ${name}(props) {
  return React.createElement(
    'svg',
    Object.assign({}, props, {'data-file-name': ${name}.name})
  )
}
module.exports = ${name}
            `
  },
}

In jest.config.js

  • in moduleNameMapper: remove svg from the '^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp)$/i': '<rootDir>/<path to your mock files (e.g.) __mocks__>/fileMock.js',
  • in transform: add '^.+\\.svg$': '<rootDir>/<path to your mock files (e.g.) __mocks__>/jestSvgTransformer.js'
  • add moduleDirectories: ['node_modules', 'src'], to enable absolute imports

Upvotes: 6

Related Questions