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)
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 mypackage.json: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:
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.tsto:On
transformI changed to:Before it was:
then, in
jest.setup.ts, I added the following code:was able solve issue by downgrading node to
v18.19.1fromv18.20.4The deprecated
syntax-import-assetionswas required by thenext/babelpreset, which conflicts with thesyntax-import-attributesplugin. 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 enjoynext/babeleven for jest.@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
npm install --save-dev @babel/preset-react