2026-03-27 14:50:34 +09:00
|
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
|
import Fastify from 'fastify';
|
|
|
|
|
|
|
|
|
|
// Mock mongoose and awilix before importing the plugin
|
|
|
|
|
vi.mock('mongoose', () => ({
|
|
|
|
|
default: {
|
|
|
|
|
connect: vi.fn().mockResolvedValue(undefined),
|
|
|
|
|
disconnect: vi.fn().mockResolvedValue(undefined),
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock('@fastify/awilix', () => ({
|
|
|
|
|
diContainer: {
|
|
|
|
|
register: vi.fn(),
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
2026-05-19 11:06:03 +09:00
|
|
|
import mongoosePlugin from '../../src/plugins/mongoose.plugin.js';
|
2026-03-27 14:50:34 +09:00
|
|
|
import mongoose from 'mongoose';
|
|
|
|
|
import { diContainer } from '@fastify/awilix';
|
|
|
|
|
|
|
|
|
|
describe('mongoose.plugin', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('connects to MongoDB on registration', async () => {
|
|
|
|
|
const app = Fastify({ logger: false });
|
|
|
|
|
await app.register(mongoosePlugin);
|
|
|
|
|
await app.ready();
|
|
|
|
|
|
|
|
|
|
expect(mongoose.connect).toHaveBeenCalled();
|
|
|
|
|
expect(diContainer.register).toHaveBeenCalled();
|
|
|
|
|
|
|
|
|
|
await app.close();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('disconnects from MongoDB on close', async () => {
|
|
|
|
|
const app = Fastify({ logger: false });
|
|
|
|
|
await app.register(mongoosePlugin);
|
|
|
|
|
await app.ready();
|
|
|
|
|
await app.close();
|
|
|
|
|
|
|
|
|
|
expect(mongoose.disconnect).toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
});
|