24

I finally stopped being lazy and wrote 31 unit tests for my Discord bot.

Nothing is more satisfying than seeing them all pass and the GitHub workflow working without any problems. :)

Comments
  • 7
  • 5
    I see you are a man of culture aswell
  • 2
    Okay I have a question, so I know what they are but I don’t know how to write a unit test and how to implement it I to my projects and when. Is there any resources you could point me to that’ll help me with it? I haven’t found good ones
  • 2
    @Bubbles I also just got into them ^^
    I wrote those with mocha, a TDD tool for Node.js.

    suite("Danbooru", () => {
    test("Danbooru blocks non-NSFW", (done) => {
    danbooruCommand.content(["cute"], false).then(msg => {
    assert.equal(msg.startsWith(":x: Sorry,"), true);
    done();
    });
    });
    });

    This small code starts a new test category named "Danbooru" and a test named "Danbooru blocks non-NSFW".
    The test executes a function, gets the return value and asserts whether it starts with ":x:Sorry,". Then it calls done() and the assert result is used as test result.
  • 0
    @PrivateGER so does something in the current code call this new file that the test is in and then tests each one (if there are more than one in a file)
  • 1
    Not quite.
    Tests are all collected in a seperate "test" directory.
    mocha runs all the JS files found in it.
    Tests must run completely seperate from the main code, otherwise you break the testing environment.
  • 2
    @PrivateGER look into chai, it's my goto package to use with mocha.
  • 0
    Would it be at all possible to get some more insight as how you wrote your unit tests? Did you mock data?
Add Comment