File: index.md

package info (click to toggle)
node-tap 12.0.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,916 kB
  • sloc: makefile: 84; sh: 75
file content (334 lines) | stat: -rw-r--r-- 10,167 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
---
layout: layout
title: Test Anything Protocol
---

# Test Anything Protocol

`tap` is a JavaScript implementation of the [Test Anything
Protocol](http://testanything.org/).  TAP is a highly parseable,
human-readable, loosely-specified format for reporting test results.
It rose to popularity in the Perl community, with CPAN's
[Test](http://search.cpan.org/~exodist/Test-Simple-1.302075/) family.

This protocol is how child test processes communicate their success or
failure with their parent process.

Most of the time, you'll view `tap`'s output using one of the
[reporting options](/reporting/).  However, occasionally it's useful
to view the raw output for a variety of reasons.  For example, you may
wish to run a test file directly, or store TAP output for later
analysis.

Most of the time, you'll generate TAP by using the functions in
`tap`'s [API](/api/).  But if you have a different kind of program
that you would like to consume with `tap`'s test runner, you can just
print TAP to standard output in any way you please.

This page describes the TAP format that `tap` supports.

## Version

TAP streams generally start with `TAP version 13`.  This isn't
strictly required by `tap`, but since some other TAP implementations
_do_ require it, `tap` always outputs `TAP version 13` as the line.

There's no way to set the version in `tap`.

Since some TAP consumers get upset about an indented version
declaration, the version in Subtest streams is always stripped out.

## Plan

Every TAP stream must contain a "plan" either at the beginning or the
end of the set of test points.  The plan lists the range of test point
IDs that are expected in the TAP stream.  It can also optionally
contain a comment prefixed by a `#`.

A plan of `1..0` indicates that the test file is completely skipped,
and no tests are expected.

Examples:

```tap
1..9
1..0 # skip this test file, no tests here
5..8 # only run tests 5 through 8
```

When consuming a plan, `tap` will accept any of these.  However, when
generating test output with `tap`, you may only set the _end_ of the
plan to indicate the number of tests you expect to run (or expect to
have run).

Plans cannot be set in the middle of a test set.  That is, they have
to come before all the test points, or after all of them.

To set a plan in `tap` explicitly, use the `t.plan(n, [comment])`
function.  If you end a test by returning a [promise](/promises/) or
calling `t.end()`, then a plan will be automatically generated at the
end of the stream.

## Test Point

Sometimes called an "assert" or "test line", this is the core of the
TAP format.  A test point consists of 4 things:

1. Either `ok` or `not ok`.  This is required.  It specifies whether
   the test point is a pass or a fail.
2. A optional numeric identifier.  This is a check to ensure that test
   points are correctly ordered, and that the output is reasonable.
   `tap` does not let you set this number explicitly.  It assigns test
   point IDs based on its own internal counter.
3. An optional message, which may be prefixed by a `-` character.
4. A directive, prefixed with a `#` character.  (See below)

After a test point, there can be some YAML diagnostics, and
potentially also a buffered subtest.

```tap
1..2
ok
not ok 2 - that last test point was pretty bare
  ---
  but: this one
  has:
    - lots
    - of
    - stuff
  ...
```

The most common way to generate a test point in `tap` is to use one of
the [assertion methods](/asserts/).  Test points are also generated
for [subtests](/subtests/), and to communicate failures for unfinished
tests, exceeding a plan count, and anything else that might go wrong.

## Directives

A directive is similar to a comment, but communicates some information
about a test point.

A test point can be marked as `todo` to indicate that it is going to
be implemented later, or `skip` to indicate that the test should not
be performed in the given context.  `todo` and `skip` are

A test point associated with a
Subtest can also have a `# time=...` directive indicating how long the
subtest took to run.

Example:

```tap
not ok 1 - this will pass some day # todo
ok 2 - unix stuff # SKIP do not run on Windows
# Subtest: child test
    1..1
    ok
ok 3 - child test # time=12ms
1..3
```

In this case, we see a test that failed, but that's expected, because
it hasn't been implemented yet.  Then, test point #2, we're skipping
because we're on Windows.  Lastly, there's a child test stream that
took 12ms to complete.

Overall, a passing test stream :)

To set a `todo` or `skip` directive, pass `{ todo: reason }` or
`{skip: reason}` in either an assert or subtest method.  If you don't
wish to provide a reason, you can pass `{todo: true}` or `{skip:
true}`.  You can also mark subtests as `todo` by omitting the callback
function.

## YAML Diagnostics

Diagnostics can be used to provide additional details about a test
point.  They are a YAML object indented by 2 spaces, starting with
`---` and ending with `...`.  YAML diagnostics are associated with the
preceeding test point.

```tap
TAP version 13

ok 1 - everything is ok, just very communicative
  ---
  yaml: is
  so: true
  to:
    - every
    - vector
  ...

not ok 2 - failing, gonna tell you why
  ---
  at:
    file: foo.js
    line: 13
    column: 4
  message: This is not ok
  thrown: true
  ...

1..2
```

In `tap`, diagnostics are printed by default with failing tests, but
not with passing tests.  You can change this default by setting
`TAP_DIAG=0` in the environment to make it not print diagnostics with
failing tests or by setting `TAP_DIAG=1` to make it print diagnostics
with passing tests by default.  Setting `{ diagnostic: true }` in a
test point options object will always print diagnostics.  Setting `{
diagnostic: false }` will always omit diagnostics.

## Subtests

A [subtest](/subtests/) is an indented TAP stream that is a child of
the current set of tests.  It can be used to group test points
together, consume the output of a TAP-producing child process, or run
tests asynchronously.

"Unbuffered" subtests start with a `# Subtest: <name>` comment,
followed by the child TAP stream indented by 4 spaces, and finished
with a test point that indicates the passing status of the stream as a
whole.

"Buffered" subtest start with a test point indicating the status of
the group, and the indented child stream is wrapped in `{}` braces.
It's called "buffered" because the entire child stream has to be
parsed before the summary test point can be generated.

The summary test point ensures that TAP consumers that ignore indented
lines will at least report on the passing status based on the summary.

```tap
1..2
# Subtest: not buffered
    ok 1 - each line just printed as it comes
    ok 2 - no time to wait!
    1..2
ok 1 - not buffered

ok 2 - buffered {
    1..3
    ok 1 - this test is buffered
    ok 2 - so all the test points had to be parsed
    ok 3 - before success could be reported
}
```

Directives on buffered subtests can go either before or after the `{`
character.  When a buffered subtest has diagnostics, the `{` goes on
the line by itself after the yaml block.

```tap
1..2
ok 1 - usually would not even run this # TODO {
    ok 1 - but here we are anyway
    ok 2 - todo'ing away
    1..2
}
ok 2 - a very diagnostic subtest # time=33ms
  ---
  this: is fine
  i: am ok with the way things are proceeding
  ...
{
    1..1
    ok 1 - whatever
}
```

The most common way to run subtests is via `t.test(...)`.  See
[Subtests](/subtests/) for more info.

## Pragma

Pragmas are a way to set arbitrary switches on the parser.

The only switch that is treated specially is `strict`.  When in strict
mode, any non-TAP data is treated as an error.

```tap
TAP version 13
pragma +strict
ok 1 - this is very strict
so this line here is an error
not ok 2 - that line failed
pragma -strict
but this line here is fine
ok 3 - because garbage data is allowed in non-strict mode
1..3
```

Set pragms in `tap` by doing `t.pragma({ keys: values, ... })`.
The object can contain any number of keys, but only `strict` has any
meaning to `tap` itself.

## Bail out!

Sometimes a set of tests hits a state where there's no point
continuing.  Or, perhaps you just wish to stop on the first failure to
work on errors one at a time with a faster turnover.

In this case, TAP allows a "bail out".  A bail out is much more
extreme than a test point failure.  It means that everything should
come to a halt, all the way up to the highest level test harness.
Nothing should come after a bailout.  Any plan is dropped, test points
ignored, and so on.

```tap
TAP version 13
# Subtest: child
    # Subtest: grandchild
        1..2999
        ok 1 - here we go
        Bail out! Nope.
Bail out! Nope.
```

Bail outs in buffered tests should still print the closing `}` braces,
but no other output.

```tap
TAP version 13
not ok 1 - child {
    not ok 2 - grandchild {
        1..2999
        ok 1 - here we go
        Bail out! Nope.
    }
}
Bail out! Nope.
```

You can generate a bailout explicitly by doing `t.bailout(reason)`.
You can also have `tap` bail out on any test failure by setting
`TAP_BAIL=1` in the environment, or by setting `{ bail: true }` in a
child test options, or by running with the `tap` [command-line
interface](/cli/) and passing the `--bail` flag.

## Comments and Other Stuff

Anything that starts with a `#` and is not a directive or subtest
prefix is treated as a comment, and ignored.

Anything that isn't parseable as one of the above types of lines is
considered "extra" non-TAP data.  In strict mode, extra output is an
error.  In non-strict mode, it's ignored.

The `tap` runner ignores comments.  Non-TAP data is passed through the
reporting engine and printed to the top-level process standard output.
This means that `console.log('foo')` will make its way to the top
level, instead of being swallowed by a reporter.

You can output comments by doing `t.comment('foo')`.  This function
takes any arguments that can be passed to `console.log()`.  For
example, `t.comment('number %d and\nobj =', 1, { foo: 'bar' })` would
output:

```tap
# number 1 and
# obj = { foo: 'bar' }
```