DawnbrandBots / bastion-bot

A Discord bot for displaying information about Yu-Gi-Oh! cards in all official languages.
https://discord.com/application-directory/383854640694820865
GNU Affero General Public License v3.0
10 stars 11 forks source link

Testing and discord.js #275

Open kevinlul opened 1 year ago

kevinlul commented 1 year ago

This research applies to Emcee too.

I'm not satisfied with the approach to unit tests right now and how we are doing mocking. Relevant links for now:

From 2021:

Example of unit tests from a Discord.js core dev https://github.com/skyra-project/skyra/blob/refactor/switch-to-v13/tests/mocks/MockInstances.ts https://github.com/skyra-project/skyra/blob/main/tests/lib/database/settings/structures/PermissionNodeManager.test.ts https://github.com/discordjs/discord.js-modules/blob/main/packages/rest/__tests__/RequestHandler.test.ts another one with lower level mocking at nock/msw

2023: https://github.com/cordejs/corde integration test with a second test bot, but not updated to recent discord.js. We can easily just implement this ourselves. Only works for messages anyway because Discord does not allow bots to send application commands. https://github.com/discordjs/discord.js/pull/7693 some other Discord.js dev test https://github.com/discordjs/discord.js/discussions/6179 discussion on testing. One sample with a massive amount of mocking. https://github.com/cherryblossom000/discord.js-mock somebody's attempt in that discussion to create a mocking library, abandoned, only useful for reference if anything

kevinlul commented 1 year ago

Tests in the main Discord.js package are not automated unit tests. They are manually verified and require connecting to the gateway. With the exception of that cancelled CR and a whole mock server package. @discordjs/rest and @discordjs/ws do have them, but the latter is not employed yet. Sapphire framework has few tests as well.

kevinlul commented 1 year ago

For REST requests, undici has a native MockAgent because it does not depend on Node.js' HTTP stack. nock can mock parts of this HTTP stack for use with other libraries (node-fetch, got, etc.) but I don't believe it works with HTTP/2.

kevinlul commented 1 year ago

See https://github.com/nock/nock/issues/1625 and https://github.com/nock/nock/issues/1625

kevinlul commented 3 months ago

https://github.com/b-u-n/discord.js-mock-interactions https://github.com/Markkop/corvo-astral/tree/master/tests