Skip to content
This repository was archived by the owner on May 31, 2020. It is now read-only.

Commit 6632cf9

Browse files
committed
test: test coverage
1 parent 7b255dd commit 6632cf9

File tree

4 files changed

+82
-10
lines changed

4 files changed

+82
-10
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ module.exports = (options) => {
6363
if (this._dest) {
6464
this._dest.stream.write(chunk, encoding, callback);
6565
} else {
66-
this._destWritten.push(arguments);
66+
this._destWritten.push([chunk, encoding, callback]);
6767
}
6868
}
6969

test/error-test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const streamify = require('..');
2+
const assert = require('assert');
3+
4+
5+
describe('Errorneously call stream methods', () => {
6+
describe('Call `stream.addSource()` with an existing source', () => {
7+
it('Throws error', () => {
8+
const stream = streamify();
9+
stream.addSource(streamify());
10+
assert.throws(() => {
11+
stream.addSource(streamify());
12+
}, /source stream has already been added/);
13+
});
14+
});
15+
16+
describe('Call `stream.addDest()` with an existing dest', () => {
17+
it('Throws error', () => {
18+
const stream = streamify();
19+
stream.addDest(streamify());
20+
assert.throws(() => {
21+
stream.addDest(streamify());
22+
}, /destination stream has already been added/);
23+
});
24+
});
25+
26+
describe('Call `stream.removeSource()` without adding one', () => {
27+
it('Throws error', () => {
28+
const stream = streamify();
29+
assert.throws(() => {
30+
stream.removeSource();
31+
}, /source stream has not been added/);
32+
});
33+
});
34+
35+
describe('Call `stream.removeDest()` without adding one', () => {
36+
it('Throws error', () => {
37+
const stream = streamify();
38+
assert.throws(() => {
39+
stream.removeDest(streamify());
40+
}, /destination stream has not been added/);
41+
});
42+
});
43+
44+
});

test/pipe-test.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ describe('Pipe from a readable stream', () => {
1717
stream.pipe(writeStream);
1818

1919
streamEqual(readStream, writeStream, (err, equal) => {
20-
if (err) return done(err);
21-
20+
assert.ifError(err);
2221
assert.ok(equal);
2322
done();
2423
});
@@ -38,8 +37,7 @@ describe('Pipe to a writable stream', () => {
3837
fs.createReadStream(input, { bufferSize: 1024 }).pipe(stream);
3938

4039
streamEqual(readStream, writeStream, (err, equal) => {
41-
if (err) return done(err);
42-
40+
assert.ifError(err);
4341
assert.ok(equal);
4442
done();
4543
});
@@ -58,8 +56,7 @@ describe('Pipe to itself', () => {
5856
stream.pipe(stream);
5957

6058
streamEqual(readStream, writeStream, (err, equal) => {
61-
if (err) return done(err);
62-
59+
assert.ifError(err);
6360
assert.ok(equal);
6461
done();
6562
});

test/writable-test.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ const assert = require('assert');
33
const path = require('path');
44
const fs = require('fs');
55

6-
const output1 = path.join(__dirname, 'files', 'output1.txt');
7-
const output2 = path.join(__dirname, 'files', 'output2.txt');
8-
96

107
describe('Create a writable stream', () => {
118
it('Does not throw "write after end" error"', (done) => {
9+
const output1 = path.join(__dirname, 'files', 'output1.txt');
1210
const stream = streamify({ readable: false });
1311
stream.resolve(fs.createWriteStream(output1));
1412
after((done) => fs.unlink(output1, done));
@@ -25,6 +23,7 @@ describe('Create a writable stream', () => {
2523
});
2624

2725
it('Ends underlying write stream when streamify ends', (done) => {
26+
const output2 = path.join(__dirname, 'files', 'output2.txt');
2827
const stream = streamify({ readable: false });
2928
stream.end('the only one');
3029

@@ -35,4 +34,36 @@ describe('Create a writable stream', () => {
3534
ws.on('close', done);
3635
}, 50);
3736
});
37+
38+
describe('Add another destination after unresolving', () => {
39+
it('Writes to both streams', (done) => {
40+
const output3 = path.join(__dirname, 'files', 'output3.txt');
41+
const output4 = path.join(__dirname, 'files', 'output4.txt');
42+
const stream = streamify({ readable: false });
43+
44+
stream.resolve(fs.createWriteStream(output3));
45+
after((done) => fs.unlink(output3, done));
46+
stream.write('one\n');
47+
stream.unresolve();
48+
49+
stream.resolve(fs.createWriteStream(output4));
50+
after((done) => fs.unlink(output4, done));
51+
stream.write('two!\n');
52+
stream.end('end\n');
53+
54+
stream.on('finish', () => {
55+
let n = 2;
56+
fs.readFile(output3, 'utf8', (err, data) => {
57+
assert.ifError(err);
58+
assert.equal(data, 'one\n');
59+
if (--n === 0) { done(); }
60+
});
61+
fs.readFile(output4, 'utf8', (err, data) => {
62+
assert.ifError(err);
63+
assert.equal(data, 'two!\nend\n');
64+
if (--n === 0) { done(); }
65+
});
66+
});
67+
});
68+
});
3869
});

0 commit comments

Comments
 (0)