Testing with Mocha

Right now I’m focusing on learning testing for my final two back-end project, and I think that I’m getting the hang of it. However, I would love to get some validation / a second opinion. Am I doing this correctly? How could I improve? Here is a link to my (admittedly over sized) Mocha test file. I’m planning on reorganizing it into smaller chunks soon.

Mocha API Integration Tests

Thank you in advance for taking a look.

1 Like

The logic looks fine to me, as far as I can tell. The biggest problem I see is that I’d love to read the output in documentation format, but most of the tests error out. I presume this is because I don’t have a database set up correctly, but I am having a hard time figuring out which db to run just by reading your code. You’ve already got a README file, which is great. It should include details on how to get the app running from nothing. Also, clearly define the commands you want to use. Why is there test and test2? Which should I be using?

A note on your NPM scripts: bash doesn’t require you to separate variables from commands, and you don’t need to use the export or set builtins when you’re inlining. NODE_ENV=testing mocha will set the NODE_ENV variable in the context of this one command and will be more readable. Use & only when you want to run more than one bash script at once. Semicolons are unnecessary here.

2 Likes

Thank you for your response! I apologize, I should have anticipated that in order to give meaningful feedback, people would need to actually run the tests instead of just reading over the test cases. :smile: In this case, it’s an easy fix. Just copy or move example.env to .env. This will allow the dotenv package to read the cookie secret that Express uses, and should resolve most/all of the test case failures. I’ll update the README to include these instructions.

The reason for two NPM test scripts is that my main dev box is a Windows 10 desktop, and my secondary is a Ubuntu laptop. I really should have named the test2 script test-ubuntu or something. I’ll adjust that as well. I didn’t realize that NPM scripts worked that way, I’ll look into that and simplify it where possible. That will streamline my process a bit, so thank you for the heads up! :grin:

Also, thank you again for taking the time to look at this, I really do appreciate the feedback.

@ZackWard Can you tell me if we can use mocha in a different test application in order to test an different main application ?

@pranshu1595 I’m sorry but I don’t understand the question.