Encountering challenges during the transition from Jest@26 to Jest@27

I'm currently in the process of upgrading Jest from version 26 to 27 for my Next.js 13 project, but I've encountered an error when running tests after the update. The error message is as follows:

 FAIL  src/__tests__/app.test.tsx
  ● Test suite failed to run

    Cannot combine importAssertions and importAttributes plugins.

      at validatePlugins (node_modules/@babel/parser/src/plugin-utils.ts:130:11)
      // more error details here...

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.683 s

Prior to updating Jest, the test was passing without any issues, so it seems to be related to the Jest upgrade itself rather than my code. Here is what my jest.config.js file looks like:

// jest configuration details...

And this is my babel.config.js content:

// babel configuration details...

Furthermore, here is a snippet from my package.json:

// package.json dependencies and devDependencies snapshot...

If someone could provide assistance on resolving this issue, it would be greatly appreciated!

Answer №1

This error occurred for us because our babel configuration for jest tests was using next/babel, while nextjs 14 utilizes @babel/plugin-syntax-import-assertions. Jest, however, relies on the plugin "babel-preset-current-node-syntax" version "1.1.0", which also employs @babel/plugin-syntax-import-assertions.

The proposal to replace import assertions with import attributes offers greater flexibility:

To accommodate both old and new syntax, you can refer to this link:

The clash arises when Nextjs 14 incorporates import assertions, but by using next/babel preset in babel.config.js and applying jest-babel on top of it, import attributes are introduced through "babel-preset-current-node-syntax" version "1.1.0".

However, the transition from import assertions to babel-plugin-syntax-import-attributes with deprecatedassertsyntax has been featured in the latest next canary update:

We also noticed that resolutions property in yarn's package.json did not impact the installed version of @jest/core/node_modules/babel-preset-current-node-syntax.

To resolve this issue, we took the following steps:

yarn install <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="15213c35171310233e38173e37171d33331b25">[email protected]</a>

We removed the caret operator and pinned the version like so:

"babel-preset-current-node-syntax": "1.0.0",

Subsequently, we implemented the following script:


import { exec } from '@utils/scripting';
import { existsSync } from 'node:fs';

const JEST_BABEL_PRESET_CURRENT_NODE_SYNTAX_LOC = 'node_modules/@jest/core/node_modules/babel-preset-current-node-syntax';
const VERSION_ONE_BABEL_PRESET_CURRENT_NODE_SYNTAX_LOC = 'node_modules/babel-preset-current-node-syntax';
export const run = (): void => {
  if (!existsSync(`${process.cwd()}/${JEST_BABEL_PRESET_CURRENT_NODE_SYNTAX_LOC}`)) {
    throw new Error(`The file ${JEST_BABEL_PRESET_CURRENT_NODE_SYNTAX_LOC} does not exist.
        You probably upgraded jest and now you have to downgrade or read the comments in deploy/bin/patch-jest-import-assertions.ts`);
  }
  exec(`rm -rf ${JEST_BABEL_PRESET_CURRENT_NODE_SYNTAX_LOC}`);
  exec(`cp -R ${VERSION_ONE_BABEL_PRESET_CURRENT_NODE_SYNTAX_LOC} ${JEST_BABEL_PRESET_CURRENT_NODE_SYNTAX_LOC}`);
};

With these adjustments, next/babel will handle the addition of import attributes or import assertions as needed, without affecting our usage since we don't utilize either syntax.

Answer №2

We found the solution for our next.js application to be the following:

Include the babel.config.js file with the following content:

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

Additionally, make sure to install the necessary dev dependency:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to efficiently transfer a Tensorflow.js Tensor between Node.js processes?

Currently, I am in the process of building an AI model using Tensorflow.js and Node.js. One of the challenges I am facing is handling a large dataset in a streaming manner due to its size being too massive to be stored in memory all at once. This task invo ...

The AngularJS framework is failing to disable the autocomplete feature for the input field with a password type

I have attempted to disable auto-complete for the password input, but it doesn't seem to be working. Below is a sample of my code: <form name="testfrm" ng-submit="test(testfrm)" autocomplete="off"> <input type="password" id="passwor ...

Configuring a Meteor.js application requires defining variable scopes for templates in order to manage

Is there a way to define a variable that is limited in scope to a specific template? I want this variable to be accessible by multiple helpers within the template, but not outside of it. For example, how can the game variable be shared between two templat ...

