To run the samples in this demo you will first need to run npm install from inside the repo root. All demos have npm scripts to simplify execution.
-
TAP = Test Anything Protocol
- Basic protocol created for testing Perl scripts.
- Implementations in multiple languages
-
tapJS library written by Isaac Z. Schlueter (@izs, founder of npm)- All of npm's test are built on tap, long term support.
- Pros:
- Can detect a test timing out.
- Each test runs in own child process, less risk of environment corruption
- Cons:
- Cannot do browser testing
-
tapeJS library written by James Halliday (@substack, created Browserify)- Now has multiple maintainers and is used in hundreds of projects.
- Pros:
- Lighter-weight, faster.
- Can be used in the browser.
- Cons:
- Most browser test centered around Browserify
- No examples/tools for using with RequireJS
-
Both libraries consist of two parts:
- Testing Framework (
require('tape').test)
- Outputs TAP to stdout
- All tests assumed to be asynchronous
test('name', function (t) {})- Nest
test()calls for subtests, instead ofdefine()andit()
- Nest
tcontains assertions and.end()- Meshes well with sinon
t.pass(spy.calledOnce)
- Can also use chai assertions.
- Anything thrown within the
test()function gets registered as a failed assertion, just like in mocha. - Wrap chai assertions in a
t.shouldNotThrow()function to prevent undone tests.
- Anything thrown within the
- Testing Harness (shell command)
- Receives TAP from tests.
tapformats into clean output, add-Cto get raw TAPtapeaggregates results into a single output, must be piped to formatter.
- Testing Framework (
-
Both libraries can run each others tests.
- For node testing there is a benefit to building tests with
tapeand running them withtap.
- For node testing there is a benefit to building tests with
-
Tests can be parallelized using Prove, a Perl TAP harness.
- Dunno why nobody has made a node based parallelized test runner.
-
Show
index.js- Something wrong with this file, but don't look for it yet.
-
Show
mocha/test.js- Walk through test structure, point out assertions.
-
npm run mocha- Show that test passes, everything looks good right?
-
Show
tape/test.js- Same test structure, just different way of calling assertions.
-
npm run tape- Test is failing, why?
- plan != count
-
node tape/test.jsto get raw output- Tape tests are normal node processes that output TAP to stdout
- Each assertion is enumerated with
okandnot ok, indents for sub-tests. t.plan(4)call identifies expected four total assertions- Count off assertions, only three ran.
- Note that close connection is missing
- Identify missing parens on closeConnection call in
index.js - Add parens, re-run test. Ta da.
-
Show
mocha/test-working.js- Mocha can be made to catch missing executions
- Mock functions wrapped in sinon spies
- Assertions that spies were called at end of test
- Requires more effort, greater risk, complicates tests
-
Show
index2.js- Basic unit, semi-contrived example. Returns collection of data.
-
Show
mocha/test2.js- Asserting that all three pieces of data pass.
-
npm run mocha2- Test fails at first assertion.
-
Show
tape/test2.js- Exact same test.
-
npm run tape2- Outputs all three failing assertions.
- Because tape doesn't rely on assertions, it doesn't fall down on the first failure, giving more useful output.
-
npm run cover- Show coverage folder.
-
npm run cover-mocha- Show coverage.
-
Both are created the same way through istanbul.
istanbul cover tape tape/*istanbul cover _mocha mocha/*- Must use
_mochadue to test runner isolation inmocha taprunner has same problem, solves by bundling istanbul into itself
- Must use