babel: [Bug]: Conflict between @babel/plugin-syntax-import-assertions and @babel/plugin-syntax-import-attributes in Jest tests

💻

  • Would you like to work on a fix?

How are you using Babel?

Other (Next.js, Gatsby, vue-cli, …)

Input code

No specific code triggering the issue, but the setup is as follows:

Monorepo with React, TypeScript, and Next.js Jest tests using babel-jest with the next/babel preset for transformation There are no import … assert statements in the codebase

Configuration file name

No response

Configuration

Jest Config:

module.exports = {
  transform: {
    "^.+\\.(ts|tsx)$": [
      "babel-jest",
      {
        presets: ["next/babel"],
      },
    ],
  },
  setupFilesAfterEnv: ["./test/setup-tests.ts"],
  moduleNameMapper: {
    ".+\\.(css|styl|less|sass|scss|jpg|png|ttf|woff|woff2)$": "identity-obj-proxy",
    "^.+\\.svg$": "jest-svg-transformer",
    "^@/(.*)$": "<rootDir>/$1",
  },
  snapshotSerializers: ["@emotion/jest/serializer"],
  collectCoverage: true,
  coverageDirectory: "coverage",
  testEnvironment: "jsdom",
};

Current and expected behavior

The Jest tests fail due to a conflict between @babel/plugin-syntax-import-assertions and @babel/plugin-syntax-import-attributes, despite neither of these plugins being explicitly included in the configuration.

Expected Behavior The tests should run successfully without loading conflicting Babel plugins since no import … assert syntax is used in the codebase.

Environment

Babel version(s): @babel/core@latest Node/npm version: Node v18 OS: macOS/Linux Monorepo setup with Next.js, React, TypeScript, Jest Jest version: 29.7.0 babel-jest version: 29.7.0

Possible solution

No response

Additional context

I’ve tried the following solutions:

Downgraded Babel dependencies to previous stable versions Cleared Jest and Babel caches Verified that no import … assert statements exist in the codebase Forced resolutions in package.json to remove the conflicting plugins

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 19 (3 by maintainers)

Most upvoted comments

omg. alright i fixed the issue with

in package.json:

