Having trouble setting up mongodb-memory-server 8 to work with jest

I am currently working on integrating the latest version of mongodb-memory-server with jest on a node express server. While following the guide provided in the mongodb-memory-server documentation (), I encountered some gaps that I am struggling to fill in.

To document my progress, I have created a repository with my attempts to piece everything together: https://github.com/jimmythecode/mongodbmemoryserver-guide

Despite my efforts, I have been unable to find any updated instructions online for the current version of mongodb-memory-server. Any assistance would be greatly appreciated.

Answer №1

Below is the configuration I am using with the "mongodb-memory-server" version "^8.5.2" and "jest" version "^28.1.0". Please review

import { MongoMemoryServer } from "mongodb-memory-server";
import mongoose from "mongoose";


let mongo: any;
beforeAll(async () => {
  process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

  mongo = await MongoMemoryServer.create();
  const mongoUri = await mongo.getUri();

  await mongoose.connect(mongoUri);
});

beforeEach(async () => {
  const collections = await mongoose.connection.db.collections();

  for (let collection of collections) {
    await collection.deleteMany({});
  }
});

afterAll(async () => {
  jest.setTimeout(20000)
  await mongo.stop();
  await mongoose.connection.close();
});

Answer №2

Success! I was able to get everything up and running smoothly. For detailed instructions, check out the latest updates in this branch. Hopefully, this can be of assistance to others facing the same 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

What is the C sharp version of this code structure?

I'm curious to know what the C# syntax is for declaring a property like this: filters: { [arg: string]: string }; ...

When employing Pug, static files are not served by Express

Having trouble serving static files with Express and Pug as the templating engine. My assets are not loading correctly. Directory structure : +front +views +login index.pug +images +js +css ... server.js Server code : ap ...

How can I resolve a bug in Nuxt2 when using TypeScript?

I need help with implementing code using Nuxt.js 2 option API with TypeScript. computed: { form: { get: () => this.value, set: (value) => this.$emit('input', value) } } Additionally, I am encountering the fo ...

An unexpected error has occurred: Uncaught promise rejection with the following message: Assertion error detected - The type provided is not a ComponentType and does not contain the 'ɵcmp' property

I encountered an issue in my Angular app where a link was directing to an external URL. When clicking on that link, I received the following error message in the console: ERROR Error: Uncaught (in promise): Error: ASSERTION ERROR: Type passed in is not Co ...

Launching a Phonegap app using Node.js and Express framework

My goal is to transform my current webapp into a mobile app. The existing setup consists of a nodejs-express REST API server with an HTML/JS client. I aim to utilize the API from the REST server and replace the client with a phonegap/cordova based mobile ...

Inquiry regarding the return value of 'async-lock' in nodejs

I am utilizing the async-lock module in my typescript project to handle concurrency. However, I am encountering difficulties with returning the result within lock.acquire(...) {...}. Any guidance on how to resolve this issue would be greatly appreciated. ...

Interacting with User Input in React and TypeScript: Utilizing onKeyDown and onChange

Trying to assign both an onChange and onKeyDown event handler to an Input component with TypeScript has become overwhelming. This is the current structure of the Input component: import React, { ChangeEvent, KeyboardEvent } from 'react' import ...

Ensuring that Express app.use is triggered with a middleware function: A guide using Mocha

I am faced with the challenge of testing whether app.use is being called with middleware. The issue arises when app.use is called with functions that return other functions, making it difficult to conduct proper testing. app-factory.js const express = re ...

Docz: Utilizing Typescript definitions for props rendering beyond just interfaces

We are currently using Docz to document our type definitions. While it works well for interfaces, we've run into an issue where rendering anything other than interfaces as props in Docz components doesn't seem to display properly. I'm seeki ...

Experiencing a lack of response from the Express.js server

I have been encountering an issue while attempting to make a POST request to my server running on port 5000. Although the server is up and running, I am not receiving any response. Below is the content of my app.js file import express from "express&q ...

Leveraging a NodeJS application for efficiently handling complex operations on Azure virtual machines

I have developed a NodeJS express API that interacts with Azure blob storage to process files. When making a POST request to this API, I only include the filename in the body. The API retrieves data from the blob, creates a local file with that data, execu ...

Aggregate the values in an array and organize them into an object based on their frequency

I have an array of information structured like this: 0: { first: "sea", second: "deniz", languageId: "English-Turkish"} 1: { first: "play", second: "oynamak", languageId: "English-Turkish&qu ...

The TypeScript optional callback parameter is not compatible with the anonymous function being passed to it

Encountering an issue with TS callbacks and function signatures. Here is my scenario: ... //inside a class //function should accept a callback function as parameter refreshConnection(callback?: Function) { //do something //then ca ...

Tips for executing a SOAP request using NodeJs

Despite my efforts in researching various blogs, tutorials, and videos, I still can't find a clear answer on how to execute a RESTful request. For example, in NodeJs, you would code the request, hit the route (https://localhost/3000/api/getStudent), a ...

Encountering an error while implementing a Typescript addEventListener for keydown events

Struggling with adding and removing event listeners to HTML elements capable of focus, such as buttons. encountering a typescript error specifically related to the lines of code responsible for adding and removing the event listener: focusableElements.fo ...

When using TypeScript's array intersection type, properties are not accessible when using methods like Array.forEach or Array.some. However, they can be accessed within a for loop

It was challenging to search for this problem because I may not have the correct technical terms, but I hope my example can help clarify it. Background: I am using query data selectors in react-query to preprocess query results and add some properties tha ...

Applying limits and sorting to populated sub-documents in Mongoose

Having trouble sorting and limiting a nested sub doc, I've explored various solutions without success. System const systemSchema = mongoose.Schema({ data: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Data' }], ...other }) ...

Best practices for extending the Array<T> in typescript

In a discussion on extending the Static String Class in Typescript, I came across an example showing how we can extend existing base classes in typescript by adding new methods. interface StringConstructor { isNullOrEmpty(str:string):boolean; } String. ...

Encountering an issue with managing promises in Observables for Angular HTTP Interceptor

Currently, I am encountering the following situation: I have developed an authentication service using Angular/Fire with Firebase authentication. The authentication service is expected to return the ID token through the idToken observable from Angular/Fir ...

Comparing the installation of TypeScript on Ubuntu utilizing npm versus native packages (via apt)

I'm looking to incorporate Typescript into my Ubuntu system and have come across two different methods to do so: Using sudo apt update && sudo apt install node-typescript -y Running sudo npm install -g typescript My main question revolves ar ...