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

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.