| 1 | import { MySqlContainer } from "@testcontainers/mysql" |
| 2 | import { PostgreSqlContainer } from "@testcontainers/postgresql" |
| 3 | import type { TestProject } from "vitest/node" |
| 4 | |
| 5 | export interface ClusterDatabases { |
| 6 | readonly mysql: string |
| 7 | readonly pg: string |
| 8 | } |
| 9 | |
| 10 | declare module "vitest" { |
| 11 | export interface ProvidedContext { |
| 12 | readonly clusterDatabases: ClusterDatabases |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | const makeMysqlContainer = () => |
| 17 | new MySqlContainer("mysql:lts").withHealthCheck({ |
| 18 | test: [ |
| 19 | "CMD-SHELL", |
| 20 | "MYSQL_PWD=\"$MYSQL_ROOT_PASSWORD\" mysqladmin ping --protocol TCP --host 127.0.0.1 --user root --silent" |
| 21 | ], |
| 22 | interval: 250, |
| 23 | timeout: 1000, |
| 24 | retries: 1000 |
| 25 | }) |
| 26 | |
| 27 | export default function setup(project: TestProject) { |
| 28 | return Promise.all([ |
| 29 | new PostgreSqlContainer("postgres:alpine").start(), |
| 30 | makeMysqlContainer().start() |
| 31 | ]).then(([pg, mysql]) => { |
| 32 | project.provide("clusterDatabases", { |
| 33 | mysql: mysql.getConnectionUri(), |
| 34 | pg: pg.getConnectionUri() |
| 35 | }) |
| 36 | return () => Promise.all([pg.stop(), mysql.stop()]).then(() => undefined) |
| 37 | }) |
| 38 | } |