Last active
January 31, 2024 19:14
-
-
Save Dionid/fb682082d24159b195fdc4b478bf8013 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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