Skip to content

Commit 1a6102d

Browse files
authored
Allow overriding default compose wait strategy (#990)
1 parent e4fa915 commit 1a6102d

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

docs/features/compose.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ const environment = await new DockerComposeEnvironment(composeFilePath, composeF
4141
.up();
4242
```
4343

44+
### With a default wait strategy
45+
46+
By default Testcontainers uses the "listening ports" wait strategy for all containers. If you'd like to override
47+
the default wait strategy for all services, you can do so:
48+
49+
```javascript
50+
const environment = await new DockerComposeEnvironment(composeFilePath, composeFile)
51+
.withDefaultWaitStrategy(Wait.forHealthCheck())
52+
.up();
53+
```
54+
4455
### With a pull policy
4556

4657
Testcontainers will automatically pull an image if it doesn't exist. This is configurable:

packages/testcontainers/src/docker-compose-environment/docker-compose-environment.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
9898
await startedEnvironment.down();
9999
});
100100

101+
it("should support default wait strategy", async () => {
102+
const startedEnvironment = await new DockerComposeEnvironment(fixtures, "docker-compose-with-healthcheck.yml")
103+
.withDefaultWaitStrategy(Wait.forHealthCheck())
104+
.up();
105+
106+
await checkEnvironmentContainerIsHealthy(startedEnvironment, await composeContainerName("container"));
107+
108+
await startedEnvironment.down();
109+
});
110+
101111
it("should support log message wait strategy", async () => {
102112
const startedEnvironment = await new DockerComposeEnvironment(fixtures, "docker-compose.yml")
103113
.withWaitStrategy(await composeContainerName("container"), Wait.forLogMessage("Listening on port 8080"))

packages/testcontainers/src/docker-compose-environment/docker-compose-environment.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class DockerComposeEnvironment {
2323
private profiles: string[] = [];
2424
private environment: Environment = {};
2525
private pullPolicy: ImagePullPolicy = PullPolicy.defaultPolicy();
26+
private defaultWaitStrategy: WaitStrategy = Wait.forListeningPorts();
2627
private waitStrategy: { [containerName: string]: WaitStrategy } = {};
2728
private startupTimeout?: number;
2829

@@ -63,6 +64,11 @@ export class DockerComposeEnvironment {
6364
return this;
6465
}
6566

67+
public withDefaultWaitStrategy(waitStrategy: WaitStrategy): this {
68+
this.defaultWaitStrategy = waitStrategy;
69+
return this;
70+
}
71+
6672
public withWaitStrategy(containerName: string, waitStrategy: WaitStrategy): this {
6773
this.waitStrategy[containerName] = waitStrategy;
6874
return this;
@@ -140,7 +146,7 @@ export class DockerComposeEnvironment {
140146
const boundPorts = BoundPorts.fromInspectResult(client.info.containerRuntime.hostIps, mappedInspectResult);
141147
const waitStrategy = this.waitStrategy[containerName]
142148
? this.waitStrategy[containerName]
143-
: Wait.forListeningPorts();
149+
: this.defaultWaitStrategy;
144150
if (this.startupTimeout !== undefined) {
145151
waitStrategy.withStartupTimeout(this.startupTimeout);
146152
}

0 commit comments

Comments
 (0)