Using Regex in Javascript to locate unfinished items that start and finish with brackets

Can anyone assist me in utilizing regex to identify any incomplete items that start with {{. I have attempted to search for instances in the string that begin with {{ and are followed by letters (a-Z) but do not end with }}. However, my attempts always re ...

There was an issue locating a declaration file for the module 'clarifai'

https://i.stack.imgur.com/PgfqO.jpg I recently encountered a problem after installing the Clarifai API for a face recognition project. Despite my efforts, I have been unable to find a solution. When I hover over "import clarifai," I receive the message: ...

The MomentJS difference calculation is incorporating an additional hour

I am currently working on a project that involves time calculations. const currentCETTime = moment.tz('2020-03-18 15:58:38', 'Europe/Madrid'); const limitCETTime = moment.tz('2020-03-18 18:00:00', 'Europe/Madrid') ...

My locale NUXT JavaScript files are being blocked by Content Security Policy

I've been working on implementing CSP headers for my website to ensure data is loaded from trusted sources. However, I'm facing an issue where CSP is blocking my local JS files. Here's a snippet from my nuxt.config.js: const self = 'lo ...

The callback function used for the database query did not provide any results

So here's a code snippet: module.exports.checkEmailInUse = (email) => { connection.query('SELECT `id` FROM `users` WHERE email = ?', [ email ], function(err, rows, fields) { console. ...

What caused its increase? Issue: Minified Redux error #12

Can anyone explain why I am encountering the following error? Error: Minified Redux error #12; I implemented Next.js React support after watching this tutorial video: https://www.youtube.com/watch?v=QIqvxQnPOCM //slices/User.ts import { Action, create ...

Tips for selecting React component props based on a specific condition

Here is a React component that I have: <SweetAlert show={this.props.message} success title={this.props.message} onConfirm={this.props.handleCloseAlert}> </SweetAlert> Upon using this component, I receive the following alert ...

Troubleshooting a problem with dynamic options in Materialize select set

I'm currently facing an issue with my two dependent dropdowns, country and state. When I select a country, I use JavaScript to populate the respective states in the state dropdown. However, even though the options are added correctly when I inspect th ...

Storing information in local storage as JSON using AngularJS

I am working on a form with the following fields: <form ng-submit="addState()"> <input ng-model="text1" type="text"> <input ng-model="text2" type="text"> <input ng-model="text3" type="text"> ...

Leveraging AngularJS with Angular Material or JQuery to showcase a custom window design

Seeking solutions on how to create a button like the FAB speed dial from angular material, or another option that displays a small window next to the clicked button. The small window should contain a text area and a button. Preferably looking to implement ...

Performing optimized searches in Redis

In the process of creating a wallet app, I have incorporated redis for storing the current wallet balance of each user. Recently, I was tasked with finding a method to retrieve the total sum of all users' balances within the application. Since this in ...

Attempted to create registrations for two views using the identical name RCTScrollView

Having trouble running my React Native app on iOS, I keep getting an error while the Android version works perfectly fine. Does anyone have any insight on this issue? XCode 11.5, RN 0.61.5, Using React Native CLI I've searched multiple sites but hav ...

React - Struggling to locate the source of the leak, suspecting an infinite state declaration

I am just starting out with React and I recently created my first app. However, I have encountered a problem where my app becomes very laggy after entering a value in the input field and clicking the add button at the bottom right. I suspect that I may hav ...

Obtaining Texture Map coordinates from an Object's surface in Three.js

Seeking a solution for mapping a 3D object in Three.js to a point on its surface and finding the corresponding point on a texture file using x,y coordinates. Currently, I am using raycasting to locate points on the object's face, each of which should ...

What is the method for configuring my bot to forward all logs to a specific channel?

const logsChannel = message.guild.channels.cache.find(channel => channel.name === 'logs'); I am looking to set up my bot to send log messages for various events, like member join/leave or message deletion, specifically in a channel named &apo ...

The AJAX load event listener triggers prior to receiving a response from the server

When working on a script to upload images via AJAX, the process is as follows: a user selects an image using a form file input field. The form contains an onsubmit function that prevents the page from reloading after submission. Within this function, data ...

Adjust the aesthetic based on whether the field is populated or empty

I have a simple text field on my website that triggers a search when the user inputs a value. I am wondering if it is possible to style the text field differently depending on whether it is empty or has content. Specifically, I want to change the border c ...