Skip to content

Commit 7b37da4

Browse files
authored
chore: Add tests for streamx (#58)
fix!: Allow end-of-stream to handle the stream error states
1 parent dfa4f0b commit 7b37da4

File tree

4 files changed

+87
-7
lines changed

4 files changed

+87
-7
lines changed

index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ var eos = require('end-of-stream');
66
var once = require('once');
77
var exhaust = require('stream-exhaust');
88

9-
var eosConfig = {
10-
error: false,
11-
};
9+
var eosConfig = {};
1210

1311
function rethrowAsync(err) {
1412
process.nextTick(rethrow);

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"nyc": "^15.1.0",
4343
"pumpify": "^2.0.1",
4444
"rxjs": "^7.4.0",
45+
"streamx": "^2.12.0",
4546
"through2": "^4.0.2",
4647
"typescript": "^4.4.4"
4748
},

test/streams.js

-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ function pumpifyError() {
4141
var read = fs.createReadStream(exists);
4242
var pipeline = pumpify(through(), through(withErr), through());
4343

44-
pipeline.on('error', function (err) {
45-
throw err;
46-
});
47-
4844
return read.pipe(pipeline);
4945
}
5046

test/streamx.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
3+
var expect = require('expect');
4+
5+
var streamx = require('streamx');
6+
7+
var asyncDone = require('../');
8+
9+
function success() {
10+
return streamx.Readable.from('Foo Bar Baz').pipe(new streamx.Writable());
11+
}
12+
13+
function failure() {
14+
return streamx.Readable.from('Foo Bar Baz').pipe(
15+
new streamx.Writable({
16+
write: function (data, cb) {
17+
cb(new Error('Fail'));
18+
},
19+
})
20+
);
21+
}
22+
23+
function pipelineError() {
24+
return streamx.pipeline(
25+
streamx.Readable.from('Foo Bar Baz'),
26+
new streamx.Transform(),
27+
new streamx.Transform({
28+
transform: function (data, cb) {
29+
cb(new Error('Fail'));
30+
},
31+
}),
32+
new streamx.Writable()
33+
);
34+
}
35+
36+
function unpiped() {
37+
return streamx.Readable.from('Foo Bar Baz');
38+
}
39+
40+
describe('streamx streams', function () {
41+
it('should handle a successful stream', function (done) {
42+
asyncDone(success, function (err) {
43+
expect(err).not.toBeInstanceOf(Error);
44+
done();
45+
});
46+
});
47+
48+
it('should handle an errored stream', function (done) {
49+
asyncDone(failure, function (err) {
50+
expect(err).toBeInstanceOf(Error);
51+
expect(err.message).not.toEqual('premature close');
52+
done();
53+
});
54+
});
55+
56+
it('should handle an errored pipeline', function (done) {
57+
asyncDone(pipelineError, function (err) {
58+
expect(err).toBeInstanceOf(Error);
59+
expect(err.message).not.toEqual('premature close');
60+
done();
61+
});
62+
});
63+
64+
it('handle a returned stream and cb by only calling callback once', function (done) {
65+
asyncDone(
66+
function (cb) {
67+
return success().on('finish', function () {
68+
cb(null, 3);
69+
});
70+
},
71+
function (err, result) {
72+
expect(err).not.toBeInstanceOf(Error);
73+
expect(result).toEqual(3); // To know we called the callback
74+
done();
75+
}
76+
);
77+
});
78+
79+
it('consumes an unpiped readable stream', function (done) {
80+
asyncDone(unpiped, function (err) {
81+
expect(err).not.toBeInstanceOf(Error);
82+
done();
83+
});
84+
});
85+
});

0 commit comments

Comments
 (0)