Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Inherited status / statusCode properties can not be checked #218

Closed
austince opened this issue Jul 25, 2018 · 2 comments
Closed

Bug: Inherited status / statusCode properties can not be checked #218

austince opened this issue Jul 25, 2018 · 2 comments

Comments

@austince
Copy link
Contributor

austince commented Jul 25, 2018

Using the popular error library http-errors, the normal chai-http assertions for status / statusCode do not work. I think this is because of the inheritence used by http-errors when creating new error objects.

I have this, which works:

const createError = require('http-errors');

  try {
    throw createError(400, "Bad request");
  } catch (err) {
    expect(err).to.have.property('message');
    expect(err).to.have.property('status');
    expect(err.status).to.equal(400);
    return;
  }

It should probably still work as:

const createError = require('http-errors');

  try {
    throw createError(400, "Bad request");
  } catch (err) {
    expect(err).to.have.property('message');
    expect(err).to.have.status(400);
    return;
  }
@keithamus
Copy link
Member

Hey @austince thanks for the issue!

I'm guessing this doesn't work because of L80 in the status assertion:

chai-http/lib/http.js

Lines 79 to 90 in 1af976d

Assertion.addMethod('status', function (code) {
new Assertion(this._obj).to.have.any.keys('status', 'statusCode');
var status = this._obj.status || this._obj.statusCode;
this.assert(
status == code
, 'expected #{this} to have status code #{exp} but got #{act}'
, 'expected #{this} to not have status code #{act}'
, code
, status
);
});

We should make this work though! We could change that assertion to just use something like if('status' in this._obj) instead.

PR's welcome!

@austince
Copy link
Contributor Author

austince commented Aug 1, 2018

Fixed with #219 🎊

@austince austince closed this as completed Aug 1, 2018
@austince austince mentioned this issue Aug 2, 2018
JamesMessinger added a commit that referenced this issue Aug 14, 2018
4.1.0

Changes included:

- #219
- #217

Fixes:

- #218
- #189
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants