Skip to content

Commit 155f962

Browse files
authored
Fix abstract started container async dispose (#1203)
Signed-off-by: Joe Bowbeer <[email protected]>
1 parent 252239f commit 155f962

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Mock } from "vitest";
2+
import { AbstractStartedContainer, GenericContainer } from "../index";
3+
4+
describe.sequential("AbstractStartedContainer", { timeout: 60_000 }, () => {
5+
let containerStopping: Mock;
6+
let containerStopped: Mock;
7+
8+
beforeEach(() => {
9+
containerStopping = vi.fn();
10+
containerStopped = vi.fn();
11+
});
12+
13+
it("should call overridden lifecycle methods when disposing asynchronously", async () => {
14+
{
15+
await using _container = await new CustomContainerWithCustomStartedContainer(
16+
"cristianrgreco/testcontainer:1.1.14"
17+
)
18+
.withExposedPorts(8080)
19+
.start();
20+
}
21+
22+
expect(containerStopping).toHaveBeenCalled();
23+
expect(containerStopped).toHaveBeenCalled();
24+
});
25+
26+
class CustomContainerWithCustomStartedContainer extends GenericContainer {
27+
public override async start(): Promise<CustomStartedContainer> {
28+
return new CustomStartedContainer(await super.start());
29+
}
30+
}
31+
32+
class CustomStartedContainer extends AbstractStartedContainer {
33+
protected override async containerStopping(): Promise<void> {
34+
containerStopping();
35+
}
36+
37+
protected override async containerStopped(): Promise<void> {
38+
containerStopped();
39+
}
40+
}
41+
});

packages/testcontainers/src/generic-container/abstract-started-container.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class AbstractStartedContainer implements StartedTestContainer {
1212
await this.containerStopping();
1313
}
1414

15-
const stoppedContainer = this.startedTestContainer.stop(options);
15+
const stoppedContainer = await this.startedTestContainer.stop(options);
1616

1717
if (this.containerStopped) {
1818
await this.containerStopped();
@@ -105,6 +105,6 @@ export class AbstractStartedContainer implements StartedTestContainer {
105105
}
106106

107107
async [Symbol.asyncDispose]() {
108-
await this.startedTestContainer[Symbol.asyncDispose]();
108+
await this.stop();
109109
}
110110
}

0 commit comments

Comments
 (0)