Welcome! In this lab you'll learn about Test-Driven Development (TDD) by getting some hands-on experience writing code using TDD.
The lab is written in Node.js because JavaScript is a simple, ubiquitous language. You will not need to be an expert in it to complete this lab. However, you must possess some basic programming experience to get the most out of it.
You should have Node.js 10.x or above installed, preferably the latest Long-Term Support (LTS) version. At the time of this writing, that is version 12. Get it here or use nvm
(We strongly recommend using nvm
).
Our testing environment consists of the following tools. You will not need detailed knowledge of these tools to complete this lab.
We're using Node Tap as our test framework. If you're not familiar with it, you should familiarize yourself with the API.
We use the Sinon library for mocking, stubbing, and spying in our unit tests. Check out the API if you'd like to know more. It's very useful for writing unit tests, so we encourage you to familiarize yourself with it.
For hard-to-unit-test modules like express
, we've used Proxyquire. Hopefully, you won't need this much, but it is an important tool to be aware of.
Run:
npm ci
Run these commands in your terminal with npm run <command>
(ex. npm run start:dev
):
lint
- Run the XO linter. This helps us not worry about coding too much so we can focus on learning TDD.lint:fix
- Auto-fix XO lint issues.start:dev
- Run our Express server usingnodemon
. This will refresh the server every time it detects a change to the source code.start
- Run the Express server normally.test
- Run the Node Tap tests.test:watch
- Run the Node Tap tests in watch-mode so that they run every time a change is detected. Warning: you may have to restart this job if you add or delete a file.
You will mostly be using npm run test
, npm run test:watch
, and npm run start:dev
.
There are other popular testing frameworks such as Jest, Mocha, QUnit and Ava.js. They are all fine and will serve you well if you decide to try them out. Our particular setup prioritizes certain aspects of testing:
- A stub-first mindset to testing (vs. a spy-first mentality like Jasmine-based frameworks)
- Very fast tests with native parallelized test runs
- The option to have a shallowly-nested test structure