1
- import { fail } from 'node:assert' ;
2
1
import { expect } from 'chai' ;
3
2
import nock from 'nock' ;
4
3
import { KubeConfig } from './config' ;
@@ -147,32 +146,21 @@ describe('Metrics', () => {
147
146
currentContext : 'currentContext' ,
148
147
} ) ;
149
148
const metricsClient = new Metrics ( kc ) ;
150
- try {
151
- await metricsClient . getPodMetrics ( ) ;
152
- fail ( 'expected thrown error' ) ;
153
- } catch ( e ) {
154
- expect ( e instanceof ApiException ) ;
155
- if ( e instanceof ApiException ) {
156
- expect ( e . message ) . to . include ( 'connect ECONNREFUSED 127.0.0.1:51011' ) ;
157
- }
158
- }
149
+ await expect ( metricsClient . getPodMetrics ( ) ) . to . be . rejectedWith (
150
+ ApiException ,
151
+ 'connect ECONNREFUSED 127.0.0.1:51011' ,
152
+ ) ;
159
153
} ) ;
160
154
it ( 'should throw when no current cluster' , async ( ) => {
161
155
const [ metricsClient , scope ] = systemUnderTest ( {
162
156
clusters : [ { name : 'cluster' , server : 'https://127.0.0.1:51010' } ] ,
163
157
users : [ { name : 'user' , password : 'password' } ] ,
164
158
contexts : [ { name : 'currentContext' , cluster : 'cluster' , user : 'user' } ] ,
165
159
} ) ;
166
-
167
- try {
168
- await metricsClient . getPodMetrics ( ) ;
169
- fail ( 'expected thrown error' ) ;
170
- } catch ( e ) {
171
- expect ( e instanceof ApiException ) ;
172
- if ( e instanceof ApiException ) {
173
- expect ( e . message ) . to . equal ( 'No currently active cluster' ) ;
174
- }
175
- }
160
+ await expect ( metricsClient . getPodMetrics ( ) ) . to . be . rejectedWith (
161
+ Error ,
162
+ 'No currently active cluster' ,
163
+ ) ;
176
164
scope . done ( ) ;
177
165
} ) ;
178
166
it ( 'should resolve to error when 500 - V1 Status' , async ( ) => {
@@ -183,33 +171,23 @@ describe('Metrics', () => {
183
171
const [ metricsClient , scope ] = systemUnderTest ( ) ;
184
172
const s = scope . get ( '/apis/metrics.k8s.io/v1beta1/pods' ) . reply ( 500 , response ) ;
185
173
186
- try {
187
- await metricsClient . getPodMetrics ( ) ;
188
- fail ( 'expected thrown error' ) ;
189
- } catch ( e ) {
190
- if ( ! ( e instanceof ApiException ) ) {
191
- fail ( 'expected ApiException error' ) ;
192
- }
174
+ await expect ( metricsClient . getPodMetrics ( ) ) . to . be . rejected . then ( ( e ) => {
175
+ expect ( e ) . to . be . an . instanceOf ( ApiException ) ;
193
176
expect ( e . code ) . to . equal ( response . code ) ;
194
177
expect ( e . body . message ) . to . equal ( response . message ) ;
195
- }
178
+ } ) ;
196
179
s . done ( ) ;
197
180
} ) ;
198
181
it ( 'should resolve to error when 500 - non-V1Status' , async ( ) => {
199
182
const response = 'some other response' ;
200
183
const [ metricsClient , scope ] = systemUnderTest ( ) ;
201
184
const s = scope . get ( '/apis/metrics.k8s.io/v1beta1/pods' ) . reply ( 500 , response ) ;
202
185
203
- try {
204
- await metricsClient . getPodMetrics ( ) ;
205
- fail ( 'expected thrown error' ) ;
206
- } catch ( e ) {
207
- if ( ! ( e instanceof ApiException ) ) {
208
- fail ( 'expected ApiException error' ) ;
209
- }
186
+ await expect ( metricsClient . getPodMetrics ( ) ) . to . be . rejected . then ( ( e ) => {
187
+ expect ( e ) . to . be . an . instanceOf ( ApiException ) ;
210
188
expect ( e . code ) . to . equal ( 500 ) ;
211
189
expect ( e . message ) . to . include ( 'Error occurred in metrics request' ) ;
212
- }
190
+ } ) ;
213
191
s . done ( ) ;
214
192
} ) ;
215
193
} ) ;
@@ -239,16 +217,11 @@ describe('Metrics', () => {
239
217
const [ metricsClient , scope ] = systemUnderTest ( ) ;
240
218
const s = scope . get ( '/apis/metrics.k8s.io/v1beta1/nodes' ) . reply ( 500 , response ) ;
241
219
242
- try {
243
- await metricsClient . getNodeMetrics ( ) ;
244
- fail ( 'expected thrown error' ) ;
245
- } catch ( e ) {
246
- if ( ! ( e instanceof ApiException ) ) {
247
- fail ( 'expected ApiException error' ) ;
248
- }
220
+ await expect ( metricsClient . getNodeMetrics ( ) ) . to . be . rejected . then ( ( e ) => {
221
+ expect ( e ) . to . be . an . instanceOf ( ApiException ) ;
249
222
expect ( e . code ) . to . equal ( response . code ) ;
250
223
expect ( e . body . message ) . to . equal ( response . message ) ;
251
- }
224
+ } ) ;
252
225
s . done ( ) ;
253
226
} ) ;
254
227
} ) ;
0 commit comments