Unit Testing DiscordJS Types
I'm trying to write some unit tests for my bot and I need to generate some instances of some DiscordJS classes, but I'm having a hard time figuring out how to do so easily. Is there a quick/easy way to create instances of classes like Users, Guilds, Channels, etc, without having to implement all of the properties/functions/etc?
9 Replies
• What's your exact discord.js
npm list discord.js
and node node -v
version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.
I should mention that I'm hoping to be able to keep these tests small and runnable outside of needing to actually start up a connection to Discord
Those classes have constructors if you have API data that you want to use, but if you're trying to recreate a full class from nothing, then you should probably just let your function accept a union type: either a User class, or a custom-made object with necessary properties that are the same type as a normal User
I can pull data from my test server, that's not an issue. I tried accessing the constructor, but they all seem private and not available
Well, some are
e.g. the constructor for the Guild type is marked as
private constructor(client: Client, data: RawGuildData);
Thanks. I'll give that a whirl.Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Yea I was. My unit testing venture didn't work out too well for other reasons, and I'm still futzing with that, but as far as creating instances of the DiscordJS objects from JSON, this worked.
I had to first get the JSON data from the server though, or at least that was the easiest way to do it
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
I just did a few JSON dumps from a dev discord server for testing purposes. I just saved them into JSON files and read them in during the tests.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View