January 2011
Intermediate to advanced
224 pages
5h 43m
English
Because JavaScript programs aren’t checked very thoroughly before execution and because
manually testing every if branch in a big program can take a lot of time,
mistakes can sit lurking in the depths of programs, even when they appear to work
fine.
For some pieces of code—those that work without too much interaction with their
environment—it is very straightforward to write automated tests. These are programs that
test programs. To test the between function we saw in this chapter, you
could, for example, write something like this:
function testBetween() { function assert(name, x) { if (!x) throw "Assertion failed: " + name; } assert("identical delimiters", between("a |b| c", "|", "|") == "b"); assert("whole string", between("[[n]]", ...