I remember watching a talk on testing JS by Christian Johansen, the author of https://www.tddjs.com/. I was just getting into testing JS and it was all still mystery to me. The moment he explained what an assertion is by writing an
assert() function from scratch everything just clicked.
I think that’s the key to comprehension for many complicated topics. You need to bridge the gap and connect the new, unknown topic, to a known topic, so your brain can make connections and store them in memory that way.
What is a Test?
A test in JS or any programming language verifies the correctness of something. This verification process is called an assertion.
At the simplest level we could have an
assert() function like this:
Testing frameworks like Jasmine have many types of assertion functions.
A simple Test
You could use this method to write a bunch of tests.
If you wanted to get a step further you could use a test runner like Mocha. A test runner allows you to write a bunch of tests and then provides a simple mechanism to run all the tests.
Writing a test with Mocha
This example is straight from the Mocha docs.
Add mocha to your JS project:
$ npm install mocha
Create your test folder and first test file.
$ mkdir test
test.js in that folder and open in in your editor.
Create a Mocha test:
Then we need to run the test file using mocha.
The assertions passed. Great. We can now add more tests.
It can be easy to get overwhelmed with all the tooling around tests, but remember two things: 1) At center of all the tests are simple assertions that you are making about your program. 2) You have assertions inside tests, and test runners run all the tests.