Encountering an issue in my NestJS project when using mocha and chai: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: The file extension ".ts" is not recognized

My project setup process was quite simple, just followed these commands: npm i -g @nestjs/cli

nest new project-name npm install --save-dev mocha chai @types/mocha @types/chai

This is the structure of my package.json:

{
  "name": "project-name",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  ...

The tsconfig.json file looks like this:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    ...

The .mocharc.json configuration includes:

{
  "extension": [
    "ts"
  ],
  "spec": "test/**/*.ts",
  ...

Here's an example code snippet from my CalculatorService:

export class CalculatorService {
  add(a: number, b: number): number {
    return a + b;
  }
}

And the test case for the CalculatorService in calculator.service.spec.ts:

import { expect } from 'chai';
...

Error encountered while running npm test command.

If you'd like to contact me about this issue, please email me at [email protected]

Answer №1

After encountering a similar issue, I dedicated an entire day to delving into the depths of mocha and chai source code. Eventually, I discovered the root of the problem:

The latest version of Chai (v5) utilizes ESM modules, which causes compatibility issues with ts-node unless you opt for the experimental ts-node/esm loader.

Ultimately, the most effective solution is to revert back to Chai version 4.5.0

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

Simulating a typescript class import with enzyme for testing purposes

I need to conduct a test on this Typescript class using Jest, here is a snippet of the code: import OrderService from '../../../services/api/OrderService'; class InstallationOverview { // using OrderService somewhere // ... } When I use en ...

What is the process of declaring a global function in TypeScript?

I am looking to create a universal function that can be accessed globally without needing to import the module each time it is used. The purpose of this function is to serve as an alternative to the safe navigation operator (?) found in C#. I want to avoi ...

Encountering a problem where I am unable to input text in a textbox after making changes

I'm having trouble editing a text field. I can't seem to type anything when trying to edit. Strangely, everything works fine when adding a user, but the issue only arises during editing. Take a look at my code below - const initialState = { ...

What is the appropriate overload to be selected when utilizing a ref in the method call?

Encountering an unfamiliar TS error while working with the code snippet below: <script lang="ts"> import {defineComponent, computed, toRef} from 'vue' import _ from 'lodash' import {DateTime} from 'luxon' int ...

Tips on programmatically filtering angular lists

Is there a way to programmatically filter an Angular list? I'm currently working on a project where I need to filter subcategories by clicking on categories. For example, when clicking on "Drinks," I want to display items like Coke, Fanta, Pepsi... ...

Unable to click on link with JavaScript using Selenium webdriver

<a id="compareCompanies" b:onclick="needsController.showQuotes = true;" href="#">Compare companies</a> Below is the Selenium Webdriver JavaScript code using Mocha: driver.wait(function () { driver.findElement(webdriver.By.id("compareCompa ...

What is the best way to go about reading the .txt file and executing the query to add the records?

I have a .txt file containing an insert query with around 10,000 records. Below is an example: INSERT INTO tblVeiculo (VeiculoId, Codigo, Fabricante, Modelo, AnoInicial, AnoFinal, Portas, Combustivel, NrMotorObstruido) VALUES (1, '001034066', &a ...

What could be causing the absence of the mat-form-field on the screen?

Within my package.json file, I have specified the version @angular/material": "^13.3.8", however, I am currently utilizing version 13.3.7 along with Angular CLI: 13.3.7. After copying the necessary codes from Angular Material and importing M ...

Node.js allows for keeping pipe and sockets open even after streaming an HTTP response

My current challenge involves streaming data from an HTTP response to a cloud storage provider within an internal service. const response = await request<Readable>({ headers: httpOpts?.headers, data: httpOpts?.data, url, method, responseTyp ...

What are the steps to execute jest in an AWS Lambda environment?

I'm looking to execute my end-to-end test post-deployment for the ability to revert in case of any issues. I've followed the guidelines outlined in this particular blog post. Below is my lambda function: export async function testLambda(event: A ...

Adding jQuery and other libraries to Typescript for optimal functionality

After spending days researching and struggling, I am reaching out here for clarification on the process of importing a library in Typescript. I used to just add the script tag and everything would work fine. Now that I am working on building a MEAN-Stack ...

describing a schema for a mongoose model in a TypeScript environment

I am currently working on a function named createFactory, which requires two parameters - a model and a data object. The purpose of this function is to generate a document based on the provided data. const createFactory = (Model, obj: object) => { M ...

Tips for preventing duplication of the interface in Typescript React

Embarking on my first Typescript app, I am determined to maintain a structured approach by keeping styles and components in separate files. With an ambitious project scope of numerous components, I intend to utilize props for calling classes, ensuring each ...

Guide on posting an object in Angular through HTTP Post

I am attempting to send an object named Pack to my API Rest server using my Angular service. Below is the function I have set up for this task: save_pack(Pack: any){ return new Promise((resolve, reject) =>{ this.http .post("http://loca ...

Encountering an error in mapping loops using React and TypeScript interface

I'm currently working on implementing a map loop in JavaScript, and here's what I have so far: interface RoutesType { path: string; name: string; icon: string; layout: string; } This is the code for the map loop: // This function creat ...

Verify additional keys in the output of a function

Imagine this scenario: type Keys = 'x' | 'y' | 'z' type Items = { [K in Keys]?: number } let items: Items = { x: 1, y: 4 } The result is: Type '{ x: number; y: number; }' cannot be assigned to type 'Items' ...

Are you experiencing ERR_CONNECTION_REFUSED on both Chrome and Firefox?

I'm currently working on an Angular2 website. Everything is running smoothly in Internet Explorer, but I am encountering an ERR_CONNECTION_REFUSED error when trying to access it from Chrome and Firefox. Here is the code snippet: This is the content ...

execute a rigorous compilation during the ng build angular process

I am currently working on a project using angular-cli and I have configured my package.json with the following scripts: "scripts": { "ng": "ng", "build": "ng build --base-href /test/", "prod": "ng build --prod --base-href /test/" } According to the ...

Angular 8 HttpClient fails to populate array on initialization

I am currently facing an issue where I am trying to populate an array from a JSON file using HttpClient. Below is the code snippet that I am using, utilizing a simple HttpClient service in Dr: getData() { this.dr.getData().subscribe(data => { for ...

The problem encountered with the Enzyme mount API is a TypeError where it is unable to read the property 'find' of an undefined variable

After converting my component to a stateless one, I started encountering an error that says: TypeError: Cannot read property 'find' of undefined Previously, my tests were running smoothly before the switch. As far as I know, you can test functio ...