1- import { describe , it } from 'node:test' ;
1+ import { afterEach , beforeEach , describe , it } from 'node:test' ;
22import { rejects , strictEqual } from 'node:assert' ;
3- import { MockAgent , getGlobalDispatcher , setGlobalDispatcher } from 'undici' ;
3+ import { MockAgent , getGlobalDispatcher , setGlobalDispatcher , type Dispatcher } from 'undici' ;
44
55import { KubeConfig } from './config.js' ;
66import { Health } from './health.js' ;
@@ -19,27 +19,28 @@ describe('Health', () => {
1919 } ,
2020 ] . forEach ( ( test ) => {
2121 describe ( test . path , ( ) => {
22- it ( 'should throw an error if no current active cluster' , async ( ) => {
23- const originalDispatcher = getGlobalDispatcher ( ) ;
24- const mockAgent = new MockAgent ( ) ;
22+ let mockAgent : MockAgent ;
23+ let originalDispatcher : Dispatcher ;
24+
25+ beforeEach ( ( ) => {
26+ originalDispatcher = getGlobalDispatcher ( ) ;
27+ mockAgent = new MockAgent ( ) ;
2528 setGlobalDispatcher ( mockAgent ) ;
2629 mockAgent . disableNetConnect ( ) ;
30+ } ) ;
31+
32+ afterEach ( async ( ) => {
33+ await mockAgent . close ( ) ;
34+ setGlobalDispatcher ( originalDispatcher ) ;
35+ } ) ;
36+
37+ it ( 'should throw an error if no current active cluster' , async ( ) => {
2738 const kc = new KubeConfig ( ) ;
2839 const health = new Health ( kc ) ;
29-
30- try {
31- await rejects ( test . method ( health , { } ) , { message : 'No currently active cluster' } ) ;
32- } finally {
33- await mockAgent . close ( ) ;
34- setGlobalDispatcher ( originalDispatcher ) ;
35- }
40+ await rejects ( test . method ( health , { } ) , { message : 'No currently active cluster' } ) ;
3641 } ) ;
3742
3843 it ( `should return true if ${ test . path } returns with status 200` , async ( ) => {
39- const originalDispatcher = getGlobalDispatcher ( ) ;
40- const mockAgent = new MockAgent ( ) ;
41- setGlobalDispatcher ( mockAgent ) ;
42- mockAgent . disableNetConnect ( ) ;
4344 const kc = new KubeConfig ( ) ;
4445 const cluster = {
4546 name : 'foo' ,
@@ -56,21 +57,12 @@ describe('Health', () => {
5657 pool . intercept ( { path : test . path , method : 'GET' } ) . reply ( 200 ) ;
5758 const health = new Health ( kc ) ;
5859
59- try {
60- const r = await test . method ( health , { } ) ;
61- strictEqual ( r , true ) ;
62- mockAgent . assertNoPendingInterceptors ( ) ;
63- } finally {
64- await mockAgent . close ( ) ;
65- setGlobalDispatcher ( originalDispatcher ) ;
66- }
60+ const r = await test . method ( health , { } ) ;
61+ strictEqual ( r , true ) ;
62+ mockAgent . assertNoPendingInterceptors ( ) ;
6763 } ) ;
6864
6965 it ( `should return false if ${ test . path } returns with status 500` , async ( ) => {
70- const originalDispatcher = getGlobalDispatcher ( ) ;
71- const mockAgent = new MockAgent ( ) ;
72- setGlobalDispatcher ( mockAgent ) ;
73- mockAgent . disableNetConnect ( ) ;
7466 const kc = new KubeConfig ( ) ;
7567 const cluster = {
7668 name : 'foo' ,
@@ -87,21 +79,12 @@ describe('Health', () => {
8779 pool . intercept ( { path : test . path , method : 'GET' } ) . reply ( 500 ) ;
8880 const health = new Health ( kc ) ;
8981
90- try {
91- const r = await test . method ( health , { } ) ;
92- strictEqual ( r , false ) ;
93- mockAgent . assertNoPendingInterceptors ( ) ;
94- } finally {
95- await mockAgent . close ( ) ;
96- setGlobalDispatcher ( originalDispatcher ) ;
97- }
82+ const r = await test . method ( health , { } ) ;
83+ strictEqual ( r , false ) ;
84+ mockAgent . assertNoPendingInterceptors ( ) ;
9885 } ) ;
9986
10087 it ( `should return true if ${ test . path } returns status 404 and /healthz returns status 200` , async ( ) => {
101- const originalDispatcher = getGlobalDispatcher ( ) ;
102- const mockAgent = new MockAgent ( ) ;
103- setGlobalDispatcher ( mockAgent ) ;
104- mockAgent . disableNetConnect ( ) ;
10588 const kc = new KubeConfig ( ) ;
10689 const cluster = {
10790 name : 'foo' ,
@@ -119,21 +102,12 @@ describe('Health', () => {
119102 pool . intercept ( { path : '/healthz' , method : 'GET' } ) . reply ( 200 ) ;
120103 const health = new Health ( kc ) ;
121104
122- try {
123- const r = await test . method ( health , { } ) ;
124- strictEqual ( r , true ) ;
125- mockAgent . assertNoPendingInterceptors ( ) ;
126- } finally {
127- await mockAgent . close ( ) ;
128- setGlobalDispatcher ( originalDispatcher ) ;
129- }
105+ const r = await test . method ( health , { } ) ;
106+ strictEqual ( r , true ) ;
107+ mockAgent . assertNoPendingInterceptors ( ) ;
130108 } ) ;
131109
132110 it ( `should return false if ${ test . path } returns status 404 and /healthz returns status 500` , async ( ) => {
133- const originalDispatcher = getGlobalDispatcher ( ) ;
134- const mockAgent = new MockAgent ( ) ;
135- setGlobalDispatcher ( mockAgent ) ;
136- mockAgent . disableNetConnect ( ) ;
137111 const kc = new KubeConfig ( ) ;
138112 const cluster = {
139113 name : 'foo' ,
@@ -151,21 +125,12 @@ describe('Health', () => {
151125 pool . intercept ( { path : '/healthz' , method : 'GET' } ) . reply ( 500 ) ;
152126 const health = new Health ( kc ) ;
153127
154- try {
155- const r = await test . method ( health , { } ) ;
156- strictEqual ( r , false ) ;
157- mockAgent . assertNoPendingInterceptors ( ) ;
158- } finally {
159- await mockAgent . close ( ) ;
160- setGlobalDispatcher ( originalDispatcher ) ;
161- }
128+ const r = await test . method ( health , { } ) ;
129+ strictEqual ( r , false ) ;
130+ mockAgent . assertNoPendingInterceptors ( ) ;
162131 } ) ;
163132
164133 it ( `should return true if both ${ test . path } and /healthz return status 404` , async ( ) => {
165- const originalDispatcher = getGlobalDispatcher ( ) ;
166- const mockAgent = new MockAgent ( ) ;
167- setGlobalDispatcher ( mockAgent ) ;
168- mockAgent . disableNetConnect ( ) ;
169134 const kc = new KubeConfig ( ) ;
170135 const cluster = {
171136 name : 'foo' ,
@@ -183,21 +148,12 @@ describe('Health', () => {
183148 pool . intercept ( { path : '/healthz' , method : 'GET' } ) . reply ( 404 ) ;
184149 const health = new Health ( kc ) ;
185150
186- try {
187- const r = await test . method ( health , { } ) ;
188- strictEqual ( r , true ) ;
189- mockAgent . assertNoPendingInterceptors ( ) ;
190- } finally {
191- await mockAgent . close ( ) ;
192- setGlobalDispatcher ( originalDispatcher ) ;
193- }
151+ const r = await test . method ( health , { } ) ;
152+ strictEqual ( r , true ) ;
153+ mockAgent . assertNoPendingInterceptors ( ) ;
194154 } ) ;
195155
196156 it ( 'should throw an error when fetch throws an error' , async ( ) => {
197- const originalDispatcher = getGlobalDispatcher ( ) ;
198- const mockAgent = new MockAgent ( ) ;
199- setGlobalDispatcher ( mockAgent ) ;
200- mockAgent . disableNetConnect ( ) ;
201157 const kc = new KubeConfig ( ) ;
202158 const cluster = {
203159 name : 'foo' ,
@@ -214,13 +170,8 @@ describe('Health', () => {
214170 pool . intercept ( { path : test . path , method : 'GET' } ) . replyWithError ( new Error ( 'an error' ) ) ;
215171 const health = new Health ( kc ) ;
216172
217- try {
218- await rejects ( test . method ( health , { } ) , { message : 'Error occurred in health request' } ) ;
219- mockAgent . assertNoPendingInterceptors ( ) ;
220- } finally {
221- await mockAgent . close ( ) ;
222- setGlobalDispatcher ( originalDispatcher ) ;
223- }
173+ await rejects ( test . method ( health , { } ) , { message : 'Error occurred in health request' } ) ;
174+ mockAgent . assertNoPendingInterceptors ( ) ;
224175 } ) ;
225176 } ) ;
226177 } ) ;
0 commit comments