dimanche 29 mars 2020

jest testing discord bot commands

So I have a file that I use module exports on and it has 4 fields among which an execute field that takes 2 args and is essentially a function. It doesn't return anything instead it uses discord.js and runs this message.channel.send('Pong');. I want to test this using jest How do I: 1 - Make sure that the message.channel.send was called with 'Pong' as args 2 - How do I mock it so it doesnt actually call it (i just want to make sure that the text inside of it, like the actual argument is 'Pong' since calling it won't work due to the lack of a proper message object)

I can access the actual command and execute it but I am unsure as to how to check the contents of message.channel.send. The message object cannot be reconstructed by me so that might also need mocking.

I'm using discord.js but that shouldn't really matter.

I will also have to test commands that feature functions that do have returns so how should I go about them?

1 commentaire:

  1. The closest I've found is proxyquire'ing Discord.js and then stubbing Discord.Client (with Sinon) so that it always returns the client you've built. Your can then emit Events and add EventListeners yourself.

    RépondreSupprimer