-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjest.setup.ts
More file actions
55 lines (45 loc) · 1.45 KB
/
jest.setup.ts
File metadata and controls
55 lines (45 loc) · 1.45 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
import { Db, MongoClient } from 'mongodb';
import { getClient, getDatabase } from '@utils/mongodb/mongoClient.mjs';
import migrate from 'migrate-mongo';
import migrationConfig from './migrate-mongo-config';
import fs from 'fs';
import path from 'path';
let client: MongoClient;
let db: Db;
beforeAll(async () => {
client = await getClient();
db = await getDatabase();
await db.dropDatabase();
await migrate.config.set(migrationConfig);
try {
await applyTestMigrations(db, client);
} catch (error) {
console.error('Error migrating up:', error);
}
});
afterAll(async () => {
try {
await migrate.down(db, client);
} catch (error) {
console.error('Error migrating down:', error);
}
await client.close();
});
export { db };
// function to run create before update migrations in test env
async function applyTestMigrations(db: Db, client: MongoClient) {
const migrationDir = path.join(__dirname, './migrations');
const migrationFiles = fs.readdirSync(migrationDir);
// separate create migrations from others
const createMigrations = migrationFiles.filter((file) =>
file.includes('create')
);
const otherMigrations = migrationFiles.filter(
(file) => !file.includes('create')
);
const orderedMigrations = [...createMigrations, ...otherMigrations];
for (const migrationFile of orderedMigrations) {
const migration = await import(path.join(migrationDir, migrationFile));
await migration.up(db, client);
}
}