-
Notifications
You must be signed in to change notification settings - Fork 0
/
runIntegrationTests.js
104 lines (90 loc) · 2.76 KB
/
runIntegrationTests.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const { exec } = require("child_process");
const path = require("path");
const dotenv = require("dotenv");
dotenv.config({ path: ".env.test" }); // Load environment variables
const execPromise = (command) =>
new Promise((resolve, reject) => {
const process = exec(command, { cwd: path.resolve(__dirname) });
process.stdout?.on("data", (data) => {
console.log(data.toString());
});
process.stderr?.on("data", (data) => {
console.error(data.toString());
});
process.on("exit", (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`Command failed with exit code ${code}`));
}
});
});
const startDocker = async () => {
console.log("Starting Docker container...");
await execPromise("docker-compose -f docker-compose.test.yml up -d test_db");
console.log("Waiting for PostgreSQL to be ready...");
// Wait for a few seconds to give PostgreSQL time to start
await new Promise((resolve) => setTimeout(resolve, 15000)); // Increase to 15 seconds
try {
await execPromise(
"docker-compose -f docker-compose.test.yml exec -T test_db pg_isready -U testuser -h localhost -p 5432",
);
console.log("PostgreSQL is ready!");
} catch (error) {
console.error("Error checking PostgreSQL readiness:", error);
console.log("PostgreSQL logs:");
await execPromise("docker-compose -f docker-compose.test.yml logs test_db");
throw error;
}
};
const applyMigrations = async () => {
console.log("Applying migrations...");
try {
await execPromise("npx prisma migrate deploy");
console.log("Migrations applied successfully.");
} catch (error) {
console.error("Error applying migrations:", error);
throw error;
}
};
const seedDatabase = async () => {
console.log("Seeding database...");
try {
await execPromise("npx prisma db seed");
console.log("Database seeded successfully.");
} catch (error) {
console.error("Error seeding database:", error);
throw error;
}
};
const runTests = async () => {
console.log("Running tests...");
try {
await execPromise("npx jest --colors");
console.log("Tests completed successfully.");
} catch (error) {
console.error("Tests failed:");
console.error(error);
throw new Error("Tests failed");
}
};
const stopDocker = async () => {
console.log("Stopping Docker container...");
await execPromise("docker-compose -f docker-compose.test.yml down");
};
const main = async () => {
let exitCode = 0;
try {
await startDocker();
await applyMigrations();
await seedDatabase();
await runTests();
} catch (error) {
console.error("Error during setup, migration, or tests:", error);
exitCode = 1;
} finally {
await stopDocker();
process.exit(exitCode);
}
};
main();