JavaScript Test should pass on stream error, fail on success

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.

Answer:1

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);
Answer:2

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....

  1. change value react
  2. change value react-select
  3. change props value react
  4. change context value react
  5. change input value react
  6. change state value react
  7. change props value react native
  8. change textinput value react native
  9. change ref value react
  10. change state value react native
  11. change animated value react native
  12. change text value react native
  13. change checkbox value react
  14. change value onclick react
  15. change const value react
  16. change array value react
  17. change textarea value react
  18. change context value react hooks
  19. change const value react native
  20. change input value react testing library

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. ...

  1. how to unblock developer tools on chromebook

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 ...