Automated Testing

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]]", ...

Get Eloquent JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.