Let a configured container be cloned and customised with functions

Open
#1,556 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
55/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
docker, node.js, typescript
Domain
testing, tooling

Research direction

Start with the dependency, issue #1520, since this issue says clone() depends on its builder state being made copyable. Then read packages/testcontainers/src/generic-container/generic-container.ts and packages/testcontainers/src/test-container.ts to see how builder state and subclass state are handled. Done means clone() returns an independent builder of the same class and with(fn) applies a customizer in the chain; run the relevant tests in the test-container file.

Written by the indexing model from the issue text.

Description

enhancement minor

What happens

A configured container cannot be used as a template. Builders are mutable and every with* method returns the same object:

const base = new GenericContainer(image).withEnvironment({ LOG_LEVEL: "debug" });
const a = base.withName("a");
const b = base.withName("b");
// a, b and base are one object, named "b"

Shared configuration therefore lives in factory functions, and a reusable piece of configuration has to be applied outside the chain:

const container = new PostgreSqlContainer(image).withDatabase("orders");
applyTracing(container);
await container.start();

Expected

  • clone() returns an independent copy of the builder, of the same class.
  • with(fn) applies a function inside the chain:
const withTracing = <T extends GenericContainer>(container: T) => container.withEnvironment({ OTEL_ENABLED: "true" });

await new PostgreSqlContainer(image).withDatabase("orders").with(withTracing).start();

Where

packages/testcontainers/src/generic-container/generic-container.ts and packages/testcontainers/src/test-container.ts.

Notes

  • Go models reusable configuration as ContainerCustomizer values passed to Run.
  • clone() needs the builder's state to be copyable in one place, including what subclasses add.

Dependencies

  • Blocked by #1520: clone() needs the builder state to be copyable, which is what that fix introduces.
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.