UNPKG

13.9 kBMarkdownView Raw
1# tape
2
3tap-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
14var test = require('tape');
15
16test('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
30TAP version 13
31# timing test
32ok 1 should be equal
33not ok 2 should be equal
34 ---
35 operator: equal
36 expected: 100
37 actual: 107
38 ...
39
401..2
41# tests 2
42# pass 1
43# fail 1
44```
45
46# usage
47
48You always need to `require('tape')` in test files. You can run the tests by
49usual node means (`require('test-file.js')` or `node test-file.js`). You can
50also run tests using the `tape` binary to utilize globbing, on Windows for
51example:
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
59perform the expansion on a system where the shell performs such expansion, quote
60the arguments as necessary:
61
62```sh
63$ tape 'tests/**/*.js'
64$ tape "tests/**/*.js"
65```
66
67## Preloading modules
68
69Additionally, 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
75Depending 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
77The `-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
79For example:
80
81```sh
82$ tape -r ./my/local/module tests/**/*.js
83```
84
85Please 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
93The default TAP output is good for machines and humans that are robots.
94
95If you want a more colorful / pretty output there are lots of modules on npm
96that 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
121To use them, try `node test/index.js | tap-spec` or pipe it into one
122of the modules of your choice!
123
124## uncaught exceptions
125
126By 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
139The assertion methods in `tape` are heavily influenced or copied from the methods
140in [node-tap](https://github.com/isaacs/node-tap).
141
142```js
143var test = require('tape')
144```
145
146## test([name], [opts], cb)
147
148Create 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
150finished. Tests execute serially.
151
152Available `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
158If you forget to `t.plan()` out how many assertions you are going to run and you
159don't call `t.end()` explicitly, your test will hang.
160
161## test.skip([name], [opts], cb)
162
163Generate a new test that will be skipped over.
164
165## test.onFinish(fn)
166
167The onFinish hook will get invoked when ALL `tape` tests have finished
168right before `tape` is about to print the test summary.
169
170## test.onFailure(fn)
171
172The onFailure hook will get invoked whenever any `tape` tests has failed.
173
174## t.plan(n)
175
176Declare that `n` assertions should be run. `t.end()` will be called
177automatically after the `n`th assertion. If there are any more assertions after
178the `n`th, or after `t.end()` is called, they will generate errors.
179
180## t.end(err)
181
182Declare the end of a test explicitly. If `err` is passed in `t.end` will assert
183that it is falsey.
184
185## t.fail(msg)
186
187Generate a failing assertion with a message `msg`.
188
189## t.pass(msg)
190
191Generate a passing assertion with a message `msg`.
192
193## t.timeoutAfter(ms)
194
195Automatically timeout the test after X ms.
196
197## t.skip(msg)
198
199Generate an assertion that will be skipped over.
200
201## t.ok(value, msg)
202
203Assert that `value` is truthy with an optional description of the assertion `msg`.
204
205Aliases: `t.true()`, `t.assert()`
206
207## t.notOk(value, msg)
208
209Assert that `value` is falsy with an optional description of the assertion `msg`.
210
211Aliases: `t.false()`, `t.notok()`
212
213## t.error(err, msg)
214
215Assert that `err` is falsy. If `err` is non-falsy, use its `err.message` as the
216description message.
217
218Aliases: `t.ifError()`, `t.ifErr()`, `t.iferror()`
219
220## t.equal(actual, expected, msg)
221
222Assert that `actual === expected` with an optional description of the assertion `msg`.
223
224Aliases: `t.equals()`, `t.isEqual()`, `t.is()`, `t.strictEqual()`,
225`t.strictEquals()`
226
227## t.notEqual(actual, expected, msg)
228
229Assert that `actual !== expected` with an optional description of the assertion `msg`.
230
231Aliases: `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
236Assert that `actual` and `expected` have the same structure and nested values using
237[node's deepEqual() algorithm](https://github.com/substack/node-deep-equal)
238with strict comparisons (`===`) on leaf nodes and an optional description of the assertion `msg`.
239
240Aliases: `t.deepEquals()`, `t.isEquivalent()`, `t.same()`
241
242## t.notDeepEqual(actual, expected, msg)
243
244Assert 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)
246with strict comparisons (`===`) on leaf nodes and an optional description of the assertion `msg`.
247
248Aliases: `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
254Assert that `actual` and `expected` have the same structure and nested values using
255[node's deepEqual() algorithm](https://github.com/substack/node-deep-equal)
256with loose comparisons (`==`) on leaf nodes and an optional description of the assertion `msg`.
257
258Aliases: `t.looseEqual()`, `t.looseEquals()`
259
260## t.notDeepLooseEqual(actual, expected, msg)
261
262Assert 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)
264with loose comparisons (`==`) on leaf nodes and an optional description of the assertion `msg`.
265
266Aliases: `t.notLooseEqual()`, `t.notLooseEquals()`
267
268## t.throws(fn, expected, msg)
269
270Assert 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
274Assert 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
278Create a subtest with a new test handle `st` from `cb(st)` inside the current
279test `t`. `cb(st)` will only fire when `t` finishes. Additional tests queued up
280after `t` will not be run until all subtests finish.
281
282You may pass the same options that [`test()`](#testname-opts-cb) accepts.
283
284## t.comment(message)
285
286Print 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
290Assert 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
294Assert 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
298Create a new test harness instance, which is a function like `test()`, but with
299a new pending stack and test state.
300
301By default the TAP output goes to `console.log()`. You can pipe the output to
302someplace else if you `htest.createStream().pipe()` to a destination stream on
303the first tick.
304
305## test.only([name], [opts], cb)
306
307Like `test([name], [opts], cb)` except if you use `.only` this is the only test case
308that will run for the entire process, all other test cases using `tape` will
309be ignored.
310
311## var stream = test.createStream(opts)
312
313Create a stream of output, bypassing the default output stream that writes
314messages to `console.log()`. By default `stream` will be a text stream of TAP
315output, but you can get an object stream instead by setting `opts.objectMode` to
316`true`.
317
318### tap stream reporter
319
320You can create your own custom test reporter using this `createStream()` api:
321
322``` js
323var test = require('tape');
324var path = require('path');
325
326test.createStream().pipe(process.stdout);
327
328process.argv.slice(2).forEach(function (file) {
329 require(path.resolve(file));
330});
331```
332
333You could substitute `process.stdout` for whatever other output stream you want,
334like a network connection or a file.
335
336Pass in test files to run as arguments:
337
338```sh
339$ node tap.js test/x.js test/y.js
340TAP version 13
341# (anonymous)
342not ok 1 should be equal
343 ---
344 operator: equal
345 expected: "boop"
346 actual: "beep"
347 ...
348# (anonymous)
349ok 2 should be equal
350ok 3 (unnamed assert)
351# wheee
352ok 4 (unnamed assert)
353
3541..4
355# tests 4
356# pass 3
357# fail 1
358```
359
360### object stream reporter
361
362Here's how you can render an object stream instead of TAP:
363
364``` js
365var test = require('tape');
366var path = require('path');
367
368test.createStream({ objectMode: true }).on('data', function (row) {
369 console.log(JSON.stringify(row))
370});
371
372process.argv.slice(2).forEach(function (file) {
373 require(path.resolve(file));
374});
375```
376
377The 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
395With [npm](https://npmjs.org) do:
396
397```sh
398npm install tape --save-dev
399```
400
401# license
402
403MIT