@@ -4,6 +4,10 @@ import { checkContainerIsHealthy } from "../utils/test-helper";
44describe ( "GenericContainer reuse" , ( ) => {
55 jest . setTimeout ( 180_000 ) ;
66
7+ afterEach ( ( ) => {
8+ process . env . TESTCONTAINERS_REUSE_ENABLE = undefined ;
9+ } ) ;
10+
711 it ( "should not reuse the container by default" , async ( ) => {
812 const container = await new GenericContainer ( "cristianrgreco/testcontainer:1.1.14" )
913 . withName ( "there_can_only_be_one" )
@@ -63,26 +67,54 @@ describe("GenericContainer reuse", () => {
6367 }
6468 } ) ;
6569
66- it ( "should reuse the container" , async ( ) => {
67- const container1 = await new GenericContainer ( "cristianrgreco/testcontainer:1.1.14" )
68- . withName ( "there_can_only_be_one" )
69- . withExposedPorts ( 8080 )
70- . withReuse ( )
71- . start ( ) ;
72- await checkContainerIsHealthy ( container1 ) ;
70+ it ( "should not reuse the container when TESTCONTAINERS_REUSE_ENABLE is set to false" , async ( ) => {
71+ process . env . TESTCONTAINERS_REUSE_ENABLE = "false" ;
7372
74- const container2 = await new GenericContainer ( "cristianrgreco/testcontainer:1.1.14" )
73+ const container = await new GenericContainer ( "cristianrgreco/testcontainer:1.1.14" )
7574 . withName ( "there_can_only_be_one" )
7675 . withExposedPorts ( 8080 )
7776 . withReuse ( )
7877 . start ( ) ;
79- await checkContainerIsHealthy ( container2 ) ;
80-
81- expect ( container1 . getId ( ) ) . toBe ( container2 . getId ( ) ) ;
78+ await checkContainerIsHealthy ( container ) ;
8279
83- await container1 . stop ( ) ;
80+ try {
81+ await expect ( ( ) =>
82+ new GenericContainer ( "cristianrgreco/testcontainer:1.1.14" )
83+ . withName ( "there_can_only_be_one" )
84+ . withExposedPorts ( 8080 )
85+ . withReuse ( )
86+ . start ( )
87+ ) . rejects . toThrowError ( ) ;
88+ } finally {
89+ await container . stop ( ) ;
90+ }
8491 } ) ;
8592
93+ it . each ( [ "true" , undefined ] ) (
94+ "should reuse the container when TESTCONTAINERS_REUSE_ENABLE is set to %s" ,
95+ async ( reuseEnable : string | undefined ) => {
96+ process . env . TESTCONTAINERS_REUSE_ENABLE = reuseEnable ;
97+
98+ const container1 = await new GenericContainer ( "cristianrgreco/testcontainer:1.1.14" )
99+ . withName ( "there_can_only_be_one" )
100+ . withExposedPorts ( 8080 )
101+ . withReuse ( )
102+ . start ( ) ;
103+ await checkContainerIsHealthy ( container1 ) ;
104+
105+ const container2 = await new GenericContainer ( "cristianrgreco/testcontainer:1.1.14" )
106+ . withName ( "there_can_only_be_one" )
107+ . withExposedPorts ( 8080 )
108+ . withReuse ( )
109+ . start ( ) ;
110+ await checkContainerIsHealthy ( container2 ) ;
111+
112+ expect ( container1 . getId ( ) ) . toBe ( container2 . getId ( ) ) ;
113+
114+ await container1 . stop ( ) ;
115+ }
116+ ) ;
117+
86118 it ( "should create a new container when an existing reusable container has stopped and is removed" , async ( ) => {
87119 const container1 = await new GenericContainer ( "cristianrgreco/testcontainer:1.1.14" )
88120 . withName ( "there_can_only_be_one" )
0 commit comments