Skip to content
Advertisement

SyntaxError: Unexpected token ‘<' on shallow render with Enzyme and Jest in React

I am running tests with jest and enzyme in my React application, and while my simple sanity check test (2+2 expect 4) works, this error is thrown when I go to shallow render a component. It also throws when I try to replace shallow() with render()

JavaScript

Here is the app.test.js file:

JavaScript

babel.config.js:

JavaScript

EDIT : I added babel-jest and now this is the error:

JavaScript

The components are .jsx filetypes. Has anyone seen this error before? I don’t think I need to change anything in my config, this seems to be a pr

Advertisement

Answer

I found the solution. There are several places I needed to configure correctly. I’m listing them out below for anyone else that has this error and is not having any luck with other answers.

Setup your package.json to include these dependencies, and jest configuration:

JavaScript

Basic test:

JavaScript

Add these styleMock and filemock modules to prevent errors on jsx components importing css/scss and/or files:

JavaScript
JavaScript

👩‍💻👨‍💻 …and now you should be able to run your tests with jest + enzyme, and in my case, chai.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement