Categories
JavaScript Answers

How to fix “Unexpected token ‘import'” error while running Jest tests?

Spread the love

Sometimes, we want to fix "Unexpected token ‘import’" error while running Jest tests.

In this article, we’ll look at how to fix "Unexpected token ‘import’" error while running Jest tests.

How to fix "Unexpected token ‘import’" error while running Jest tests?

To fix "Unexpected token ‘import’" error while running Jest tests, we can set transformIgnorePatterns to include the modules that raised this error so that Jest can parse the file.

For instance, we write

transformIgnorePatterns: ["/node_modules/(?!vue-awesome)"],

to include the vue-awesome Node module with the Jest transformation.

This would transform any ES6 import to something Jest can parse.

Conclusion

To fix "Unexpected token ‘import’" error while running Jest tests, we can set transformIgnorePatterns to include the modules that raised this error so that Jest can parse the file.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *