Skip to content

Commit

Permalink
Disconnect clients at end of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Throne3d committed Aug 9, 2020
1 parent 1a1ead9 commit 6a98615
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ class Bot {
this.attachListeners();
}

disconnect() {
this.ircClient.disconnect();
this.discord.destroy();
Object.values(this.webhooks).forEach(x => x.client.destroy());
}

attachListeners() {
this.discord.on('ready', () => {
logger.info('Connected to Discord');
Expand Down
4 changes: 3 additions & 1 deletion test/bot-events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import discord from 'discord.js';
import Bot from '../lib/bot';
import logger from '../lib/logger';
import createDiscordStub from './stubs/discord-stub';
import createWebhookStub from './stubs/webhook-stub';
import ClientStub from './stubs/irc-client-stub';
import config from './fixtures/single-test-config.json';

Expand All @@ -25,7 +26,6 @@ describe('Bot Events', function () {
bot.sendToIRC = sandbox.stub();
bot.sendToDiscord = sandbox.stub();
bot.sendExactToDiscord = sandbox.stub();
// TODO: investigate this making tests hang?
return bot;
};

Expand All @@ -37,13 +37,15 @@ describe('Bot Events', function () {
this.sendStub = sandbox.stub();
irc.Client = ClientStub;
discord.Client = createDiscordStub(this.sendStub);
discord.WebhookClient = createWebhookStub(this.sendStub);
ClientStub.prototype.send = sandbox.stub();
ClientStub.prototype.join = sandbox.stub();
this.bot = createBot();
this.bot.connect();
});

afterEach(function () {
this.bot.disconnect();
sandbox.restore();
});

Expand Down
2 changes: 2 additions & 0 deletions test/stubs/discord-stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export default function createDiscordStub(sendStub) {
this.rest = this.createRestStub();
}

destroy() {}

addTextChannel(guild, textChannel) {
const textChannelData = {
type: discord.Constants.ChannelTypes.TEXT,
Expand Down
3 changes: 3 additions & 0 deletions test/stubs/irc-client-stub.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import events from 'events';

/* eslint-disable class-methods-use-this */
class ClientStub extends events.EventEmitter {
constructor(...args) {
super();
this.nick = args[1]; // eslint-disable-line prefer-destructuring
}

disconnect() {}
}

export default ClientStub;
2 changes: 2 additions & 0 deletions test/stubs/webhook-stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ export default function createWebhookStub(sendWebhookMessage) {
sendWebhookMessage(...args);
return new Promise(() => {});
}

destroy() {}
};
}

0 comments on commit 6a98615

Please sign in to comment.