Skip to content

Commit 2fecb13

Browse files
author
Filip Maj
committed
add a unit test for connect() method of SlackWebSocket
1 parent 6f918b0 commit 2fecb13

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

packages/socket-mode/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@
5757
"@tsconfig/recommended": "^1.0.7",
5858
"@types/chai": "^4",
5959
"@types/mocha": "^10",
60+
"@types/proxyquire": "^1.3.31",
6061
"@types/sinon": "^17",
6162
"c8": "^10.1.2",
6263
"chai": "^4",
6364
"mocha": "^10",
6465
"nodemon": "^3.1.0",
66+
"proxyquire": "^2.1.3",
6567
"shx": "^0.3.2",
6668
"sinon": "^19",
6769
"source-map-support": "^0.5.21",

packages/socket-mode/src/SlackWebSocket.spec.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
import { ConsoleLogger } from '@slack/logger';
22
import { assert } from 'chai';
33
import EventEmitter from 'eventemitter3';
4+
import proxyquire from 'proxyquire';
45
import sinon from 'sinon';
5-
6-
import { SlackWebSocket } from './SlackWebSocket';
6+
proxyquire.noPreserveCache();
77
import logModule from './logger';
88

9+
// A slightly spruced up event emitter aiming at mocking out the `ws` library's `WebSocket` class
10+
class WSMock extends EventEmitter {
11+
// biome-ignore lint/suspicious/noExplicitAny: event listeners can accept any args
12+
addEventListener(evt: string, fn: (...args: any[]) => void) {
13+
this.addListener.call(this, evt, fn);
14+
}
15+
}
16+
917
describe('SlackWebSocket', () => {
1018
const sandbox = sinon.createSandbox();
11-
19+
let SlackWebSocket: typeof import('./SlackWebSocket').SlackWebSocket;
20+
beforeEach(() => {
21+
SlackWebSocket = proxyquire.load('./SlackWebSocket', {
22+
ws: {
23+
WebSocket: WSMock,
24+
},
25+
}).SlackWebSocket;
26+
});
1227
afterEach(() => {
1328
sandbox.restore();
1429
});
@@ -38,4 +53,31 @@ describe('SlackWebSocket', () => {
3853
assert.isFalse(logFactory.called);
3954
});
4055
});
56+
describe('WebSocket event handling', () => {
57+
it('should call disconnect() if websocket emits an error', async () => {
58+
// an exposed event emitter pretending its a websocket
59+
const ws = new WSMock();
60+
// mock out the `ws` library and have it return our event emitter mock
61+
SlackWebSocket = proxyquire.load('./SlackWebSocket', {
62+
ws: {
63+
WebSocket: class Fake {
64+
constructor() {
65+
// biome-ignore lint/correctness/noConstructorReturn: for test mocking purposes
66+
return ws;
67+
}
68+
},
69+
},
70+
}).SlackWebSocket;
71+
const sws = new SlackWebSocket({
72+
url: 'whatevs',
73+
client: new EventEmitter(),
74+
clientPingTimeoutMS: 1,
75+
serverPingTimeoutMS: 1,
76+
});
77+
const discStub = sinon.stub(sws, 'disconnect');
78+
sws.connect();
79+
ws.emit('error', { error: new Error('boom') });
80+
sinon.assert.calledOnce(discStub);
81+
});
82+
});
4183
});

packages/socket-mode/test/integration.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('Integration tests with a WebSocket server', () => {
132132
});
133133
describe('unexpected socket messages sent to client', () => {
134134
const debugLoggerSpy = sinon.stub(); // add the following to expose further logging: .callsFake(console.log);
135-
const noop = () => { };
135+
const noop = () => {};
136136
beforeEach(() => {
137137
client = new SocketModeClient({
138138
appToken: 'whatever',

0 commit comments

Comments
 (0)