Testing Future Reference
Daily Standup
May 01, 2019
Got my tests implemented (sort of)! Thought there are still some unturned stones which I may need to turn later. So here are some references I found helpful this time around as a jumping off point.
Testing Express Node App with Mocha & Chai Hot Tips
Mocha Docs - the testing framework.
Chai Docs the assertion library.
If I decide later to keep tests in the same folder with what they’re testing, I can change my npm test
script to check all folders for files ending .test.js
:
"test": "mocha \"./{,!(node_modules)/**/}*.test.js\" || true", |
When testing asynchronous functions don’t forget to pass done()
, otherwise the test may pass incorrectly.
Traversy Media’s Intro To JavaScript Unit Testing With Mocha JS & Chai is a good & basic intro for installing Mocha & Chai and writing basic tests.
Node jSTL’s meetup video Testing, Mocha, Chai, and you! goes a bit more in-depth and explains the different between using assert
, expect
, and should
. Also explains a bit about mocking and suggests a couple npm packages to help with this.
When writing the tests, don’t use arrow functions (also here). This article (with related repo) talks some more about testing promises.
This repo has some fairly decent examples of tests in a Sequelize/Express app. The awful video that goes with the repo is here and the same person’s slightly better video (when the audio works) is here.
The npm module sequelize-mock
may be helpful for mocking the db. It looks more reliable than sequelize-mocking
.
Another option we talked about for redirecting the tests away from my actual DB was changing the Express context using middleware. Packages like express-http-context
or rewire
could be ways to do this.
This Node.js Guide to Actually Doing Integration Tests seems like it could be helpful for integration tests.
Dependency injection is a concept (look it up again) that might help.
Finally, these were a few more articles I found but didn’t implement:
- Testing with Sequelize (setup before writing tests)
- Unit-Testing Sequelize Models Made Easy (uses the author’s utility
sequelize-test-helpers
) - How to test Express.js with Jest and Supertest