A quick search through CodePen shows that many have figured out how to unit test JS in a pen already. Here are four pens I forked to show you how to set up JS unit tests with Mocha/Chai, Jasmine, Tape and qUnit.
- Feel free to hack your own functions and tests in. All the code is editable from this page in these pens.
All pens test this simple adding function
const addr = (a, b) => a + b;.
- All tests have a "failing" test that is commented out. Remove the comment if you want to see what a failing test looks like.
Mocha and Chai
Forked fromBrian Holt
Forked fromHussein Morsy
Forked fromAndrew Worcester
The first challenge with unit testing is figuring out how to do it in the first place. The second challenge is just making yourself do it. The third challenge is keeping your tests simple. Regardless, if you need to set up a quick test in a pen, then fork one of these and get to testing.
Wanna read up on unit testing?
Here you go:
- Why I use Tape Instead of Mocha & So Should You
- Getting Started with QUnit
Go fall in love. Stay awesome!