UNPKG

1.26 kBJavaScriptView Raw
1'use strict'
2
3const test = require('tap').test
4const stream = require('../lib/destroyable-stream')
5
6test('Destroyable streams', function (t) {
7 t.plan(4 * 3 + 4 * 2)
8
9 testStream(new stream.Transform(), true)
10 testStream(new stream.Readable({ read: () => {} }), true)
11 testStream(new stream.Duplex({ read: () => {} }), true)
12 testStream(new stream.Writable({ read: () => {} }), true)
13 testStream(new stream.Transform(), false)
14 testStream(new stream.Readable({ read: () => {} }), false)
15 testStream(new stream.Duplex({ read: () => {} }), false)
16 testStream(new stream.Writable({ read: () => {} }), false)
17
18 function testStream (s, shouldError) {
19 const error = new Error('error')
20 let errored = false
21
22 s.once('end', function () {
23 t.fail('should not end')
24 })
25
26 s.once('finish', function () {
27 t.fail('should not finish')
28 })
29
30 s.once('error', function (err) {
31 errored = true
32 t.strictEqual(err, error)
33 })
34
35 s.once('close', function () {
36 if (shouldError) t.ok(errored, 'should have errored first')
37 else t.notOk(errored, 'should not have errored first')
38 t.pass('should emit close')
39 })
40
41 if (s.resume) s.resume()
42 if (shouldError) s.destroy(error)
43 else s.destroy()
44 }
45})