Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Jest fails when a component imports an .svg file

ErrorA test fails as soon as a component that imports an .svg file is loaded

Short answer

Jest runs every imported file as JavaScript, and an .svg file is XML, not JavaScript. Tell Jest what to do with it: map .svg imports to a small stub module with moduleNameMapper, or give them a transformer. If you add a transform, list babel-jest again, because a custom transform replaces the default one.

  • Applies to Jest 29, Jest 30, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the first lines of the failure. The file named in the stack and the line it points at show whether Jest is parsing the SVG as code or cannot find it.

    npx jest --no-cache 2>&1 | head -40
  2. 2Match what you see, then jump to its fix

    1. 1The stack points into the .svg file, or a syntax error appears on the first line of itJest has no rule for .svg filesFix
    2. 2The error stays after you added a ruleThe mapper pattern is wrong or sits below a broader ruleFix
    3. 3SVG imports work, but JSX or TypeScript files now fail to parseA custom transform replaced babel-jestFix

What does the error look like?

  • A test fails as soon as a component that imports an .svg file is loaded
  • A syntax error is reported for the .svg file, because Jest tries to parse it as JavaScript

Cause 1 of 3

Jest has no rule for .svg files

1Check

By default Jest only transforms JavaScript and TypeScript files. An image is not valid JavaScript, so Jest needs a stub or a transformer for it.

cat jest.config.js 2>/dev/null; grep -n '"jest"' package.json

2Fix

Map images to a stub file. The stub only needs to export something.

// jest.config.js
module.exports = {
  moduleNameMapper: {
    '\\.(svg|png|jpg|jpeg|gif|webp)$': '<rootDir>/__mocks__/fileMock.js',
  },
};

// __mocks__/fileMock.js
module.exports = 'test-file-stub';

3Verify

Run the test that imports the component.

npx jest src/App.test.js

Confirmed by jestjs.io

Did this fix it?

Cause 2 of 3

The mapper pattern is wrong or sits below a broader rule

1Check

moduleNameMapper checks patterns one by one in order, so a pattern listed earlier can take the import before yours. A pattern without ^ and $ boundaries also matches more than you mean.

grep -n -A8 moduleNameMapper jest.config.js

2Fix

Put the specific svg rule first and anchor broad patterns.

moduleNameMapper: {
  '\\.svg$': '<rootDir>/__mocks__/fileMock.js',
  '^@/(.*)$': '<rootDir>/src/$1',
}

3Verify

Run the failing test again.

npx jest

Confirmed by jestjs.io

Did this fix it?

Cause 3 of 3

A custom transform replaced babel-jest

1Check

The default transform is babel-jest for .js, .jsx, .ts and .tsx files. Once you set the transform option yourself, that default is gone unless you list it.

grep -n -A6 "transform" jest.config.js

2Fix

Add babel-jest back next to the svg transformer.

transform: {
  '\\.[jt]sx?$': 'babel-jest',
  '\\.svg$': '<rootDir>/fileTransformer.js',
},

3Verify

Run the whole suite.

npx jest

Confirmed by jestjs.io

Did this fix it?

How do I stop it coming back?

  • Add the asset stub to the Jest config the day you add the first image import.
  • Keep one jest.config.js; avoid a second config inside package.json.
  • When you change the transform option, rerun the full suite once, not only the failing file.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

Electron app blank with 'Refused to evaluate a string as JavaScript'

Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive

The page has a Content-Security-Policy without 'unsafe-eval', and some code calls eval or new Function.

3 causes, each with a fix

Web apps

Jest fails to parse a React Native or Expo dependency

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax

Jest does not run Babel on node_modules by default, and many React Native packages ship untranspiled source.

3 causes, each with a fix

Web apps

TypeScript says a JavaScript module implicitly has an any type

Could not find a declaration file for module 'react-materialize'. 'path/to/module-name.js' implicitly has an 'any' type.

TypeScript found the JavaScript file but no types for it, and noImplicitAny turns that into an error.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.