Actualmente estoy tratando de escribir algunas pruebas para mi Rest-API en NodeJS. Para eso, he usado Mongoose Memory Server hasta ahora y funcionó sin problemas. Pero ahora agregué un nuevo modelo a mi proyecto y Mongoose realmente está luchando para insertar los datos en la base de datos de la memoria.
Siempre obtengo un error de "tiempo de espera agotado" cuando ejecuto la prueba.
Ese es mi DB-Util que crea la conexión antes de cada prueba:
const mongoose = require("mongoose"); const { MongoMemoryServer } = require("mongodb-memory-server"); // Extend the default timeout so MongoDB binaries can download jest.setTimeout(60000); // List all of your collection names here - I'll add some examples const COLLECTIONS = [ "users", "roles", "dashboards", "querydatas", "snapshots", "tabs", ]; class DBManager { constructor() { this.db = null; this.connection = null; } // Spin up a new in-memory mongo instance async start() { this.server = await MongoMemoryServer.create(); const url = await this.server.getUri(); await mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true, }); this.connection = mongoose.connection; } // Close the connection and halt the mongo instance stop() { this.connection.close(); return this.server.stop(); } // Remove all documents from the entire database - useful between tests async cleanup() { const collections = mongoose.connection.collections; for (const key in collections) { const collection = collections[key]; await collection.deleteMany(); } } } module.exports = DBManager;Y esta es la prueba que no funcionará:
afterAll(() => dbman.stop()); beforeAll(() => dbman.start()); afterEach(() => dbman.cleanup()); beforeEach( () => (this.grafanaServiceMock = rewire("../../services/grafana.service")) ); describe("working with snapshots", () => { it("should create a snapshot", async () => { this.grafanaServiceMock.__set__("grafanaRestService", { createSnapshotForDashboard: () => { return Promise.resolve(getSnapshotData()); }, getSnapshotByKey: () => { return Promise.resolve(getSnapshotByKeyData()); }, }); const snapshot = await this.grafanaServiceMock.createSnapshot( getDashboardData(), 4 ); expect(snapshot).toEqual(getCreatedSnapshotData()); }); });¿Alguien puede decirme qué estoy haciendo mal?
¿Alguien aquí con una idea?
Gracias por adelantado.