Skip to content
Advertisement

MUI5 not working with jest – SyntaxError: Cannot use import statement outside a module

Reproducible repo: https://github.com/hutber/cannotusestatement

What is more worrying is: https://codesandbox.io/s/vigilant-bartik-bmz8x in the sandbox the tests pass. However if you checkout the above repo, which was imported into this sandbox it will not pass locally.

I have no doubt that the issue is my jest does not compile the node_modules that would be needed for running my tests. But I am at a loss now on how to get it working.

I would simply like to be able to run the tests. They do not run currently

test

JavaScript

jest.config.js

JavaScript

babel.config.js

JavaScript

package.json

JavaScript

yarn test

JavaScript

JavaScript

Advertisement

Answer

First you have two exports in your Select.tsx file. Just use the default export, so change line 20 to:

JavaScript

Then in your Select.test.tsx file change to:

JavaScript

Finally to fix the import issue change your code in Select.tsx to:

JavaScript

This is a fairly common issue as can be seen here.

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