“overrides”: { “@babel/parser”: “7.21.9”,

using old version of parser that doesn’t have the sub dependencies of @babel/plugin-syntax-import-assertions and @babel/plugin-syntax-import-attributes

make sure to delete node_modules and package-lock.json and do a fresh npm install

downgrading node to 18.19.1 was not an option for me since my app depends on higher version of node. I had to use node v20.17.0, cuz node 22.7 was incompatible, but 20.17 had no issues with compatibility.

cheers. took me forever.

I was able to resolve this issue and avoid moving away from next/babel (in my case @nx/next/babel) by adding the following resolution in my package.json:

"resolutions": {
    "babel-preset-current-node-syntax": "1.0.0"
},

In case it helps anyone 😉

For us, nextjs app as well, the problem seems to be related with “next/babel”.

So following @pkhaninejad approach, we changed the test environment in our .babelrc to not use “next/babel”.

Now it’s like this:

{
  "env": {
    "development": {
      "presets": [
        [
          "next/babel",
          {
            "transform-runtime": {
              "useESModules": false
            }
          }
        ]
      ]
    },
    "production": {
      "presets": [
        [
          "next/babel",
          {
            "transform-runtime": {
              "useESModules": false
            }
          }
        ]
      ]
    },
    "test": {
      "presets": [
        [
          "@babel/preset-env",
          {
            "targets": {
              "node": "current"
            }
          }
        ],
        "@babel/preset-react",
        "@babel/preset-typescript"
      ],
      "plugins": [
        "dynamic-import-node"
      ]
    }
  }
}

I believe if you manage to bump our app to next@14.3.0-canary.64 no need to move away from “next/babel”

I took advantage of @JoaoCEAlmeida’s resolution, and changed my jest.config.ts to:

import type { Config } from 'jest'

export const jestConfigBase: Config = {
  maxWorkers: 2,
  workerIdleMemoryLimit: 0.2,
  rootDir: '../../../',
  testEnvironment: 'jsdom',
  clearMocks: true,
  setupFilesAfterEnv: ['<rootDir>/tests/jest.setup.ts'],
  moduleNameMapper: {
    '\\.(scss|sass|css)$': 'identity-obj-proxy',
    '@root/(.*)': ['<rootDir>/$1'],
    '@src/(.*)': ['<rootDir>/src/$1'],
    '@public/(.*)': ['<rootDir>/public/$1'],
    '@shared/(.*)': ['<rootDir>/../shared/$1'],
  },
  transform: {
    '^.+\\.(js|jsx|ts|tsx)$': [
      'babel-jest',
      {
        presets: [
          [
            '@babel/preset-env',
            {
              targets: {
                node: 'current',
              },
            },
          ],
          '@babel/preset-react',
          '@babel/preset-typescript',
        ],
        plugins: ['dynamic-import-node'],
      },
    ],
  },
  coveragePathIgnorePatterns: [
    '<rootDir>/src/modules/SEOTags/json/',
    '<rootDir>/shared/components/ProgressBar/',
    '<rootDir>/shared/components/FormEdit/',
    '<rootDir>/src/modules/AutoInit/Welcome/json/',
    '<rootDir>/src/pages/blog/breadcrumb/',
    '<rootDir>/src/pages/blog/',
  ],
  transformIgnorePatterns: [
    'node_modules',
    '^.+\\.(css|sass|scss)$',
    '^.+config\\.(js|ts)$',
  ],
  testPathIgnorePatterns: [
    '<rootDir>/.next/',
    '<rootDir>/out/',
    '<rootDir>/src/pages/includes/',
    '<rootDir>/public/',
    '/node_modules/',
    'pacco/',
    '<rootDir>/src/pages/blog/',
    '<rootDir>/shared/components/Blog/',
  ],
}

export default {
  projects: ['<rootDir>/apps/*/tests/projects/*'],
}

On transform I changed to:

 transform: {
    '^.+\\.(js|jsx|ts|tsx)$': [
      'babel-jest',
      {
        presets: [
          [
            '@babel/preset-env',
            {
              targets: {
                node: 'current',
              },
            },
          ],
          '@babel/preset-react',
          '@babel/preset-typescript',
        ],
        plugins: ['dynamic-import-node'],
      },
    ],
  },

Before it was:

 transform: {
    "^.+\\.(js|jsx|ts|tsx)$": [
      "babel-jest",
      {
        presets: ["next/babel"],
      },
    ],
  },

then, in jest.setup.ts, I added the following code:

import React from 'react'
import '@testing-library/jest-dom';

global.React = React

was able solve issue by downgrading node to v18.19.1 from v18.20.4

The deprecated syntax-import-assetions was required by the next/babel preset, which conflicts with the syntax-import-attributes plugin. The upstream issue has been fixed by https://github.com/vercel/next.js/pull/65749. Please upgrade next.js to the latest version.

We could resolve similar issue updating next to "next": "^14.2.8". As a result we could enjoy next/babel even for jest.

  transform: {
    "^.+\\.(ts|tsx)$": [
      "babel-jest",
      {
        presets: ["next/babel"],
      },
    ],
  },

@Sephyrine you just saved my sanity haha

i apologize if it’s a basic question:

Is it possible that i have a working app, change nothing about it, package.json stays the same, then suddenly an error occurs (like this one)? if so, how does happen, is there code in the packages that call something external but the external has changed and isn’t compatible. thing is i reverted back to a previously working build from months ago, and it was still failing with same error.

For us(next.js app) this was the fix.

babel.config.js

module.exports = {
  presets: [
    '@babel/preset-env',
    '@babel/preset-react',
    '@babel/preset-typescript',
  ],
  plugins: ['@emotion/babel-plugin'],
};

npm install --save-dev @babel/preset-react