-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathjest.setup.js
More file actions
78 lines (67 loc) · 2.74 KB
/
jest.setup.js
File metadata and controls
78 lines (67 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { MongoMemoryServer } from 'mongodb-memory-server';
import mongoose from 'mongoose';
import Redis from 'ioredis-mock'; // Import the mock
import config from './src/config/config.js'; // Your app's config
import redisService from './src/config/redis.js'; // Your redis service
let mongod;
// Mock ioredis before any of your modules try to import the real one
// Correct way to mock ioredis
jest.mock('ioredis', () => {
// Require 'ioredis-mock' inside the factory function.
// This ensures it's resolved correctly during Jest's mocking phase.
const IORedisMock = require('ioredis-mock');
return IORedisMock; // The mock will replace the 'ioredis' module
});
beforeAll(async () => {
mongod = await MongoMemoryServer.create();
const uri = mongod.getUri();
config.DB_URL = uri; // Override DB_URL in your config for tests
// It's important to disconnect and reconnect mongoose if it was already connected
if (mongoose.connection.readyState !== 0) {
await mongoose.disconnect();
}
await mongoose.connect(uri);
// Re-initialize your Redis service with the mock if needed,
// or ensure it uses the mocked 'ioredis' when it initializes.
// Since we globally mocked 'ioredis', your redisService should pick up the mock.
// You might need to adjust your redisService to be more easily testable
// or ensure it re-initializes its client if it's already created one.
// For your current redis.js, it creates an instance on import.
// We need to ensure it uses the mocked Redis.
// A simple way is to re-assign its client or re-initialize.
// If your redisService.connect() can be called multiple times or re-initializes the client:
await redisService.disconnect(); // Disconnect if already connected
redisService.client = new Redis({
host: config.redis.host,
port: config.redis.port,
password: config.redis.password,
db: config.redis.db,
}); // Manually set the client to the mock instance
// Or, if your redisService.connect() re-creates the client with the (now mocked) ioredis:
// await redisService.connect();
});
afterAll(async () => {
await mongoose.disconnect();
if (mongod) {
await mongod.stop();
}
if (redisService && typeof redisService.disconnect === 'function') {
await redisService.disconnect();
}
jest.clearAllMocks(); // Clear all mocks after tests are done
});
// Optional: Clear database between each test
beforeEach(async () => {
const collections = mongoose.connection.collections;
for (const key in collections) {
const collection = collections[key];
await collection.deleteMany({});
}
// Clear Redis mock data before each test
if (
redisService.client &&
typeof redisService.client.flushdb === 'function'
) {
await redisService.client.flushdb();
}
});