4 ways to unit test JS in Codepen
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:
- 5 step method to make test-driven development and unit testing easy
- Physics of Test Driven Development
- Why I use Tape Instead of Mocha & So Should You
- Unit Test Your JavaScript Using Mocha and Chai
- Jasmine – JavaScript Unit Testing Tutorial with Examples
- Testing JavaScript Modules with Tape
- Getting Started with QUnit
- JavaScript Unit Testing For Beginners
- JavaScript Functional Testing with Nightwatch.js
- JavaScript Testing: Unit vs Functional vs Integration Tests
- Wasting Time TDDing The Wrong Things
- Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Go fall in love. Stay awesome!