Getting Started
Install Corde using yarn
:
yarn add --dev corde
Or npm
:
npm install --save-dev corde
Note: Corde documentation uses yarn
commands, but npm
will also work. You can compare yarn
and npm
commands in the yarn docs, here.
info
This example uses Discord.js in the bot showcase.
Let's get started writing a simple test for a bot that sends just send a simple message.
First, we need the corde
config file. The fastest way to do that is by calling corde CLI with the following command:
yarn corde --init
This command will create the corde.config.json
file at the root of your project. For more details about the init
command
check the CLI documentation here.
This file will have the following structure:
{
"cordeBotToken": "",
"botTestId": "",
"botToken": "",
"guildId": "",
"channelId": "",
"botPrefix": "+",
"testMatches": ["./test"]
}
With the config file created, we need to put the necessary data to getting started with tests. Check configurations for details about each option.
Okay, after having the options set, Let's create the first test.
Let's supose that we have a file called bot.js
(our bot) and it's have the following structure:
const Discord = require('discord.js');
client.on('message', async (message) => {
if (command === 'hello') {
await message.channel.send('Hello!!');
}
}
function loginBot() {
client.login(config.token);
}
exports.client = client;
exports.loginBot = loginBot;
Then let's create a test
folder in the root of your application and then a file called for example: bot.test.js
.
By doing this, we have the following structure of files
.
โโโ bot.js
โโโ test
| โโโ bot.test.js
โโโ _site
โโโ corde.config.json
This bot has just a simple task: Send a fixed message when the users send hello.
So, we need to create a test to ensure that the bot will always send the fixed message (Hello!!) when
the user call !hello (!
is the prefix for the bot. Always use a prefix for your bot).
Let's implement the bot.test.js
:
const { group, test, command, beforeStart, afterAll, expect } = require("corde");
// You can also import const corde = require("corde"); This is a default export with all others
// functions.
const { client, loginBot } = require("..");
beforeStart(() => {
loginBot();
});
group("main commands", () => {
test("hello command should return 'Hello!!'", () => {
expect("hello").toReturn("Hello!!");
});
});
afterAll(() => {
client.destroy();
});
info
Corde does not have a global function. We pretend to create them in the future.
So, what we got here?
The first thing that we do is call the beforeStart
function. This function is mainly used to
init our bot connection (remember the loginBot()
?).
Then we can declare the group
function. Be clear that group
and test
functions are optional.
You can use just the expect
function. That is what makes the magic happens. (uh)
expect
wait for the command name. Pay attention that we do not put the bot prefix. Corde already does it
for us. If we put it, we will see Corde sending !!hello
to Discord (we do not want this).
After that, we call toReturn
with the parameter Hello!!
. So, we are telling Corde that we expect that after sending !hello
,
our bot should send Hello!!
. And that is it.
To execute this test, we just need to call:
yarn corde
It will run all tests and print the result like follow:
And that is it.