Skip to main content
Version: 2.x

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 in 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": "+",
"testFiles": ["./test"]
}

With the config file created, we need to put the necesary data to getting started with tests. Check configurations for details about each option.

Okay, after have the options setted, Let's create or first test.

Let's supose that we have a file called bot.js (our bot) and it's have the following structure:

bot.js
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 aplication 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.json

This bot have 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 bot. Always use a prefix for your bot).

Let's implement the bot.test.js:

./tests/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 global function. We pretend to create then in the future.

So, what we got here?

The first thing that we do is call beforeStart function. This function is mainly used to init our bot connection (remember the loginBot()?).

Then we can declare the group function. Be clare that group and test functions are optional. You can use just the expect function. That is what make the magic happens. (uuuh)

expect wait for the command name. Pay attemtion that we do not put the bot prefix. Corde already do it for us. If we put it, we will se 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:

Result code

And that is it.