Skip to content
Closed
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
4 changes: 4 additions & 0 deletions docs/modules/nats.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ npm install @testcontainers/nats --save-dev
[Set credentials:](../../packages/modules/nats/src/nats-container.test.ts) inside_block:credentials
<!--/codeinclude-->

<!--codeinclude-->
[No credentials:](../../packages/modules/nats/src/nats-container.test.ts) inside_block:noCredentials
<!--/codeinclude-->

<!--codeinclude-->
[Enable JetStream:](../../packages/modules/nats/src/nats-container.test.ts) inside_block:jetstream
<!--/codeinclude-->
16 changes: 16 additions & 0 deletions packages/modules/nats/src/nats-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,23 @@ describe("NatsContainer", { timeout: 180_000 }, () => {
// connect {
it("should start, connect and close", async () => {
const container = await new NatsContainer(IMAGE).start();
expect(container.getConnectionOptions().pass).toEqual("test");
expect(container.getConnectionOptions().user).toEqual("test");
// establish connection
const nc = await connect(container.getConnectionOptions());

await nc.close();
// check if the close was OK
const err = await nc.closed();
expect(err).toBe(undefined);

await container.stop();
});
// noCredentials {
it("should start, connect and close when noCredentials is true", async () => {
const container = await new NatsContainer(IMAGE).withCredentials(false).start();
expect(container.getConnectionOptions().user).toBeUndefined();
expect(container.getConnectionOptions().pass).toBeUndefined();
// establish connection
const nc = await connect(container.getConnectionOptions());
// close the connection
Expand Down
19 changes: 18 additions & 1 deletion packages/modules/nats/src/nats-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const PASS_ARGUMENT_KEY = "--pass";
export class NatsContainer extends GenericContainer {
private args = new Set<string>();
private values = new Map<string, string | undefined>();
private useCredentials = true;

constructor(image: string) {
super(image);
Expand Down Expand Up @@ -42,6 +43,18 @@ export class NatsContainer extends GenericContainer {
return this;
}

public withCredentials(useCredentials: boolean): this {
Copy link
Collaborator

@cristianrgreco cristianrgreco Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@george-of-croton Need to handle this scenario:

const container = await
  .withUsername("x")
  .withUsername("y")
  .withCredentialsEnabled(true)
  .start()

container.getUser(); // test
container.getPass(); // test

this.useCredentials = useCredentials;
if (!useCredentials) {
this.args.delete(USER_ARGUMENT_KEY);
this.args.delete(PASS_ARGUMENT_KEY);
} else {
this.withUsername("test");
this.withPass("test");
}
return this;
}

public withArg(name: string, value: string): this;
public withArg(name: string): this;
public withArg(...args: [string, string] | [string]): this {
Expand Down Expand Up @@ -69,7 +82,11 @@ export class NatsContainer extends GenericContainer {

public override async start(): Promise<StartedNatsContainer> {
this.withCommand(this.getNormalizedCommand());
return new StartedNatsContainer(await super.start(), this.getUser(), this.getPass());
return new StartedNatsContainer(
await super.start(),
(this.useCredentials && this.getUser()) || undefined,
(this.useCredentials && this.getPass()) || undefined
Comment on lines +87 to +88
Copy link

Copilot AI Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The conditional logic could be simplified by using a ternary operator for better readability: this.useCredentials ? this.getPass() : undefined

Suggested change
(this.useCredentials && this.getUser()) || undefined,
(this.useCredentials && this.getPass()) || undefined
this.useCredentials ? this.getUser() : undefined,
this.useCredentials ? this.getPass() : undefined

Copilot uses AI. Check for mistakes.
);
}

private getUser(): string | undefined {
Expand Down