I am coding a Discord bot and I want to test some code. With the discord.js library, we have to use a client to connect to the Discord API. At the beginning the client receives an event when it's ready:
In my index.js
client.on('ready', () => {})
But in my test I need information (a guild) coming from the cache of the client:
beforeAll(() => {
client.on('ready', () => {
// => guild that I need for test
guildToTest = client.guilds.cache.get('912785389704609832');
});
});
describe('setCounterForRoles testing', () => {
test('Unit test', async () => {
// => Here guildToTest is null
const guilds = await setCounterForRoles(guildToTest);
});
});
I need the guild for the test but this test is processed before the event ready is coming so the guild is null.
How can I make sure that the ready event is fired before running the tests?
I read some things in the docs on jest.spyOn() but I don't know if it's the solution.
