Files
ruflo/examples/05-swarm-apps/rest-api-advanced/tests/setup.js
T
2025-06-13 23:38:46 +00:00

47 lines
1.1 KiB
JavaScript

// Test setup and configuration
const { MongoMemoryServer } = require('mongodb-memory-server');
const mongoose = require('mongoose');
let mongod;
// Setup before all tests
beforeAll(async () => {
// Create an in-memory MongoDB instance
mongod = await MongoMemoryServer.create();
const uri = mongod.getUri();
// Connect to the in-memory database
await mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
});
// Cleanup after each test
afterEach(async () => {
// Clear all collections
const collections = mongoose.connection.collections;
for (const key in collections) {
const collection = collections[key];
await collection.deleteMany({});
}
});
// Cleanup after all tests
afterAll(async () => {
// Disconnect and stop the in-memory MongoDB instance
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
await mongod.stop();
});
// Global test timeout
jest.setTimeout(30000);
// Mock console methods in test environment
global.console = {
...console,
error: jest.fn(),
warn: jest.fn(),
log: jest.fn(),
};