Skip to content

Instantly share code, notes, and snippets.

@Dionid
Last active January 31, 2024 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dionid/fb682082d24159b195fdc4b478bf8013 to your computer and use it in GitHub Desktop.
Save Dionid/fb682082d24159b195fdc4b478bf8013 to your computer and use it in GitHub Desktop.
import path from "path";
import { DockerCompose } from "libs/docker-compose";
import { isPortReachable } from "libs/is-port-reachable";
import * as dockerCompose from "docker-compose";
import knex from "knex";
import { TestConfig } from "./global-config";
module.exports = async () => {
const dirname = __dirname;
const testConfig = TestConfig();
console.time("global-setup");
if (!testConfig.isInfraSkipped) {
const dockerDefaultOptions = {
cwd: path.join(dirname),
log: true,
};
// Speed up during development, if already live then do nothing
const isRMQReachable = await isPortReachable(
testConfig.rabbitMQ.hostname,
testConfig.rabbitMQ.port
);
if (!isRMQReachable) {
// ️️️Start the infrastructure within a test hook - No failures occur because the DB is down
await dockerCompose.upAll(dockerDefaultOptions);
await DockerCompose.waitUntilLog(
testConfig.rabbitMQ.containerName,
"Server startup complete",
{
dockerOptions: {
...dockerDefaultOptions,
log: false,
},
}
);
}
if (!testConfig.isCI) {
// . Build migrations
await dockerCompose.buildAll({
log: true,
cwd: path.join(dirname, "../../../databases/main"),
});
}
// . Wait till migrations passed
const resMigrations = await dockerCompose.upAll({
...dockerDefaultOptions,
config: path.join(dirname, "./docker-compose.migrations.yaml"),
});
if (resMigrations.exitCode === 1) {
throw new Error(
`Migrations has completed with error: ${resMigrations.err}`
);
}
const resFixtures = await dockerCompose.upAll({
...dockerDefaultOptions,
config: path.join(dirname, "./docker-compose.fixtures.yaml"),
});
if (resFixtures.exitCode === 1) {
throw new Error(
`Migrations has completed with error: ${resMigrations.err}`
);
}
// . Test connection to PG
const testMainMDB = knex({
client: "pg",
connection: testConfig.pgConnection,
});
await testMainMDB.destroy();
// 👍🏼 We're ready
console.timeEnd("global-setup");
}
// 👍🏼 We're ready
console.timeEnd("global-setup");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment