I'm trying to write a mocha test which passes on a stream error but fails if the stream ends without an error.
Detecting the error is no problem, but the finish handler is always called, even if the stream is forced to error out. In this code, the error
handler's should.exist(err)
assertion works correctly, but then the finish
handler still throws an error.
describe('catch stream errors', function() {
it('should throw an error', function(done) {
var stream = failStream();
stream.on('error', function(err) {
should.exist(err);
done();
})
stream.on('finish', function() {
done(new Error('Why does this still run?'));
});
stream.write();
stream.end();
})
})
One workaround, which seems like a bit of a hack, is to scope an errored
variable above the handlers, then flip it in the error
handler and check the value in the finish
handler. Seems like there should be a better way of handling this.
var errored = false;
stream.on('error', function(err) {
should.exist(err);
errored = true;
done();
})
stream.on('finish', function() {
if (!errored) {
done(new Error('Error suppressed'));
}
});
Full gist here.
A more elegant solution is to remove the finish listener from inside the error listener. This is similar to the cleanup
method in node's lib/stream
source file.
// Refactor out the listeners
var onerror = function(err) {
should.exist(err);
this.removeListener('finish', onfinish);
done();
};
var onfinish = function() {
done(new Error('Expected stream to throw an error.'));
};
stream.on('error', onerror);
stream.on('finish', onfinish);
I'm building a photo gallery in react js. Obviously it's going to have to be responsive and I've tackled this by setting values like so in the component's render method: let thumbWidth = window....
I'm building a photo gallery in react js. Obviously it's going to have to be responsive and I've tackled this by setting values like so in the component's render method: let thumbWidth = window....
We have URL when we hit URL URL in browser we got data. We got Data like [{"UserId":"c2fbd9fb-a423-4d33-9ea4-3aa58f7b52cf","UserType":"Parent","OutPutMessage":"Sucess"}]. But We need get data ...
We have URL when we hit URL URL in browser we got data. We got Data like [{"UserId":"c2fbd9fb-a423-4d33-9ea4-3aa58f7b52cf","UserType":"Parent","OutPutMessage":"Sucess"}]. But We need get data ...
I want to access the developer tools on this domain "http://umang.gov.in" using F12 or right click of the mouse but the site website developer block this option. I follow this step to access this. ...
I want to access the developer tools on this domain "http://umang.gov.in" using F12 or right click of the mouse but the site website developer block this option. I follow this step to access this. ...
I am developing an Ember-cli app with ember 2.1. I created a templates/loading.hbs and several other templates. My foo template has a input that I use to send a queryParam to foo/bar. foo/bar uses the ...
I am developing an Ember-cli app with ember 2.1. I created a templates/loading.hbs and several other templates. My foo template has a input that I use to send a queryParam to foo/bar. foo/bar uses the ...