Skip to content

Commit

Permalink
test: replace chai with node:assert
Browse files Browse the repository at this point in the history
This commit replaces the uses of chai and chai-as-promised
throughout the codebase.
  • Loading branch information
cjihrig committed Jan 19, 2025
1 parent 95fe749 commit b83b8d3
Show file tree
Hide file tree
Showing 25 changed files with 821 additions and 950 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ Run `npm run lint` or install an editor plugin.

# Testing

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

To run tests, execute the following:
Expand Down
1 change: 0 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export default tseslint.config(
'@typescript-eslint/no-empty-object-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-unused-expressions': 'off',
'@typescript-eslint/no-unused-vars': ['error', { args: 'none' }],
},
},
Expand Down
12 changes: 6 additions & 6 deletions src/attach_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from 'chai';
import { strictEqual } from 'node:assert';
import WebSocket from 'isomorphic-ws';
import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers';
import { anyFunction, anything, capture, instance, mock, verify, when } from 'ts-mockito';
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('Attach', () => {
await attach.attach(namespace, pod, container, osStream, errStream, isStream, false);
const [, , outputFn] = capture(fakeWebSocketInterface.connect).last();

expect(outputFn).to.not.be.null;
strictEqual(typeof outputFn, 'function');

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

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

buffer = Buffer.alloc(1024, 20);
outputFn(WebSocketHandler.StderrStream, buffer);
expect(errStream.size()).to.equal(1024);
strictEqual(errStream.size(), 1024);
buff = errStream.getContents() as Buffer;
for (let i = 0; i < 1024; i++) {
expect(buff[i]).to.equal(20);
strictEqual(buff[i], 20);
}

const initialTerminalSize: TerminalSize = { height: 0, width: 0 };
Expand Down
51 changes: 21 additions & 30 deletions src/azure_auth_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { use, expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { rejects, strictEqual } from 'node:assert';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

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

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

use(chaiAsPromised);

describe('AzureAuth', () => {
const testUrl1 = 'https://test1.com';
let auth: AzureAuth;
Expand All @@ -26,7 +23,7 @@ describe('AzureAuth', () => {
},
} as User;

expect(auth.isAuthProvider(user)).to.equal(true);
strictEqual(auth.isAuthProvider(user), true);
});

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

expect(auth.isAuthProvider(user)).to.equal(false);
strictEqual(auth.isAuthProvider(user), false);
});

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

expect(auth.isAuthProvider(user)).to.equal(false);
strictEqual(auth.isAuthProvider(user), false);
});

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

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

requestContext.setHeaderParam('Host', 'foo.com');
await config.applySecurityAuthentication(requestContext);
expect(requestContext.getHeaders().Authorization).to.equal(`Bearer ${token}`);
strictEqual(requestContext.getHeaders().Authorization, `Bearer ${token}`);
});

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

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

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

await config.applySecurityAuthentication(requestContext);
// @ts-expect-error
expect(requestContext.getAgent().options.rejectUnauthorized).to.equal(false);
strictEqual(requestContext.getAgent().options.rejectUnauthorized, false);
});

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

await config.applySecurityAuthentication(requestContext);
// @ts-expect-error
expect(requestContext.getAgent().options.rejectUnauthorized).to.equal(undefined);
strictEqual(requestContext.getAgent().options.rejectUnauthorized, undefined);
});

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

return expect(config.applySecurityAuthentication(requestContext)).to.eventually.be.rejectedWith(
'Token is expired!',
);
await rejects(config.applySecurityAuthentication(requestContext), {
message: 'Token is expired!',
});
});

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

return expect(config.applySecurityAuthentication(requestContext)).to.eventually.be.rejectedWith(
/Failed to refresh token/,
);
await rejects(config.applySecurityAuthentication(requestContext), {
message: /Failed to refresh token/,
});
});

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

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

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

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

expect(requestContext.getHeaders()).to.not.be.undefined;
expect(requestContext.getHeaders()['Authorization']).to.equal(`Bearer ${token}`);
strictEqual(requestContext.getHeaders()?.['Authorization'], `Bearer ${token}`);
});
});
Loading

0 comments on commit b83b8d3

Please sign in to comment.