Skip to content

Commit b83b8d3

Browse files
committed
test: replace chai with node:assert
This commit replaces the uses of chai and chai-as-promised throughout the codebase.
1 parent 95fe749 commit b83b8d3

25 files changed

+821
-950
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ Run `npm run lint` or install an editor plugin.
177177

178178
# Testing
179179

180-
Tests are written using the [Chai](http://chaijs.com/) library. See
180+
Tests are written using the [Mocha](https://mochajs.org/) test runner and
181+
[`node:assert`](https://nodejs.org/api/assert.html) assertion library. See
181182
[`config_test.ts`](./src/config_test.ts) for an example.
182183

183184
To run tests, execute the following:

eslint.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export default tseslint.config(
2121
'@typescript-eslint/no-empty-object-type': 'off',
2222
'@typescript-eslint/no-explicit-any': 'off',
2323
'@typescript-eslint/no-non-null-assertion': 'off',
24-
'@typescript-eslint/no-unused-expressions': 'off',
2524
'@typescript-eslint/no-unused-vars': ['error', { args: 'none' }],
2625
},
2726
},

src/attach_test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect } from 'chai';
1+
import { strictEqual } from 'node:assert';
22
import WebSocket from 'isomorphic-ws';
33
import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers';
44
import { anyFunction, anything, capture, instance, mock, verify, when } from 'ts-mockito';
@@ -73,7 +73,7 @@ describe('Attach', () => {
7373
await attach.attach(namespace, pod, container, osStream, errStream, isStream, false);
7474
const [, , outputFn] = capture(fakeWebSocketInterface.connect).last();
7575

76-
expect(outputFn).to.not.be.null;
76+
strictEqual(typeof outputFn, 'function');
7777

7878
// this is redundant but needed for the compiler, sigh...
7979
if (!outputFn) {
@@ -83,18 +83,18 @@ describe('Attach', () => {
8383
let buffer = Buffer.alloc(1024, 10);
8484

8585
outputFn(WebSocketHandler.StdoutStream, buffer);
86-
expect(osStream.size()).to.equal(1024);
86+
strictEqual(osStream.size(), 1024);
8787
let buff = osStream.getContents() as Buffer;
8888
for (let i = 0; i < 1024; i++) {
89-
expect(buff[i]).to.equal(10);
89+
strictEqual(buff[i], 10);
9090
}
9191

9292
buffer = Buffer.alloc(1024, 20);
9393
outputFn(WebSocketHandler.StderrStream, buffer);
94-
expect(errStream.size()).to.equal(1024);
94+
strictEqual(errStream.size(), 1024);
9595
buff = errStream.getContents() as Buffer;
9696
for (let i = 0; i < 1024; i++) {
97-
expect(buff[i]).to.equal(20);
97+
strictEqual(buff[i], 20);
9898
}
9999

100100
const initialTerminalSize: TerminalSize = { height: 0, width: 0 };

src/azure_auth_test.ts

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { use, expect } from 'chai';
2-
import chaiAsPromised from 'chai-as-promised';
1+
import { rejects, strictEqual } from 'node:assert';
32
import { dirname, join } from 'node:path';
43
import { fileURLToPath } from 'node:url';
54

@@ -10,8 +9,6 @@ import { HttpMethod, RequestContext } from './index.js';
109

1110
const __dirname = dirname(fileURLToPath(import.meta.url));
1211

13-
use(chaiAsPromised);
14-
1512
describe('AzureAuth', () => {
1613
const testUrl1 = 'https://test1.com';
1714
let auth: AzureAuth;
@@ -26,7 +23,7 @@ describe('AzureAuth', () => {
2623
},
2724
} as User;
2825

29-
expect(auth.isAuthProvider(user)).to.equal(true);
26+
strictEqual(auth.isAuthProvider(user), true);
3027
});
3128

3229
it('should be false for other user', () => {
@@ -36,13 +33,13 @@ describe('AzureAuth', () => {
3633
},
3734
} as User;
3835

39-
expect(auth.isAuthProvider(user)).to.equal(false);
36+
strictEqual(auth.isAuthProvider(user), false);
4037
});
4138

4239
it('should be false for null user.authProvider', () => {
4340
const user = {} as User;
4441

45-
expect(auth.isAuthProvider(user)).to.equal(false);
42+
strictEqual(auth.isAuthProvider(user), false);
4643
});
4744

4845
it('should populate from auth provider', async () => {
@@ -63,12 +60,11 @@ describe('AzureAuth', () => {
6360
const requestContext = new RequestContext(testUrl1, HttpMethod.GET);
6461

6562
await config.applySecurityAuthentication(requestContext);
66-
expect(requestContext.getHeaders()).to.not.be.undefined;
67-
expect(requestContext.getHeaders()['Authorization']).to.equal(`Bearer ${token}`);
63+
strictEqual(requestContext.getHeaders()?.['Authorization'], `Bearer ${token}`);
6864

6965
requestContext.setHeaderParam('Host', 'foo.com');
7066
await config.applySecurityAuthentication(requestContext);
71-
expect(requestContext.getHeaders().Authorization).to.equal(`Bearer ${token}`);
67+
strictEqual(requestContext.getHeaders().Authorization, `Bearer ${token}`);
7268
});
7369

7470
it('should populate from auth provider without expiry', async () => {
@@ -88,8 +84,7 @@ describe('AzureAuth', () => {
8884
const requestContext = new RequestContext(testUrl1, HttpMethod.GET);
8985

9086
await config.applySecurityAuthentication(requestContext);
91-
expect(requestContext.getHeaders()).to.not.be.undefined;
92-
expect(requestContext.getHeaders()['Authorization']).to.equal(`Bearer ${token}`);
87+
strictEqual(requestContext.getHeaders()?.['Authorization'], `Bearer ${token}`);
9388
});
9489

9590
it('should populate rejectUnauthorized=false when skipTLSVerify is set', async () => {
@@ -110,7 +105,7 @@ describe('AzureAuth', () => {
110105

111106
await config.applySecurityAuthentication(requestContext);
112107
// @ts-expect-error
113-
expect(requestContext.getAgent().options.rejectUnauthorized).to.equal(false);
108+
strictEqual(requestContext.getAgent().options.rejectUnauthorized, false);
114109
});
115110

116111
it('should not set rejectUnauthorized if skipTLSVerify is not set', async () => {
@@ -133,10 +128,10 @@ describe('AzureAuth', () => {
133128

134129
await config.applySecurityAuthentication(requestContext);
135130
// @ts-expect-error
136-
expect(requestContext.getAgent().options.rejectUnauthorized).to.equal(undefined);
131+
strictEqual(requestContext.getAgent().options.rejectUnauthorized, undefined);
137132
});
138133

139-
it('should throw with expired token and no cmd', () => {
134+
it('should throw with expired token and no cmd', async () => {
140135
const config = new KubeConfig();
141136
config.loadFromClusterAndUser(
142137
{ skipTLSVerify: false } as Cluster,
@@ -151,12 +146,12 @@ describe('AzureAuth', () => {
151146
);
152147
const requestContext = new RequestContext(testUrl1, HttpMethod.GET);
153148

154-
return expect(config.applySecurityAuthentication(requestContext)).to.eventually.be.rejectedWith(
155-
'Token is expired!',
156-
);
149+
await rejects(config.applySecurityAuthentication(requestContext), {
150+
message: 'Token is expired!',
151+
});
157152
});
158153

159-
it('should throw with bad command', () => {
154+
it('should throw with bad command', async () => {
160155
const config = new KubeConfig();
161156
config.loadFromClusterAndUser(
162157
{ skipTLSVerify: false } as Cluster,
@@ -173,9 +168,9 @@ describe('AzureAuth', () => {
173168
);
174169
const requestContext = new RequestContext(testUrl1, HttpMethod.GET);
175170

176-
return expect(config.applySecurityAuthentication(requestContext)).to.eventually.be.rejectedWith(
177-
/Failed to refresh token/,
178-
);
171+
await rejects(config.applySecurityAuthentication(requestContext), {
172+
message: /Failed to refresh token/,
173+
});
179174
});
180175

181176
it('should exec when no cmd and token is not expired', async () => {
@@ -224,8 +219,7 @@ describe('AzureAuth', () => {
224219
const requestContext = new RequestContext(testUrl1, HttpMethod.GET);
225220
await config.applySecurityAuthentication(requestContext);
226221

227-
expect(requestContext.getHeaders()).to.not.be.undefined;
228-
expect(requestContext.getHeaders()['Authorization']).to.equal(`Bearer ${token}`);
222+
strictEqual(requestContext.getHeaders()?.['Authorization'], `Bearer ${token}`);
229223
});
230224
it('should exec without access-token', async () => {
231225
// TODO: fix this test for Windows
@@ -252,8 +246,7 @@ describe('AzureAuth', () => {
252246
const requestContext = new RequestContext(testUrl1, HttpMethod.GET);
253247
await config.applySecurityAuthentication(requestContext);
254248

255-
expect(requestContext.getHeaders()).to.not.be.undefined;
256-
expect(requestContext.getHeaders()['Authorization']).to.equal(`Bearer ${token}`);
249+
strictEqual(requestContext.getHeaders()?.['Authorization'], `Bearer ${token}`);
257250
});
258251
it('should exec without access-token', async () => {
259252
// TODO: fix this test for Windows
@@ -280,8 +273,7 @@ describe('AzureAuth', () => {
280273
const requestContext = new RequestContext(testUrl1, HttpMethod.GET);
281274
await config.applySecurityAuthentication(requestContext);
282275

283-
expect(requestContext.getHeaders()).to.not.be.undefined;
284-
expect(requestContext.getHeaders()['Authorization']).to.equal(`Bearer ${token}`);
276+
strictEqual(requestContext.getHeaders()?.['Authorization'], `Bearer ${token}`);
285277
});
286278
it('should exec succesfully with spaces in cmd', async () => {
287279
// TODO: fix this test for Windows
@@ -308,7 +300,6 @@ describe('AzureAuth', () => {
308300
const requestContext = new RequestContext(testUrl1, HttpMethod.GET);
309301
await config.applySecurityAuthentication(requestContext);
310302

311-
expect(requestContext.getHeaders()).to.not.be.undefined;
312-
expect(requestContext.getHeaders()['Authorization']).to.equal(`Bearer ${token}`);
303+
strictEqual(requestContext.getHeaders()?.['Authorization'], `Bearer ${token}`);
313304
});
314305
});

0 commit comments

Comments
 (0)