1- import { connect , StringCodec } from "nats" ;
1+ import { jetstreamManager } from "@nats-io/jetstream" ;
2+ import { connect } from "@nats-io/transport-node" ;
23import { NatsContainer } from "./nats-container" ;
34
45const IMAGE = "nats:2.8.4-alpine" ;
@@ -20,26 +21,41 @@ describe("NatsContainer", { timeout: 180_000 }, () => {
2021 } ) ;
2122 // }
2223
24+ it ( "should start, connect and close using scratch image" , async ( ) => {
25+ const container = await new NatsContainer ( "nats:2.11" ) . start ( ) ;
26+
27+ // establish connection
28+ const nc = await connect ( container . getConnectionOptions ( ) ) ;
29+ // close the connection
30+ await nc . close ( ) ;
31+ // check if the close was OK
32+ const err = await nc . closed ( ) ;
33+ expect ( err ) . toBe ( undefined ) ;
34+
35+ await container . stop ( ) ;
36+ } ) ;
37+
2338 // pubsub {
2439 it ( "should subscribe and receive one published message" , async ( ) => {
2540 const SUBJECT = "HELLO" ;
2641 const PAYLOAD = "WORLD" ;
2742
2843 const container = await new NatsContainer ( IMAGE ) . start ( ) ;
2944 const nc = await connect ( container . getConnectionOptions ( ) ) ;
30- const sc = StringCodec ( ) ;
45+ const TE = new TextEncoder ( ) ;
46+ const TD = new TextDecoder ( ) ;
3147
3248 //----------------
3349 const sub = nc . subscribe ( SUBJECT ) ;
3450 ( async ( ) => {
3551 for await ( const m of sub ) {
36- const actual : string = sc . decode ( m . data ) ;
52+ const actual = TD . decode ( m . data ) ;
3753 expect ( actual ) . toEqual ( PAYLOAD ) ;
3854 }
3955 } ) ( ) . then ( ) ;
4056
4157 //----------------
42- nc . publish ( SUBJECT , sc . encode ( PAYLOAD ) ) ;
58+ nc . publish ( SUBJECT , TE . encode ( PAYLOAD ) ) ;
4359
4460 //----------------
4561 await nc . drain ( ) ;
@@ -75,7 +91,7 @@ describe("NatsContainer", { timeout: 180_000 }, () => {
7591 const nc = await connect ( container . getConnectionOptions ( ) ) ;
7692
7793 // ensure JetStream is enabled, otherwise this will throw an error
78- await nc . jetstream ( ) . jetstreamManager ( ) ;
94+ await jetstreamManager ( nc ) ;
7995
8096 // close the connection
8197 await nc . close ( ) ;
@@ -92,7 +108,7 @@ describe("NatsContainer", { timeout: 180_000 }, () => {
92108 const nc = await connect ( container . getConnectionOptions ( ) ) ;
93109
94110 // ensure JetStream is not enabled, as this will throw an error
95- await expect ( nc . jetstream ( ) . jetstreamManager ( ) ) . rejects . toThrow ( "503 " ) ;
111+ await expect ( jetstreamManager ( nc ) ) . rejects . toThrow ( "jetstream is not enabled " ) ;
96112
97113 // close the connection
98114 await nc . close ( ) ;
0 commit comments