Issue with mssql.connect await causing timeout in Jest

Testing some functionality in a nextjs Typescript project that needs to interact with the database directly, not relying on mocked sql query results. But encountering issues with it actually writing to the database.

A brief example of the problem:

/* @/lib/db.ts */
import sql, { ConnectionPool } from "mssql";

const sqlConfig: sql.config = {
  user: process.env.DB_USER,
  password: process.env.DB_PWD,
  database: process.env.DB_NAME,
  server: process.env.DB_HOST!,
  pool: {
    max: parseInt(process.env.MAX_DB_CONNECTIONS || "10"),
    min: 0,
    idleTimeoutMillis: 30000
  },
  options: {
    encrypt: true, // for azure
    trustServerCertificate: true // switch to true for local dev / self-signed certs
  }
};

if (!global.db) {
  global.db = { pool: null };
}

export async function connectToDatabase(): Promise<ConnectionPool> {
  if (!global.db.pool) {
    console.log("No pool available, creating new pool."); 
    const pool = await sql.connect(sqlConfig);
    global.db.pool = pool;
    console.log("Created new pool."); 
    
  }
  return global.db.pool!;
}
/* db.test.ts */

import { connectToDatabase } from "@/lib/db";
// Tried setting high timeout as connection shouldn't take long in development
jest.setTimeout(30000);

describe("Database", ()=>{
  it("can connect", async()=>{
    const pool = await connectToDatabase();
    expect(1).toBe(1);
  });
});

export {};

