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

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

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

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

To fix the “Unexpected token ‘import’” error while running Jest tests, we should add the module that’s causing the error to the transformIgnorePatterns array.

For instance, we write

module.exports = {
  //...
  transformIgnorePatterns: ["/node_modules/(?!vue-awesome)"],
  //...
};

in jest.config.js to add the "/node_modules/(?!vue-awesome)" path to the transformIgnorePatterns array if the vue-awesome module is causing the “Unexpected token ‘import’” error.

This will make Jest ignore the module when it’s doing its transformations.

Conclusion

To fix the “Unexpected token ‘import’” error while running Jest tests, we should add the module that’s causing the error to the transformIgnorePatterns array.