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: add stacktrace to failed expect function call #767

Merged
merged 2 commits into from
Oct 3, 2022
Merged
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
7 changes: 6 additions & 1 deletion lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,12 @@ function wrapAssertFn(assertFn) {

return function(res) {
let badStack;
const err = assertFn(res);
let err;
try {
err = assertFn(res);
} catch (e) {
err = e;
}
if (err instanceof Error && err.stack) {
badStack = err.stack.replace(err.message, '').split('\n').slice(1);
err.stack = [err.toString()]
Expand Down
9 changes: 3 additions & 6 deletions test/supertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const nock = require('nock');
const request = require('../index.js');
const throwError = require('./throwError');

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

Expand Down Expand Up @@ -748,9 +749,7 @@ describe('request(app)', function () {

it('reports errors', function (done) {
get
.expect(function (res) {
throw new Error('failed');
})
.expect(throwError('failed'))
.end(function (err) {
err.message.should.equal('failed');
shouldIncludeStackWithThisFile(err);
Expand All @@ -774,9 +773,7 @@ describe('request(app)', function () {

it('ensures truthy errors returned from asserts are throw to end', function (done) {
get
.expect(function (res) {
return new Error('some descriptive error');
})
.expect(throwError('some descriptive error'))
.end(function (err) {
err.message.should.equal('some descriptive error');
shouldIncludeStackWithThisFile(err);
Expand Down
10 changes: 10 additions & 0 deletions test/throwError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

/**
* This method needs to reside in its own module in order to properly test stack trace handling.
*/
module.exports = function throwError(message) {
return function() {
throw new Error(message);
};
};