Testing and Sapphire/discord.js
Heya.
I'm new to Sapphire and Discord, but not new to JavaScript or coding in general. I'd love some hints/guidance on how to structure a bot to be testable and/or hints about mocking discord messages etc in a maintainable manner or pointers to publicly available bot codebases which have useful unit and/or integration tests.
Longer intro/problem background:
I help manage a Discord where people post events that run in person - each event has 1 or more dates associated with it and a very limited number of places (3-6). We use discord events + a channel where folks running events list their events in a standard format (1 post per event so people can turn on notifications to just that channel) + each event gets a thread in another channel for folks to chat about it.
Remembering and managing these 3+ things is a lot of manual nonsense for people running events I'm planning to write a bot which gradually automates all of this, my rough plan is:
* Write code to scrape the existing discord events + parse the two relevant channels and dump stuff in sqlite (done)
* Write code to parse a canonical version of each of the events out of the text posts, and join all the dots together and report on anything missing or inconsistent (e.g. inconsistent dates)
* Write code to listen to the channels/events in real time and update the database + code to be able to 'catch up' if stuff gets posted whilst the bot is offline (this is where I feel tests would be really useful!)
* Teach the bot how to post stuff itself, and have a form - so folks can fill in 1 form and have the three things managed for them
* Eventually make ^^ the only method of posting stuff once it's proved itself
I'm interested in anything which mocks and tests any non completely trivial interactions with discord to use as inspiration before I go shave a bunch of yaks in the wrong direction.
Thanks in advance ❤️
Solution:Jump to solution
Unit testing Discord bot's has been a topic that's been discussed on and off in the DiscordJS community and the short answer has always been: just don't bother trying. It's not worth the effort. Mocking calls to the Discord API without a full blown Discord API mocking framework is nigh on impossible. The consensus has always been to extract the code that you want to test and test it separately without the scope of Discord stuff.
2 Replies
Solution
Unit testing Discord bot's has been a topic that's been discussed on and off in the DiscordJS community and the short answer has always been: just don't bother trying. It's not worth the effort. Mocking calls to the Discord API without a full blown Discord API mocking framework is nigh on impossible. The consensus has always been to extract the code that you want to test and test it separately without the scope of Discord stuff.
^ this is good advice in general. Integration testing is rarely worth it, and certainly never more than unit testing. Extract the code you have, abstract it away from any discord specifics, and test that - not only will it be easier to test but it's a far cleaner way of coding anyhow
You abstract everything away from discord and then when discord eventually blows up, you can plug and place with whatever replacement you have