'TypeError: Cannot read property 'dropDatabase' of undefined

import request from "supertest";
import app from "../../server";
import { basicSetup } from "../helpers/db.helper";

describe("Post Deck EndPoint", () => {
  basicSetup();
  it("should create a new deck", async () => {
    const res = await request(app).post("/deck").send({
      shuffled: true,
      type: "SHORT",
    });
    expect(res.statusCode).toEqual(201);
    expect(res.body).toHaveProperty("deckId");
  });
});

###################################################

import mongoose from "mongoose";

export const basicSetup = () => {
  beforeEach((done) => {
    mongoose.connect("mongodb://localhost:27017/deck-of-cards", () => done());
  });

  afterEach((done) => {
    mongoose.connection.db.dropDatabase(() => {
      mongoose.connection.close(() => done());
    });
  });
};

when i run npm test it gives me error: TypeError: Cannot read property 'dropDatabase' of undefined

My code is pushed in following repository: https://github.com/madeeha96/Cards-Node-API



Solution 1:[1]

Have you tried to remove the basiSetup(); function inside the describe function and put it above it. like so :

 basicSetup();
describe("Post Deck EndPoint", () => {.....})

and also try and see if your mongodb server is on

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chubukas