DEV Community

Philippe Arteau
Philippe Arteau

Posted on

Fixing Jest import failure

If you are reading this post, you likely encounter this error:

> 4 | import someConfig from '../config/SomeConfigs.json' with { type: 'json' }; 
    |                                                                ^
  5 |
  6 | /**

Add @babel/plugin-syntax-import-attributes (https://github.com/babel/babel/tree/main/packages/babel-plugin-syntax-import-attributes) to the 'plugins' section of your Babel config to enable parsing.

If you already added the plugin for this syntax to your config, it's possible that your config isn't being loaded.
You can re-run Babel with the BABEL_SHOW_CONFIG_FOR environment variable to show the loaded configuration:
Enter fullscreen mode Exit fullscreen mode

The issue in my case was to create a babel configuration file for Jest to use when transpiling the different source files.

babel.config.js (or babel.config.cjs)

module.exports = api => {
    const isTest = api.env('test');

      return {
        plugins: [
          "@babel/plugin-syntax-import-attributes",
        ],
      }

  };
Enter fullscreen mode Exit fullscreen mode

Source

I took inspiration from this blog post which described issues with React dependencies in Jest unit tests.

This complete example uses the conditional configuration suggested in Jest documentation.

module.exports = api => {
  const isTest = api.env('test');
  // You can use isTest to determine what presets and plugins to use.

  return {
    // ...
  };
};
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay