Jest is unable to handle ESM local imports during resolution

I am encountering an issue with my Typescript project that contains two files, a.ts and b.ts. In a.ts, I have imported b.ts using the following syntax:

import * from "./b.js"

While this setup works smoothly with Typescript, Jest (using ts-jest) throws an error:

Cannot find module './b.js' from 'a.ts'

Is there a way to configure Jest to recognize the .js import as a .ts file during testing?

Answer №1

Just found the solution. I needed to include the following code in my jest.config.ts

moduleNameMapper: {
   '^(\\.{1,2}/.*)\\.js$': '$1',
},

This code maps filenames with the extension .js to a version without an extension, allowing Jest (specifically ts-node under the hood) to locate them.

Additionally, it is essential to have preset: 'ts-jest', although I had already included that previously.

Answer №2

There are several potential causes for the "module not found" error in TypeScript:

  1. The package may not be properly installed.
  2. Issues with the node_modules directory could be the culprit.
  3. A typo in the package import statement might be causing the problem.
  4. Errors in your tsconfig.json configuration file could lead to this issue.

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

Testing Angular applications using Karma

After utilizing the yo angular 1 generator, it generated a landing page and some tests. However, I am facing an issue when trying to compile the code in VS as I receive an error stating that "module('fountainFooter');" is undefined. /// <refe ...

What could be the reason for receiving an undefined user ID when trying to pass it through my URL?

Currently, I am in the process of constructing a profile page and aiming to display authenticated user data on it. The API call functions correctly with the user's ID, and manually entering the ID into the URL on the front end also works. However, wh ...

Looking to locate or track duplicate values within a multi-dimensional array?

In a multidimensional array, I am looking to detect and count duplicates. If duplicates are found, an alert should be triggered. Arr =[[2,"sk"],[3,"df"],[7,"uz"],[3,"df"],[7,"gh"]] Suggestions: One way to ...

Troubleshooting React TypeScript: Resolving the Error "Argument of type ''""' is not assignable to parameter of type 'SetStateAction<undefined>'"

Currently, I am troubleshooting a React application that was extracted from a live server and now I am attempting to run it on my local machine. However, upon starting up the application locally, I encountered the following error message: Argument of ty ...

The yarn/npm package manager seems to be utilizing outdated code in an inexplicable way when paired with mocha and typescript

I recently encountered a strange issue that has left me scratching my head. When I manually run my test command, I receive two test results. However, when I execute the same command in a yarn/npm script, only one result is displayed. Has anyone else experi ...

Unable to use the unlink function when using a relative path

In my website backend, I am using the express framework. There is an express public folder where user-uploaded images are stored in an upload folder within the public directory. Additionally, there is a routes folder outside the public directory where I cr ...

Leveraging Expose in combination with class-transformer

I have a simple objective in mind: I need to convert the name of one property on my response DTO. refund-order.response.dto.ts export class RefundOrderResponseDto { @Expose({ name: 'order_reference' }) orderReference: string; } What I w ...

What are the recommended guidelines for using TypeScript effectively?

When facing difficulties, I have an array with functions, such as: this._array = [handler, func, type] How should I declare this private property? 1. Array<any> 2. any[] 3. T[] 4. Array<T> What is the difference in these declarations? ...

What steps can be taken to eliminate a npm install error?

I have been attempting to execute the following project: https://github.com/kentcdodds/react-in-angular This repository serves as an illustration of incorporating React into AngularJS. It consists of three tags that demonstrate the process of transitio ...

Having trouble retrieving data from the database when passing input to a mongoose query using an href tag in Node.js

Welcome to My Schema const AutomationSchema=new mongoose.Schema( {EventName:String, EventDate:String, EventLocation:String, EventDetails:String } ) The Events Model const EventsModel=new mongoose.model("Events",AutomationSchema ...

Creating Algorithms for Generic Interfaces in TypeScript to Make them Compatible with Derived Generic Classes

Consider the (simplified) code: interface GenericInterface<T> { value: T } function genericIdentity<T>(instance : GenericInterface<T>) : GenericInterface<T> { return instance; } class GenericImplementingClass<T> implemen ...

"Stay away from potential updates that could cause issues with npm-check-updates (ncu)

When using the npm-check-update command, major updates are displayed in red. This feature is visible in the initial image of the documentation. However, is there a method to prevent these disruptive changes from being included in the automatic update pro ...

The absence of color gradations in the TypeScript definition of MUI5 createTheme is worth noting

Seeking to personalize my theme colors in MUI5 using TypeScript, I am utilizing the createTheme function. This function requires a palette entry in its argument object, which TypeScript specifies should be of type PaletteOptions: https://i.stack.imgur.com ...

Developing an Angular 2 Cordova plugin

Currently, I am in the process of developing a Cordova plugin for Ionic 2. The plugin is supposed to retrieve data from an Android device and display it either on the console or as an alert. However, I am facing difficulty in displaying this data on the HT ...

the hidden input's value is null

I am encountering an issue with a hidden input in this form. When I submit the form to my API, the value of the input is empty. Isbn and packId are both properties of a book model. However, for some reason, the value of packId is coming out as empty. & ...

Retrieve information from mongodb and transform it into a PDF document

Currently, I am in the process of working on a project that involves using NODE JS and Mongoose. My goal is to extract data from my MongoDB, such as (all customers stored in my MongoDB), and then create a pdf file using this data. Here is an example of wha ...

Using Angular CLI with ES6 instead of TypeScript for your development needs can offer a

Is there a way to utilize an ES6 transpiler such as Babel instead of TypeScript in an Angular CLI project? Are there any specific flags for specifying the script language, similar to using --style? Thank you. ...

Tips for bypassing an argument using the POST method in NodeJS

Hey there! I started off by creating a question about passing over data using the GET method, but now I'm facing a new problem when trying to pass over data with the POST method. Below is my code snippet where things seem to be going wrong. My goal is ...

Encountering issues when trying to publish to MongoDB

Hi everyone, I am a beginner in MEAN stack development and I'm facing an issue while trying to make a post request using Postman. The error message I'm getting is "username not defined". I have successfully established a connection with mongodb a ...

Transferring Parameters to EJS Template in Node.js

Hey guys, I'm having an issue with rendering a MongoDB record in my .ejs file. Strangely, when I console.log the variable before the end of my route, I get the expected value. However, it becomes undefined in the .ejs file. Here is the code snippet: ...