Nats Module
NATS is a simple, secure and high performance open source messaging system for cloud native applications, IoT messaging, and microservices architectures.
Install
npm install @testcontainers/nats --save-dev
Examples
it("should start, connect and close", async () => {
const container = await new NatsContainer().start();
// establish connection
const nc = await connect(container.getConnectionOptions());
// close the connection
await nc.close();
// check if the close was OK
const err = await nc.closed();
expect(err).toBe(undefined);
await container.stop();
});
it("should subscribe and receive one published message", async () => {
const SUBJECT = "HELLO";
const PAYLOAD = "WORLD";
const container = await new NatsContainer().start();
const nc = await connect(container.getConnectionOptions());
const sc = StringCodec();
//----------------
const sub = nc.subscribe(SUBJECT);
(async () => {
for await (const m of sub) {
const actual: string = sc.decode(m.data);
expect(actual).toEqual(PAYLOAD);
}
})().then();
//----------------
nc.publish(SUBJECT, sc.encode(PAYLOAD));
//----------------
await nc.drain();
await nc.close();
const err = await nc.closed();
expect(err).toBe(undefined);
await container.stop();
});
it("should start with alternative username and password ", async () => {
// set username and password like this
const container = await new NatsContainer().withPass("1234").withUsername("George").start();
const nc = await connect(container.getConnectionOptions());
// close the connection
await nc.close();
// check if the close was OK
const err = await nc.closed();
expect(err).toBe(undefined);
await container.stop();
});