Skip to content

Commit fcf70aa

Browse files
committed
Add support for disabling autoend
Discussion: https://twitter.com/izs/status/958397259418238976 CC: @watson
1 parent 94be0a7 commit fcf70aa

File tree

7 files changed

+94
-5
lines changed

7 files changed

+94
-5
lines changed

docs/api/index.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,18 @@ slight modifications.
3434
the main package export.
3535
3. The test ends automatically when `process.on('exit')` fires, so
3636
there is no need to call `tap.end()` explicitly.
37-
4. Adding a `tearDown` function triggers `autoend` behavior.
37+
4. Adding a `tearDown` function triggers `autoend` behavior, unless
38+
`autoend` was explicitly set to `false`.
39+
3840
Otherwise, the `end` would potentially never arrive, if for example
3941
`tearDown` is used to close a server or cancel some long-running
4042
process, because `process.on('exit')` would never fire of its own
4143
accord.
4244

45+
If you disable `autoend`, and _also_ use a `teardown()` function on
46+
the main tap instance, you need to either set a `t.plan(n)` or
47+
explicitly call `t.end()` at some point.
48+
4349
## tap.Test
4450

4551
The `Test` class is the main thing you'll be touching when you use
@@ -229,3 +235,13 @@ Generally, you never need to worry about this directly.
229235
However, this method can also be called explicitly in cases where an
230236
error would be handled by something else (for example, a default
231237
[Promise](/promises/) `.catch(er)` method.)
238+
239+
### t.autoend(value)
240+
241+
If `value` is boolean `false`, then it will disable the `autoend`
242+
behavior. If `value` is anything other than `false`, then it will
243+
cause the test to automatically end when nothing is pending.
244+
245+
Note that this is triggered by default on the root `tap` instance when
246+
a `teardown()` function is set, unless `autoend` was explicitly
247+
disabled.

lib/tap.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ class TAP extends Test {
8484
// Root test runner doesn't have the 'teardown' event, because it
8585
// isn't hooked into any parent Test as a harness.
8686
teardown (fn) {
87-
this.autoend()
87+
if (this.options.autoend !== false)
88+
this.autoend(true)
8889
return Test.prototype.teardown.apply(this, arguments)
8990
}
9091
tearDown (fn) {

lib/test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -710,9 +710,15 @@ class Test extends Base {
710710
return should
711711
}
712712

713-
autoend () {
714-
this.options.autoend = true
715-
this.maybeAutoend()
713+
autoend (value) {
714+
// set to false to NOT trigger autoend
715+
if (value === false) {
716+
this.options.autoend = false
717+
clearTimeout(this.autoendTimer)
718+
} else {
719+
this.options.autoend = true
720+
this.maybeAutoend()
721+
}
716722
}
717723

718724
maybeAutoend () {

tap-snapshots/test-tap.js-TAP.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,37 @@ not ok 2 - timeout!
477477
exports[`test/tap.js TAP timeout sigterm many times > stderr 1`] = `
478478
479479
`
480+
481+
exports[`test/tap.js TAP autoend(false) with teardown > exit status 1`] = `
482+
{ code: 0, signal: null }
483+
`
484+
485+
exports[`test/tap.js TAP autoend(false) with teardown > stdout 1`] = `
486+
TAP version 13
487+
ok 1 - this is fine
488+
1..1
489+
# {time}
490+
tear it down
491+
492+
`
493+
494+
exports[`test/tap.js TAP autoend(false) with teardown > stderr 1`] = `
495+
496+
`
497+
498+
exports[`test/tap.js TAP autoend=false with teardown > exit status 1`] = `
499+
{ code: 0, signal: null }
500+
`
501+
502+
exports[`test/tap.js TAP autoend=false with teardown > stdout 1`] = `
503+
TAP version 13
504+
ok 1 - this is fine
505+
1..1
506+
# {time}
507+
tear it down
508+
509+
`
510+
511+
exports[`test/tap.js TAP autoend=false with teardown > stderr 1`] = `
512+
513+
`

tap-snapshots/test-test.js-TAP.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,6 +2142,13 @@ not ok 6 - cannot create subtest after parent test end # {time}
21422142
21432143
`
21442144

2145+
exports[`test/test.js TAP assertions and weird stuff autoend(false) > autoend(false) 1`] = `
2146+
TAP version 13
2147+
ok 1 - this is fine
2148+
1..1
2149+
2150+
`
2151+
21452152
exports[`test/test.js TAP assertions and weird stuff endAll with test children > endAll with test children 1`] = `
21462153
TAP version 13
21472154
# Subtest: this is the test that never ends

test/tap.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ const cases = {
103103
process.kill(process.pid, 'SIGTERM')
104104
process.kill(process.pid, 'SIGTERM')
105105
},
106+
'autoend(false) with teardown': t => {
107+
t.autoend(false)
108+
t.teardown(() => console.log('tear it down'))
109+
setTimeout(() => {
110+
t.pass('this is fine')
111+
t.end()
112+
}, 50)
113+
},
114+
'autoend=false with teardown': t => {
115+
t.options.autoend = false
116+
t.teardown(() => console.log('tear it down'))
117+
setTimeout(() => {
118+
t.pass('this is fine')
119+
t.end()
120+
}, 50)
121+
},
106122
}
107123

108124
const main = t => {

test/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,15 @@ t.test('assertions and weird stuff', t => {
642642

643643
},
644644

645+
'autoend(false)': tt => {
646+
tt.autoend()
647+
tt.autoend(false)
648+
setTimeout(() => {
649+
tt.pass('this is fine')
650+
tt.end()
651+
}, 50)
652+
},
653+
645654
'endAll with test children': tt => {
646655
tt.test('this is the test that never ends', tt => {
647656
tt.test('it goes on and on my friend', tt => {

0 commit comments

Comments
 (0)