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

fix: Fail expectations that throw non-Errors #843

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ function wrapAssertFn(assertFn) {
err = assertFn(res);
} catch (e) {
err = e;

// Handle errors that fail `instanceof Error`, like `throw 1;` or dealing with multiple realms/contexts:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof#instanceof_and_multiple_realms
if (!(err instanceof Error)) {
err = new Error(err);
}
}
if (err instanceof Error && err.stack) {
badStack = err.stack.replace(err.message, '').split('\n').slice(1);
Expand Down
14 changes: 14 additions & 0 deletions test/supertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,20 @@ describe('request(app)', function () {
.expect('Content-Type', /text/)
.end(done);
});

it('fails for non-Error values that are thrown', function (done) {
get
.expect(function (res) {
// eslint-disable-next-line no-throw-literal
throw 'error123';
})
.end(function (err) {
should.exist(err);
err.message.should.equal('error123');
shouldIncludeStackWithThisFile(err);
done();
});
});
});

describe('handling multiple assertions per field', function () {
Expand Down