From e003c62e96617d679d33bf66b4e56e59e057db08 Mon Sep 17 00:00:00 2001 From: Joe Bowbeer Date: Sat, 20 Dec 2025 00:32:47 +0000 Subject: [PATCH] fix: abstract started container async dispose Signed-off-by: Joe Bowbeer --- .../abstract-started-container.test.ts | 41 +++++++++++++++++++ .../abstract-started-container.ts | 4 +- 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 packages/testcontainers/src/generic-container/abstract-started-container.test.ts diff --git a/packages/testcontainers/src/generic-container/abstract-started-container.test.ts b/packages/testcontainers/src/generic-container/abstract-started-container.test.ts new file mode 100644 index 000000000..714f05d38 --- /dev/null +++ b/packages/testcontainers/src/generic-container/abstract-started-container.test.ts @@ -0,0 +1,41 @@ +import { Mock } from "vitest"; +import { AbstractStartedContainer, GenericContainer } from "../index"; + +describe.sequential("AbstractStartedContainer", { timeout: 60_000 }, () => { + let containerStopping: Mock; + let containerStopped: Mock; + + beforeEach(() => { + containerStopping = vi.fn(); + containerStopped = vi.fn(); + }); + + it("should call overridden lifecycle methods when disposing asynchronously", async () => { + { + await using _container = await new CustomContainerWithCustomStartedContainer( + "cristianrgreco/testcontainer:1.1.14" + ) + .withExposedPorts(8080) + .start(); + } + + expect(containerStopping).toHaveBeenCalled(); + expect(containerStopped).toHaveBeenCalled(); + }); + + class CustomContainerWithCustomStartedContainer extends GenericContainer { + public override async start(): Promise { + return new CustomStartedContainer(await super.start()); + } + } + + class CustomStartedContainer extends AbstractStartedContainer { + protected override async containerStopping(): Promise { + containerStopping(); + } + + protected override async containerStopped(): Promise { + containerStopped(); + } + } +}); diff --git a/packages/testcontainers/src/generic-container/abstract-started-container.ts b/packages/testcontainers/src/generic-container/abstract-started-container.ts index 4ba5ccce5..caa067419 100644 --- a/packages/testcontainers/src/generic-container/abstract-started-container.ts +++ b/packages/testcontainers/src/generic-container/abstract-started-container.ts @@ -12,7 +12,7 @@ export class AbstractStartedContainer implements StartedTestContainer { await this.containerStopping(); } - const stoppedContainer = this.startedTestContainer.stop(options); + const stoppedContainer = await this.startedTestContainer.stop(options); if (this.containerStopped) { await this.containerStopped(); @@ -105,6 +105,6 @@ export class AbstractStartedContainer implements StartedTestContainer { } async [Symbol.asyncDispose]() { - await this.startedTestContainer[Symbol.asyncDispose](); + await this.stop(); } }