Skip to content

Commit

Permalink
add a unit test for connect() method of SlackWebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Maj committed Nov 25, 2024
1 parent 6f918b0 commit 2fecb13
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/socket-mode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@
"@tsconfig/recommended": "^1.0.7",
"@types/chai": "^4",
"@types/mocha": "^10",
"@types/proxyquire": "^1.3.31",
"@types/sinon": "^17",
"c8": "^10.1.2",
"chai": "^4",
"mocha": "^10",
"nodemon": "^3.1.0",
"proxyquire": "^2.1.3",
"shx": "^0.3.2",
"sinon": "^19",
"source-map-support": "^0.5.21",
Expand Down
48 changes: 45 additions & 3 deletions packages/socket-mode/src/SlackWebSocket.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import { ConsoleLogger } from '@slack/logger';
import { assert } from 'chai';
import EventEmitter from 'eventemitter3';
import proxyquire from 'proxyquire';
import sinon from 'sinon';

import { SlackWebSocket } from './SlackWebSocket';
proxyquire.noPreserveCache();
import logModule from './logger';

// A slightly spruced up event emitter aiming at mocking out the `ws` library's `WebSocket` class
class WSMock extends EventEmitter {
// biome-ignore lint/suspicious/noExplicitAny: event listeners can accept any args
addEventListener(evt: string, fn: (...args: any[]) => void) {
this.addListener.call(this, evt, fn);
}
}

describe('SlackWebSocket', () => {
const sandbox = sinon.createSandbox();

let SlackWebSocket: typeof import('./SlackWebSocket').SlackWebSocket;
beforeEach(() => {
SlackWebSocket = proxyquire.load('./SlackWebSocket', {
ws: {
WebSocket: WSMock,
},
}).SlackWebSocket;
});
afterEach(() => {
sandbox.restore();
});
Expand Down Expand Up @@ -38,4 +53,31 @@ describe('SlackWebSocket', () => {
assert.isFalse(logFactory.called);
});
});
describe('WebSocket event handling', () => {
it('should call disconnect() if websocket emits an error', async () => {
// an exposed event emitter pretending its a websocket
const ws = new WSMock();
// mock out the `ws` library and have it return our event emitter mock
SlackWebSocket = proxyquire.load('./SlackWebSocket', {
ws: {
WebSocket: class Fake {
constructor() {
// biome-ignore lint/correctness/noConstructorReturn: for test mocking purposes
return ws;
}
},
},
}).SlackWebSocket;
const sws = new SlackWebSocket({
url: 'whatevs',
client: new EventEmitter(),
clientPingTimeoutMS: 1,
serverPingTimeoutMS: 1,
});
const discStub = sinon.stub(sws, 'disconnect');
sws.connect();
ws.emit('error', { error: new Error('boom') });
sinon.assert.calledOnce(discStub);
});
});
});
2 changes: 1 addition & 1 deletion packages/socket-mode/test/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('Integration tests with a WebSocket server', () => {
});
describe('unexpected socket messages sent to client', () => {
const debugLoggerSpy = sinon.stub(); // add the following to expose further logging: .callsFake(console.log);
const noop = () => { };
const noop = () => {};
beforeEach(() => {
client = new SocketModeClient({
appToken: 'whatever',
Expand Down

0 comments on commit 2fecb13

Please sign in to comment.