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 from Sitepoint

Jasmine

Forked from Brian Holt

Tape

Forked from Hussein Morsy

qUnit

Forked from Andrew Worcester

Outtro

The first challenge with unit testing is figuring out how to do it in the first place. The second challenge is keeping your tests simple. Remember that the crucial parts of your program are its inputs and outputs. Test if your program/app/function consumes its input correcty. Then test that it spits out the correct result. The third challenge is just making yourself do it. 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:

Go fall in love. Stay awesome!