Skip to content

Commit

Permalink
Added response error message to status assertion error
Browse files Browse the repository at this point in the history
refs https://github.com/TryGhost/Toolbox/issues/210

- Whenevers the status assertion fails and there's a concrete error present in the response body it is much more readable to have that error displayed. Just having the status code missmatch shown leaves the developer with no clear clue where to investigate next. The error messages present in the response body usually are much much more precise and give clearer clue on where to dig next or what exactly has failed
  • Loading branch information
naz committed Feb 15, 2022
1 parent 79a6961 commit d83c20b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/express-test/lib/expect-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ class ExpectRequest extends Request {
const {error} = assertion;

error.message = `Expected statusCode ${assertion.expected}, got statusCode ${response.statusCode} ${error.contextString}`;

if (response.body && response.body.errors && response.body.errors[0].message) {
error.message += `\n${response.body.errors[0].message}`;
}

error.actual = response.statusCode;

assert.equal(response.statusCode, assertion.expected, error);
Expand Down
26 changes: 26 additions & 0 deletions packages/express-test/test/expect-request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,32 @@ describe('ExpectRequest', function () {
assert.throws(assertFn);
});

it('_assertStatus not ok when status i not ok and shows response error when present', function () {
const fn = () => { };
const jar = {};
const opts = new RequestOptions();
const request = new ExpectRequest(fn, jar, opts);

const error = new assert.AssertionError({});
error.contextString = 'foo';

const response = {
statusCode: 404,
body: {
errors: [{
message: 'Not found'
}]
}
};
const assertion = {expected: 200, error};

const assertFn = () => {
request._assertStatus(response, assertion);
};

assert.throws(assertFn, {message: 'Expected statusCode 200, got statusCode 404 foo\nNot found'});
});

it('_assertHeader ok when header is ok', function () {
const fn = () => { };
const jar = {};
Expand Down

0 comments on commit d83c20b

Please sign in to comment.