Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/features/containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ const container = await new GenericContainer("alpine")
.start();
```

Calling `withEnvironment` more than once adds to the variables set before. Setting a key again replaces its value.

### With a platform

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,25 @@ describe("GenericContainer", { timeout: 180_000 }, () => {
expect(responseBody.customKey).toBe("customValue");
});

it("should replace an environment variable that is set twice", async () => {
await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withEnvironment({ customKey: "first", otherKey: "otherValue" })
.withEnvironment({ customKey: "second" })
.withExposedPorts(8080)
.start();

const client = await getContainerRuntimeClient();
const dockerContainer = client.container.getById(container.getId());
const containerInfo = await dockerContainer.inspect();
expect(containerInfo.Config.Env.filter((env) => env.startsWith("customKey="))).toEqual(["customKey=second"]);
expect(containerInfo.Config.Env).toContain("otherKey=otherValue");

const url = `http://${container.getHost()}:${container.getMappedPort(8080)}`;
const response = await fetch(`${url}/env`);
const responseBody = (await response.json()) as { [key: string]: string };
expect(responseBody.customKey).toBe("second");
});

it("should set command", async () => {
await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withCommand(["node", "index.js", "one", "two", "three"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,7 @@ export class GenericContainer implements TestContainer {

public withEnvironment(environment: Environment): this {
this.environment = { ...this.environment, ...environment };
this.createOpts.Env = [
...(this.createOpts.Env ?? []),
...Object.entries(environment).map(([key, value]) => `${key}=${value}`),
];
this.createOpts.Env = Object.entries(this.environment).map(([key, value]) => `${key}=${value}`);
return this;
}

Expand Down
Loading