Skip to content

Commit

Permalink
to yield output satisfying: Buffer up the chunks so that the assertio…
Browse files Browse the repository at this point in the history
…n can run on the same subject multiple times.
  • Loading branch information
papandreou committed Sep 23, 2015
1 parent 2e1d0c4 commit faaa094
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
22 changes: 14 additions & 8 deletions lib/unexpectedStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ module.exports = {
var extraArgs = Array.prototype.slice.call(arguments, 2),
mustConcat = this.alternations[0] === 'output';

expect(subject, 'to have property', 'readable', true);

return expect.promise(function (resolve, reject) {
var chunks = [];
subject.on('data', function (chunk) {
chunks.push(chunk);
}).on('end', function () {
if (!subject._unexpectedStreamChunksPromise) {
subject._unexpectedStreamChunksPromise = expect.promise(function (resolve, reject) {
expect(subject, 'to have property', 'readable', true);
var chunks = [];
subject.on('data', function (chunk) {
chunks.push(chunk);
}).on('end', function () {
resolve(chunks);
}).on('error', reject);
});
}
subject._unexpectedStreamChunksPromise.then(function (chunks) {
var result;
if (mustConcat) {
if (chunks.length > 0 && chunks.every(function (chunk) { return typeof chunk === 'string'; })) {
Expand All @@ -72,10 +78,10 @@ module.exports = {
result = chunks;
}
resolve(result);
}).on('error', reject);
});
}).then(function (result) {
return expect.apply(expect, [result, 'to satisfy assertion'].concat(extraArgs));
});
});
}
};
};
12 changes: 11 additions & 1 deletion test/unexpectedStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('unexpected-stream', function () {

expect.output.preferredWidth = 150;

describe('to yield output', function () {
describe('to yield output satisfying', function () {
it('should buffer up the output of a readable stream that outputs buffers', function () {
return expect(fs.createReadStream(fooTxtPath), 'to yield output satisfying', new Buffer('foobarquux\n', 'utf-8'));
});
Expand All @@ -31,6 +31,16 @@ describe('unexpected-stream', function () {
" +blah"
);
});

it('should be able to assert the output of the same stream multiple times', function () {
var readStream = fs.createReadStream(fooTxtPath, {encoding: 'utf-8'});
return expect.promise.all([
expect(readStream, 'to yield output satisfying', 'to equal', 'foobarquux\n'),
expect(readStream, 'to yield output satisfying', 'to equal', 'foobarquux\n')
]).then(function () {
return expect(readStream, 'to yield output satisfying', 'to equal', 'foobarquux\n');
});
});
});

describe('to yield chunks', function () {
Expand Down

0 comments on commit faaa094

Please sign in to comment.