1 | # tape
|
2 |
|
3 | tap-producing test harness for node and browsers
|
4 |
|
5 | [![browser support](https://ci.testling.com/substack/tape.png)](http://ci.testling.com/substack/tape)
|
6 |
|
7 | [![build status](https://secure.travis-ci.org/substack/tape.svg?branch=master)](http://travis-ci.org/substack/tape)
|
8 |
|
9 | ![tape](https://web.archive.org/web/20170612184731if_/http://substack.net/images/tape_drive.png)
|
10 |
|
11 | # example
|
12 |
|
13 | ``` js
|
14 | var test = require('tape');
|
15 |
|
16 | test('timing test', function (t) {
|
17 | t.plan(2);
|
18 |
|
19 | t.equal(typeof Date.now, 'function');
|
20 | var start = Date.now();
|
21 |
|
22 | setTimeout(function () {
|
23 | t.equal(Date.now() - start, 100);
|
24 | }, 100);
|
25 | });
|
26 | ```
|
27 |
|
28 | ```
|
29 | $ node example/timing.js
|
30 | TAP version 13
|
31 | # timing test
|
32 | ok 1 should be equal
|
33 | not ok 2 should be equal
|
34 | ---
|
35 | operator: equal
|
36 | expected: 100
|
37 | actual: 107
|
38 | ...
|
39 |
|
40 | 1..2
|
41 | # tests 2
|
42 | # pass 1
|
43 | # fail 1
|
44 | ```
|
45 |
|
46 | # usage
|
47 |
|
48 | You always need to `require('tape')` in test files. You can run the tests by
|
49 | usual node means (`require('test-file.js')` or `node test-file.js`). You can
|
50 | also run tests using the `tape` binary to utilize globbing, on Windows for
|
51 | example:
|
52 |
|
53 | ```sh
|
54 | $ tape tests/**/*.js
|
55 | ```
|
56 |
|
57 | `tape`'s arguments are passed to the
|
58 | [`glob`](https://www.npmjs.com/package/glob) module. If you want `glob` to
|
59 | perform the expansion on a system where the shell performs such expansion, quote
|
60 | the arguments as necessary:
|
61 |
|
62 | ```sh
|
63 | $ tape 'tests/**/*.js'
|
64 | $ tape "tests/**/*.js"
|
65 | ```
|
66 |
|
67 | ## Preloading modules
|
68 |
|
69 | Additionally, it is possible to make `tape` load one or more modules before running any tests, by using the `-r` or `--require` flag. Here's an example that loads [babel-register](http://babeljs.io/docs/usage/require/) before running any tests, to allow for JIT compilation:
|
70 |
|
71 | ```sh
|
72 | $ tape -r babel-register tests/**/*.js
|
73 | ```
|
74 |
|
75 | Depending on the module you're loading, you may be able to parameterize it using environment variables or auxiliary files. Babel, for instance, will load options from [`.babelrc`](http://babeljs.io/docs/usage/babelrc/) at runtime.
|
76 |
|
77 | The `-r` flag behaves exactly like node's `require`, and uses the same module resolution algorithm. This means that if you need to load local modules, you have to prepend their path with `./` or `../` accordingly.
|
78 |
|
79 | For example:
|
80 |
|
81 | ```sh
|
82 | $ tape -r ./my/local/module tests/**/*.js
|
83 | ```
|
84 |
|
85 | Please note that all modules loaded using the `-r` flag will run *before* any tests, regardless of when they are specified. For example, `tape -r a b -r c` will actually load `a` and `c` *before* loading `b`, since they are flagged as required modules.
|
86 |
|
87 | # things that go well with tape
|
88 |
|
89 | `tape` maintains a fairly minimal core. Additional features are usually added by using another module alongside `tape`.
|
90 |
|
91 | ## pretty reporters
|
92 |
|
93 | The default TAP output is good for machines and humans that are robots.
|
94 |
|
95 | If you want a more colorful / pretty output there are lots of modules on npm
|
96 | that will output something pretty if you pipe TAP into them:
|
97 |
|
98 | - [tap-spec](https://github.com/scottcorgan/tap-spec)
|
99 | - [tap-dot](https://github.com/scottcorgan/tap-dot)
|
100 | - [faucet](https://github.com/substack/faucet)
|
101 | - [tap-bail](https://github.com/juliangruber/tap-bail)
|
102 | - [tap-browser-color](https://github.com/kirbysayshi/tap-browser-color)
|
103 | - [tap-json](https://github.com/gummesson/tap-json)
|
104 | - [tap-min](https://github.com/derhuerst/tap-min)
|
105 | - [tap-nyan](https://github.com/calvinmetcalf/tap-nyan)
|
106 | - [tap-pessimist](https://www.npmjs.org/package/tap-pessimist)
|
107 | - [tap-prettify](https://github.com/toolness/tap-prettify)
|
108 | - [colortape](https://github.com/shuhei/colortape)
|
109 | - [tap-xunit](https://github.com/aghassemi/tap-xunit)
|
110 | - [tap-difflet](https://github.com/namuol/tap-difflet)
|
111 | - [tape-dom](https://github.com/gritzko/tape-dom)
|
112 | - [tap-diff](https://github.com/axross/tap-diff)
|
113 | - [tap-notify](https://github.com/axross/tap-notify)
|
114 | - [tap-summary](https://github.com/zoubin/tap-summary)
|
115 | - [tap-markdown](https://github.com/Hypercubed/tap-markdown)
|
116 | - [tap-html](https://github.com/gabrielcsapo/tap-html)
|
117 | - [tap-react-browser](https://github.com/mcnuttandrew/tap-react-browser)
|
118 | - [tap-junit](https://github.com/dhershman1/tap-junit)
|
119 | - [tap-nyc](https://github.com/MegaArman/tap-nyc)
|
120 |
|
121 | To use them, try `node test/index.js | tap-spec` or pipe it into one
|
122 | of the modules of your choice!
|
123 |
|
124 | ## uncaught exceptions
|
125 |
|
126 | By default, uncaught exceptions in your tests will not be intercepted, and will cause `tape` to crash. If you find this behavior undesirable, use [`tape-catch`](https://github.com/michaelrhodes/tape-catch) to report any exceptions as TAP errors.
|
127 |
|
128 | ## other
|
129 |
|
130 | - CoffeeScript support with https://www.npmjs.com/package/coffeetape
|
131 | - Promise support with https://www.npmjs.com/package/blue-tape or https://www.npmjs.com/package/tape-promise
|
132 | - ES6 support with https://www.npmjs.com/package/babel-tape-runner or https://www.npmjs.com/package/buble-tape-runner
|
133 | - Different test syntax with https://github.com/pguth/flip-tape (warning: mutates String.prototype)
|
134 | - Electron test runner with https://github.com/tundrax/electron-tap
|
135 | - Concurrency support with https://github.com/imsnif/mixed-tape
|
136 |
|
137 | # methods
|
138 |
|
139 | The assertion methods in `tape` are heavily influenced or copied from the methods
|
140 | in [node-tap](https://github.com/isaacs/node-tap).
|
141 |
|
142 | ```js
|
143 | var test = require('tape')
|
144 | ```
|
145 |
|
146 | ## test([name], [opts], cb)
|
147 |
|
148 | Create a new test with an optional `name` string and optional `opts` object.
|
149 | `cb(t)` fires with the new test object `t` once all preceding tests have
|
150 | finished. Tests execute serially.
|
151 |
|
152 | Available `opts` options are:
|
153 | - opts.skip = true/false. See test.skip.
|
154 | - opts.timeout = 500. Set a timeout for the test, after which it will fail. See test.timeoutAfter.
|
155 | - opts.objectPrintDepth = 5. Configure max depth of expected / actual object printing. Environmental variable `NODE_TAPE_OBJECT_PRINT_DEPTH` can set the desired default depth for all tests; locally-set values will take precedence.
|
156 | - opts.todo = true/false. Test will be allowed to fail.
|
157 |
|
158 | If you forget to `t.plan()` out how many assertions you are going to run and you
|
159 | don't call `t.end()` explicitly, your test will hang.
|
160 |
|
161 | ## test.skip([name], [opts], cb)
|
162 |
|
163 | Generate a new test that will be skipped over.
|
164 |
|
165 | ## test.onFinish(fn)
|
166 |
|
167 | The onFinish hook will get invoked when ALL `tape` tests have finished
|
168 | right before `tape` is about to print the test summary.
|
169 |
|
170 | ## test.onFailure(fn)
|
171 |
|
172 | The onFailure hook will get invoked whenever any `tape` tests has failed.
|
173 |
|
174 | ## t.plan(n)
|
175 |
|
176 | Declare that `n` assertions should be run. `t.end()` will be called
|
177 | automatically after the `n`th assertion. If there are any more assertions after
|
178 | the `n`th, or after `t.end()` is called, they will generate errors.
|
179 |
|
180 | ## t.end(err)
|
181 |
|
182 | Declare the end of a test explicitly. If `err` is passed in `t.end` will assert
|
183 | that it is falsey.
|
184 |
|
185 | ## t.fail(msg)
|
186 |
|
187 | Generate a failing assertion with a message `msg`.
|
188 |
|
189 | ## t.pass(msg)
|
190 |
|
191 | Generate a passing assertion with a message `msg`.
|
192 |
|
193 | ## t.timeoutAfter(ms)
|
194 |
|
195 | Automatically timeout the test after X ms.
|
196 |
|
197 | ## t.skip(msg)
|
198 |
|
199 | Generate an assertion that will be skipped over.
|
200 |
|
201 | ## t.ok(value, msg)
|
202 |
|
203 | Assert that `value` is truthy with an optional description of the assertion `msg`.
|
204 |
|
205 | Aliases: `t.true()`, `t.assert()`
|
206 |
|
207 | ## t.notOk(value, msg)
|
208 |
|
209 | Assert that `value` is falsy with an optional description of the assertion `msg`.
|
210 |
|
211 | Aliases: `t.false()`, `t.notok()`
|
212 |
|
213 | ## t.error(err, msg)
|
214 |
|
215 | Assert that `err` is falsy. If `err` is non-falsy, use its `err.message` as the
|
216 | description message.
|
217 |
|
218 | Aliases: `t.ifError()`, `t.ifErr()`, `t.iferror()`
|
219 |
|
220 | ## t.equal(actual, expected, msg)
|
221 |
|
222 | Assert that `actual === expected` with an optional description of the assertion `msg`.
|
223 |
|
224 | Aliases: `t.equals()`, `t.isEqual()`, `t.is()`, `t.strictEqual()`,
|
225 | `t.strictEquals()`
|
226 |
|
227 | ## t.notEqual(actual, expected, msg)
|
228 |
|
229 | Assert that `actual !== expected` with an optional description of the assertion `msg`.
|
230 |
|
231 | Aliases: `t.notEquals()`, `t.notStrictEqual()`, `t.notStrictEquals()`,
|
232 | `t.isNotEqual()`, `t.isNot()`, `t.not()`, `t.doesNotEqual()`, `t.isInequal()`
|
233 |
|
234 | ## t.deepEqual(actual, expected, msg)
|
235 |
|
236 | Assert that `actual` and `expected` have the same structure and nested values using
|
237 | [node's deepEqual() algorithm](https://github.com/substack/node-deep-equal)
|
238 | with strict comparisons (`===`) on leaf nodes and an optional description of the assertion `msg`.
|
239 |
|
240 | Aliases: `t.deepEquals()`, `t.isEquivalent()`, `t.same()`
|
241 |
|
242 | ## t.notDeepEqual(actual, expected, msg)
|
243 |
|
244 | Assert that `actual` and `expected` do not have the same structure and nested values using
|
245 | [node's deepEqual() algorithm](https://github.com/substack/node-deep-equal)
|
246 | with strict comparisons (`===`) on leaf nodes and an optional description of the assertion `msg`.
|
247 |
|
248 | Aliases: `t.notDeepEquals`, `t.notEquivalent()`, `t.notDeeply()`, `t.notSame()`,
|
249 | `t.isNotDeepEqual()`, `t.isNotDeeply()`, `t.isNotEquivalent()`,
|
250 | `t.isInequivalent()`
|
251 |
|
252 | ## t.deepLooseEqual(actual, expected, msg)
|
253 |
|
254 | Assert that `actual` and `expected` have the same structure and nested values using
|
255 | [node's deepEqual() algorithm](https://github.com/substack/node-deep-equal)
|
256 | with loose comparisons (`==`) on leaf nodes and an optional description of the assertion `msg`.
|
257 |
|
258 | Aliases: `t.looseEqual()`, `t.looseEquals()`
|
259 |
|
260 | ## t.notDeepLooseEqual(actual, expected, msg)
|
261 |
|
262 | Assert that `actual` and `expected` do not have the same structure and nested values using
|
263 | [node's deepEqual() algorithm](https://github.com/substack/node-deep-equal)
|
264 | with loose comparisons (`==`) on leaf nodes and an optional description of the assertion `msg`.
|
265 |
|
266 | Aliases: `t.notLooseEqual()`, `t.notLooseEquals()`
|
267 |
|
268 | ## t.throws(fn, expected, msg)
|
269 |
|
270 | Assert that the function call `fn()` throws an exception. `expected`, if present, must be a `RegExp` or `Function`. The `RegExp` matches the string representation of the exception, as generated by `err.toString()`. The `Function` is the exception thrown (e.g. `Error`). `msg` is an optional description of the assertion.
|
271 |
|
272 | ## t.doesNotThrow(fn, expected, msg)
|
273 |
|
274 | Assert that the function call `fn()` does not throw an exception. `expected`, if present, limits what should not be thrown. For example, set `expected` to `/user/` to fail the test only if the string representation of the exception contains the word `user`. Any other exception would pass the test. If `expected` is omitted, any exception will fail the test. `msg` is an optional description of the assertion.
|
275 |
|
276 | ## t.test(name, [opts], cb)
|
277 |
|
278 | Create a subtest with a new test handle `st` from `cb(st)` inside the current
|
279 | test `t`. `cb(st)` will only fire when `t` finishes. Additional tests queued up
|
280 | after `t` will not be run until all subtests finish.
|
281 |
|
282 | You may pass the same options that [`test()`](#testname-opts-cb) accepts.
|
283 |
|
284 | ## t.comment(message)
|
285 |
|
286 | Print a message without breaking the tap output. (Useful when using e.g. `tap-colorize` where output is buffered & `console.log` will print in incorrect order vis-a-vis tap output.)
|
287 |
|
288 | ## t.match(string, regexp, message)
|
289 |
|
290 | Assert that `string` matches the RegExp `regexp`. Will throw (not just fail) when the first two arguments are the wrong type.
|
291 |
|
292 | ## t.doesNotMatch(string, regexp, message)
|
293 |
|
294 | Assert that `string` does not match the RegExp `regexp`. Will throw (not just fail) when the first two arguments are the wrong type.
|
295 |
|
296 | ## var htest = test.createHarness()
|
297 |
|
298 | Create a new test harness instance, which is a function like `test()`, but with
|
299 | a new pending stack and test state.
|
300 |
|
301 | By default the TAP output goes to `console.log()`. You can pipe the output to
|
302 | someplace else if you `htest.createStream().pipe()` to a destination stream on
|
303 | the first tick.
|
304 |
|
305 | ## test.only([name], [opts], cb)
|
306 |
|
307 | Like `test([name], [opts], cb)` except if you use `.only` this is the only test case
|
308 | that will run for the entire process, all other test cases using `tape` will
|
309 | be ignored.
|
310 |
|
311 | ## var stream = test.createStream(opts)
|
312 |
|
313 | Create a stream of output, bypassing the default output stream that writes
|
314 | messages to `console.log()`. By default `stream` will be a text stream of TAP
|
315 | output, but you can get an object stream instead by setting `opts.objectMode` to
|
316 | `true`.
|
317 |
|
318 | ### tap stream reporter
|
319 |
|
320 | You can create your own custom test reporter using this `createStream()` api:
|
321 |
|
322 | ``` js
|
323 | var test = require('tape');
|
324 | var path = require('path');
|
325 |
|
326 | test.createStream().pipe(process.stdout);
|
327 |
|
328 | process.argv.slice(2).forEach(function (file) {
|
329 | require(path.resolve(file));
|
330 | });
|
331 | ```
|
332 |
|
333 | You could substitute `process.stdout` for whatever other output stream you want,
|
334 | like a network connection or a file.
|
335 |
|
336 | Pass in test files to run as arguments:
|
337 |
|
338 | ```sh
|
339 | $ node tap.js test/x.js test/y.js
|
340 | TAP version 13
|
341 | # (anonymous)
|
342 | not ok 1 should be equal
|
343 | ---
|
344 | operator: equal
|
345 | expected: "boop"
|
346 | actual: "beep"
|
347 | ...
|
348 | # (anonymous)
|
349 | ok 2 should be equal
|
350 | ok 3 (unnamed assert)
|
351 | # wheee
|
352 | ok 4 (unnamed assert)
|
353 |
|
354 | 1..4
|
355 | # tests 4
|
356 | # pass 3
|
357 | # fail 1
|
358 | ```
|
359 |
|
360 | ### object stream reporter
|
361 |
|
362 | Here's how you can render an object stream instead of TAP:
|
363 |
|
364 | ``` js
|
365 | var test = require('tape');
|
366 | var path = require('path');
|
367 |
|
368 | test.createStream({ objectMode: true }).on('data', function (row) {
|
369 | console.log(JSON.stringify(row))
|
370 | });
|
371 |
|
372 | process.argv.slice(2).forEach(function (file) {
|
373 | require(path.resolve(file));
|
374 | });
|
375 | ```
|
376 |
|
377 | The output for this runner is:
|
378 |
|
379 | ```sh
|
380 | $ node object.js test/x.js test/y.js
|
381 | {"type":"test","name":"(anonymous)","id":0}
|
382 | {"id":0,"ok":false,"name":"should be equal","operator":"equal","actual":"beep","expected":"boop","error":{},"test":0,"type":"assert"}
|
383 | {"type":"end","test":0}
|
384 | {"type":"test","name":"(anonymous)","id":1}
|
385 | {"id":0,"ok":true,"name":"should be equal","operator":"equal","actual":2,"expected":2,"test":1,"type":"assert"}
|
386 | {"id":1,"ok":true,"name":"(unnamed assert)","operator":"ok","actual":true,"expected":true,"test":1,"type":"assert"}
|
387 | {"type":"end","test":1}
|
388 | {"type":"test","name":"wheee","id":2}
|
389 | {"id":0,"ok":true,"name":"(unnamed assert)","operator":"ok","actual":true,"expected":true,"test":2,"type":"assert"}
|
390 | {"type":"end","test":2}
|
391 | ```
|
392 |
|
393 | # install
|
394 |
|
395 | With [npm](https://npmjs.org) do:
|
396 |
|
397 | ```sh
|
398 | npm install tape --save-dev
|
399 | ```
|
400 |
|
401 | # license
|
402 |
|
403 | MIT
|