Backport of 8f8dc830dfeb321b0de3325a8d2c859fdc20c3af from master
Thanks Trey Hunner for cleaning the initial patch and Tim Graham for the review.
Setup QUnit, added tests, and measured test coverage. Thanks to Nick Sanford for the initial tests.