I'm currently trying to write some tests for my Rest-API in NodeJS. For that i've used the Mongoose Memory Server until now and it worked smoothly. But now i've added a new model to my project and Mongoose is really struggling to insert the data into the memory db.
I'm always getting 'buffering timed out' as error when running the test.
That's my DB-Util which creates the connection before each test:
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;
And this is the test which won't work:
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());
});
});
Can someone tell me what i'm doing wrong?
Anyone here with an idea?
Thanks in advance.