UNPKG

13.5 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
120To use them, try `node test/index.js | tap-spec` or pipe it into one
121of the modules of your choice!
122
123## uncaught exceptions
124
125By 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.
126
127## other
128
129- CoffeeScript support with https://www.npmjs.com/package/coffeetape
130- Promise support with https://www.npmjs.com/package/blue-tape or https://www.npmjs.com/package/tape-promise
131- ES6 support with https://www.npmjs.com/package/babel-tape-runner or https://www.npmjs.com/package/buble-tape-runner
132- Different test syntax with https://github.com/pguth/flip-tape (warning: mutates String.prototype)
133- Electron test runner with https://github.com/tundrax/electron-tap
134- Concurrency support with https://github.com/imsnif/mixed-tape
135
136# methods
137
138The assertion methods in `tape` are heavily influenced or copied from the methods
139in [node-tap](https://github.com/isaacs/node-tap).
140
141```js
142var test = require('tape')
143```
144
145## test([name], [opts], cb)
146
147Create a new test with an optional `name` string and optional `opts` object.
148`cb(t)` fires with the new test object `t` once all preceding tests have
149finished. Tests execute serially.
150
151Available `opts` options are:
152- opts.skip = true/false. See test.skip.
153- opts.timeout = 500. Set a timeout for the test, after which it will fail. See test.timeoutAfter.
154- 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.
155- opts.todo = true/false. Test will be allowed to fail.
156
157If you forget to `t.plan()` out how many assertions you are going to run and you
158don't call `t.end()` explicitly, your test will hang.
159
160## test.skip([name], [opts], cb)
161
162Generate a new test that will be skipped over.
163
164## test.onFinish(fn)
165
166The onFinish hook will get invoked when ALL `tape` tests have finished
167right before `tape` is about to print the test summary.
168
169## test.onFailure(fn)
170
171The onFailure hook will get invoked whenever any `tape` tests has failed.
172
173## t.plan(n)
174
175Declare that `n` assertions should be run. `t.end()` will be called
176automatically after the `n`th assertion. If there are any more assertions after
177the `n`th, or after `t.end()` is called, they will generate errors.
178
179## t.end(err)
180
181Declare the end of a test explicitly. If `err` is passed in `t.end` will assert
182that it is falsey.
183
184## t.fail(msg)
185
186Generate a failing assertion with a message `msg`.
187
188## t.pass(msg)
189
190Generate a passing assertion with a message `msg`.
191
192## t.timeoutAfter(ms)
193
194Automatically timeout the test after X ms.
195
196## t.skip(msg)
197
198Generate an assertion that will be skipped over.
199
200## t.ok(value, msg)
201
202Assert that `value` is truthy with an optional description of the assertion `msg`.
203
204Aliases: `t.true()`, `t.assert()`
205
206## t.notOk(value, msg)
207
208Assert that `value` is falsy with an optional description of the assertion `msg`.
209
210Aliases: `t.false()`, `t.notok()`
211
212## t.error(err, msg)
213
214Assert that `err` is falsy. If `err` is non-falsy, use its `err.message` as the
215description message.
216
217Aliases: `t.ifError()`, `t.ifErr()`, `t.iferror()`
218
219## t.equal(actual, expected, msg)
220
221Assert that `actual === expected` with an optional description of the assertion `msg`.
222
223Aliases: `t.equals()`, `t.isEqual()`, `t.is()`, `t.strictEqual()`,
224`t.strictEquals()`
225
226## t.notEqual(actual, expected, msg)
227
228Assert that `actual !== expected` with an optional description of the assertion `msg`.
229
230Aliases: `t.notEquals()`, `t.notStrictEqual()`, `t.notStrictEquals()`,
231`t.isNotEqual()`, `t.isNot()`, `t.not()`, `t.doesNotEqual()`, `t.isInequal()`
232
233## t.deepEqual(actual, expected, msg)
234
235Assert that `actual` and `expected` have the same structure and nested values using
236[node's deepEqual() algorithm](https://github.com/substack/node-deep-equal)
237with strict comparisons (`===`) on leaf nodes and an optional description of the assertion `msg`.
238
239Aliases: `t.deepEquals()`, `t.isEquivalent()`, `t.same()`
240
241## t.notDeepEqual(actual, expected, msg)
242
243Assert that `actual` and `expected` do not have the same structure and nested values using
244[node's deepEqual() algorithm](https://github.com/substack/node-deep-equal)
245with strict comparisons (`===`) on leaf nodes and an optional description of the assertion `msg`.
246
247Aliases: `t.notDeepEquals`, `t.notEquivalent()`, `t.notDeeply()`, `t.notSame()`,
248`t.isNotDeepEqual()`, `t.isNotDeeply()`, `t.isNotEquivalent()`,
249`t.isInequivalent()`
250
251## t.deepLooseEqual(actual, expected, msg)
252
253Assert that `actual` and `expected` have the same structure and nested values using
254[node's deepEqual() algorithm](https://github.com/substack/node-deep-equal)
255with loose comparisons (`==`) on leaf nodes and an optional description of the assertion `msg`.
256
257Aliases: `t.looseEqual()`, `t.looseEquals()`
258
259## t.notDeepLooseEqual(actual, expected, msg)
260
261Assert that `actual` and `expected` do not have the same structure and nested values using
262[node's deepEqual() algorithm](https://github.com/substack/node-deep-equal)
263with loose comparisons (`==`) on leaf nodes and an optional description of the assertion `msg`.
264
265Aliases: `t.notLooseEqual()`, `t.notLooseEquals()`
266
267## t.throws(fn, expected, msg)
268
269Assert 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.
270
271## t.doesNotThrow(fn, expected, msg)
272
273Assert 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.
274
275## t.test(name, [opts], cb)
276
277Create a subtest with a new test handle `st` from `cb(st)` inside the current
278test `t`. `cb(st)` will only fire when `t` finishes. Additional tests queued up
279after `t` will not be run until all subtests finish.
280
281You may pass the same options that [`test()`](#testname-opts-cb) accepts.
282
283## t.comment(message)
284
285Print 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.)
286
287## var htest = test.createHarness()
288
289Create a new test harness instance, which is a function like `test()`, but with
290a new pending stack and test state.
291
292By default the TAP output goes to `console.log()`. You can pipe the output to
293someplace else if you `htest.createStream().pipe()` to a destination stream on
294the first tick.
295
296## test.only([name], [opts], cb)
297
298Like `test([name], [opts], cb)` except if you use `.only` this is the only test case
299that will run for the entire process, all other test cases using `tape` will
300be ignored.
301
302## var stream = test.createStream(opts)
303
304Create a stream of output, bypassing the default output stream that writes
305messages to `console.log()`. By default `stream` will be a text stream of TAP
306output, but you can get an object stream instead by setting `opts.objectMode` to
307`true`.
308
309### tap stream reporter
310
311You can create your own custom test reporter using this `createStream()` api:
312
313``` js
314var test = require('tape');
315var path = require('path');
316
317test.createStream().pipe(process.stdout);
318
319process.argv.slice(2).forEach(function (file) {
320 require(path.resolve(file));
321});
322```
323
324You could substitute `process.stdout` for whatever other output stream you want,
325like a network connection or a file.
326
327Pass in test files to run as arguments:
328
329```sh
330$ node tap.js test/x.js test/y.js
331TAP version 13
332# (anonymous)
333not ok 1 should be equal
334 ---
335 operator: equal
336 expected: "boop"
337 actual: "beep"
338 ...
339# (anonymous)
340ok 2 should be equal
341ok 3 (unnamed assert)
342# wheee
343ok 4 (unnamed assert)
344
3451..4
346# tests 4
347# pass 3
348# fail 1
349```
350
351### object stream reporter
352
353Here's how you can render an object stream instead of TAP:
354
355``` js
356var test = require('tape');
357var path = require('path');
358
359test.createStream({ objectMode: true }).on('data', function (row) {
360 console.log(JSON.stringify(row))
361});
362
363process.argv.slice(2).forEach(function (file) {
364 require(path.resolve(file));
365});
366```
367
368The output for this runner is:
369
370```sh
371$ node object.js test/x.js test/y.js
372{"type":"test","name":"(anonymous)","id":0}
373{"id":0,"ok":false,"name":"should be equal","operator":"equal","actual":"beep","expected":"boop","error":{},"test":0,"type":"assert"}
374{"type":"end","test":0}
375{"type":"test","name":"(anonymous)","id":1}
376{"id":0,"ok":true,"name":"should be equal","operator":"equal","actual":2,"expected":2,"test":1,"type":"assert"}
377{"id":1,"ok":true,"name":"(unnamed assert)","operator":"ok","actual":true,"expected":true,"test":1,"type":"assert"}
378{"type":"end","test":1}
379{"type":"test","name":"wheee","id":2}
380{"id":0,"ok":true,"name":"(unnamed assert)","operator":"ok","actual":true,"expected":true,"test":2,"type":"assert"}
381{"type":"end","test":2}
382```
383
384# install
385
386With [npm](https://npmjs.org) do:
387
388```sh
389npm install tape --save-dev
390```
391
392# license
393
394MIT