diff --git a/docs/features/compose.md b/docs/features/compose.md index 9fbc066fe..9ffa9860a 100644 --- a/docs/features/compose.md +++ b/docs/features/compose.md @@ -41,6 +41,17 @@ const environment = await new DockerComposeEnvironment(composeFilePath, composeF .up(); ``` +### With a default wait strategy + +By default Testcontainers uses the "listening ports" wait strategy for all containers. If you'd like to override +the default wait strategy for all services, you can do so: + +```javascript +const environment = await new DockerComposeEnvironment(composeFilePath, composeFile) + .withDefaultWaitStrategy(Wait.forHealthCheck()) + .up(); +``` + ### With a pull policy Testcontainers will automatically pull an image if it doesn't exist. This is configurable: diff --git a/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.test.ts b/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.test.ts index e59c8dfd3..d97c604db 100644 --- a/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.test.ts +++ b/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.test.ts @@ -98,6 +98,16 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => { await startedEnvironment.down(); }); + it("should support default wait strategy", async () => { + const startedEnvironment = await new DockerComposeEnvironment(fixtures, "docker-compose-with-healthcheck.yml") + .withDefaultWaitStrategy(Wait.forHealthCheck()) + .up(); + + await checkEnvironmentContainerIsHealthy(startedEnvironment, await composeContainerName("container")); + + await startedEnvironment.down(); + }); + it("should support log message wait strategy", async () => { const startedEnvironment = await new DockerComposeEnvironment(fixtures, "docker-compose.yml") .withWaitStrategy(await composeContainerName("container"), Wait.forLogMessage("Listening on port 8080")) diff --git a/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.ts b/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.ts index 83dd0a2cb..f63f32640 100644 --- a/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.ts +++ b/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.ts @@ -23,6 +23,7 @@ export class DockerComposeEnvironment { private profiles: string[] = []; private environment: Environment = {}; private pullPolicy: ImagePullPolicy = PullPolicy.defaultPolicy(); + private defaultWaitStrategy: WaitStrategy = Wait.forListeningPorts(); private waitStrategy: { [containerName: string]: WaitStrategy } = {}; private startupTimeout?: number; @@ -63,6 +64,11 @@ export class DockerComposeEnvironment { return this; } + public withDefaultWaitStrategy(waitStrategy: WaitStrategy): this { + this.defaultWaitStrategy = waitStrategy; + return this; + } + public withWaitStrategy(containerName: string, waitStrategy: WaitStrategy): this { this.waitStrategy[containerName] = waitStrategy; return this; @@ -140,7 +146,7 @@ export class DockerComposeEnvironment { const boundPorts = BoundPorts.fromInspectResult(client.info.containerRuntime.hostIps, mappedInspectResult); const waitStrategy = this.waitStrategy[containerName] ? this.waitStrategy[containerName] - : Wait.forListeningPorts(); + : this.defaultWaitStrategy; if (this.startupTimeout !== undefined) { waitStrategy.withStartupTimeout(this.startupTimeout); }