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(); } }