The issue lies in the connectToDatabase() Promise not fulfilling during testing (it also doesn't reject when credentials are incorrect).

Verified that environment data is correctly read by outputting sqlConfig. The functions in db.ts work fine in development.

Any insights on why this might be failing in jest?

Answer №1

Great news, I've located the solution:

Upon a thorough examination of the error log, it became apparent that I had only reviewed the lower portion (disregard the discrepancy in file names from the original inquiry):

  ControllingTableLock
    ✕ works (30016 ms)

  ● ControllingTableLock › works

    ReferenceError: setImmediate is not defined

      at node_modules/mssql/lib/base/connection-pool.js:402:9
          at Array.forEach (<anonymous>)
      at node_modules/mssql/lib/base/connection-pool.js:401:26

  ● ControllingTableLock › works

    thrown: "Exceeded timeout of 30000 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

       5 |
       6 | describe("ControllingTableLock", ()=>{
    >  7 |   it("works", async()=>{
         |   ^
       8 |     const pool = await connectToDatabase();
       9 |     expect(1).toBe(1);
      10 |     return;

Further investigation revealed that

setImmediate is not defined

is an issue arising from jest using the jsdom environment instead of node.

The resolution was straightforward: I included

/*
* @jest-environment node
*/

at the top of the test file before the imports.

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

Issue with setting cookies in Node.js using Express

Recently I made the switch from regular JavaScript to TypeScript for my project. Everything seems to be functioning properly, except for session handling. This is the current setup of my project: Server.ts App.ts /db/mongo/MongoHandler.ts and some other ...

What purpose does the # symbol serve in Angular when interpolating values?

Here is an example provided from the following link: https://angular.io/guide/lifecycle-hooks export class PeekABoo implements OnInit { constructor(private logger: LoggerService) { } // Implementing OnInit's `ngOnInit` method ngOnInit() { this ...

Issue: The data type 'void' cannot be assigned to the data type 'ReactNode'

I am encountering an issue while calling the function, this is just for practice so I have kept everything inside App.tsx. The structure of my class is as follows: enum Actor { None = '', } const initializeCard = () => { //some logic here ...

Is there a way to retrieve all properties within a Typescript Class that includes optional properties?

If we have a scenario where: class ExampleType { item1?: string, item2?: string } and all the properties are OPTIONAL. Is there a way to generate an array of property names like: ["item1", "item2"] I attempted to use console.log( ...

Can getters and setters be excluded from code coverage reports in Angular projects?

Looking to clean up my coverage reports for the front end portion of an angular project by removing trivial code like getters and setters. I generate my reports using npm run test-sonar -- --coverage, but everything is included in the report when I view ...

Guidelines for implementing a seamless transition effect on elements with Tailwind CSS

I successfully created a responsive nav bar using Tailwind CSS in Next.js, and it is functioning perfectly. https://i.stack.imgur.com/yTCbs.png https://i.stack.imgur.com/rdXhL.png https://i.stack.imgur.com/J8fM6.png Everything works well, except when I ...

Exploring the world of mocking tests using Jest and inputs

Is there a way to create a jest test specifically for this function? const input = require('prompt-sync')(); export function choices(): void { const choice = input("Choose a letter"); if (choice === "a") { con ...

An error occurred: The property 'toUpperCase' cannot be read of an undefined Observable in an uncaught TypeError

Encountering an issue during the development of a mobile app using the ionic framework for a movie application. After finding an example on Github, I attempted to integrate certain functions and designs into my project. The problem arises with the 'S ...

Mastering AgGridReact Testing: A Comprehensive Guide Using Jest and Enzyme

Seeking guidance on testing AgGridReact with Jest/Enzyme. I'm attempting to simulate the onGridReady callback that should trigger automatically, but it's not working as expected. Below is a simplified version of my test scenario: import React fr ...

An issue has occurred when attempting to import ES Module for setting all values to an object

Working on a project in Angular 6, I encountered an issue with using the npm package object-set-all-values-to version 3.9.45. Here's what I did: 1- Successfully installed it using npm i object-set-all-values-to ✔️ OK 2- However, when trying to i ...

Error message encountered: The object contains unknown key(s): 'images' at "experimental" - Error in Next.js version 14.1.0

Currently, I am in the process of deploying my nextJs project on github pages and following a tutorial. I encountered an issue while trying to execute a pipeline in github, receiving an error message. Below is the code snippet from my next.config.mjs fil ...

What is the best way to explain the concept of type indexing in TypeScript using its own keys?

I'm still learning TypeScript, so please bear with me if my question sounds basic. Is there a way to specify the index for this type so that it utilizes its own keys rather than just being an object? export type TypeAbCreationModal = { [index: stri ...

Rewriting Next.js with custom headers

My web app allows users to create their own profiles with custom usernames, which can be accessed via the following URLs. ourplatform.com/john ourplatform.com/john/about ourplatform.com/john/contact ourplatform.com/jane ourplatform.com/jane/about ourplatf ...

What is the reason for the component that is imported conditionally not being displayed/render

I'm in the process of setting up a basic blog platform. My goal is to dynamically load a specific component based on the URL parameters (specifically, the id parameter). However, after implementing this code, the component always displays "Loading" ...

A warning has been issued: CommonsChunkPlugin will now only accept one argument

I am currently working on building my Angular application using webpack. To help me with this process, I found a useful link here. In order to configure webpack, I created a webpack.config.js file at the package.json level and added the line "bundle": "web ...

Do not display large numbers within an HTML card

I have this card here and displaying dynamic data inside it. The number is quite large, so I would like it to appear as 0.600000+. If a user hovers over the number, a tooltip should display the full number. How can I achieve this? Below is the HTML code ...

Resolving DOMException issue in Google Chrome: A Step-by-Step Guide

In my browser game, I am experiencing an issue with sound playback specifically in Google Chrome. Although the sound manager functions properly in other browsers, it returns a DOMException error after playing sounds in 50% of cases. I'm unsure what co ...

Search the database to retrieve a specific value from a multi-dimensional array

My database is structured as shown in the image below: I am attempting to retrieve tasks assigned to a specific user named "Ricardo Meireles" using the code snippet below: const getTasksByEmployee = async () => { setTasks([]); const query = qu ...

The act of employing `Function.prototype.run` within an Angular TypeScript class is deemed as illegitimate

Is there a way to globally define a new function called run within my Angular component as shown below? Function.prototype.run = function (delay: number) { // some content; }; However, the compiler shows an error that the Property 'run' does n ...

Angular 12: How to detect when a browser tab is closing and implement a confirmation dialog with MatDialog

I have a scenario where I am checking if the browser tab is closed using the code below. It currently works with windows dialog, but I would like to incorporate MatDialog for confirmation instead. @HostListener('window:beforeunload', ['$eve ...