Add lifecycle hooks that do not need a subclass

Open
#1,554 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 4 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
docker, node.js, typescript
Domain
backend, testing

Research direction

Start with the three files named under “Where,” then read #1520, #1521, and #1582 to understand the dependencies on builder state, reuse behavior, and startup time. Implement registered lifecycle hooks and the startup command shorthand while retaining the protected methods. Done means hooks receive the described context and can be combined; verify behavior for each lifecycle stage and reused containers.

Written by the indexing model from the issue text.

Description

enhancement minor

What happens

The lifecycle hooks are protected methods: beforeContainerCreated, containerCreated, containerStarting and containerStarted on GenericContainer, and containerStopping and containerStopped on AbstractStartedContainer. Using one means writing a subclass.

  • A hook cannot be added to a module container without subclassing the module.
  • Two independent behaviours cannot be combined, because each wants to be the subclass.
  • A one-off "run this command once the container is ready" needs a class.

Expected

Hooks can be registered on the builder, and several can be registered:

new PostgreSqlContainer(image).withLifecycleHooks({
  started: async (container) => {
    await container.exec(["psql", "-f", "/seed.sql"]);
  },
});

plus a shorthand for the commonest case, withStartupCommands([...]). The protected methods keep working.

Where

  • packages/testcontainers/src/generic-container/generic-container.ts
  • packages/testcontainers/src/generic-container/started-generic-container.ts
  • packages/testcontainers/src/test-container.ts

Notes

  • Go has WithLifecycleHooks and WithAdditionalLifecycleHooks, with hooks for each of pre/post create, start, ready, stop and terminate, and the shorthands WithStartupCommand and WithAfterReadyCommand.
  • Each hook should be told whether the container was reused.
  • Pass hooks one context object instead of more positional arguments. It should carry reused, the remaining startup time and, later, an abort signal.

Dependencies

  • Stack on #1520: registered hooks are builder state, and they run inside the reworked start().
  • Coordinate with #1521: that issue decides when hooks run for a reused container.
  • Coordinate with #1582: the hook context carries the remaining startup time.
Dominant language
TypeScript
Stars
2.7k
Forks
290
Avg merge
4d 1h
Merged PRs (30d)
22

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from testcontainers/testcontainers-node

All issues in testcontainers/testcontainers-node

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.