Skip to content

Commit

Permalink
Merge pull request #767 from alexandre-abrioux/expect-stacktrace
Browse files Browse the repository at this point in the history
fix: add stacktrace to failed expect function call
  • Loading branch information
titanism committed Oct 3, 2022
2 parents ba4b43b + 3dba4e9 commit e064b5a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
7 changes: 6 additions & 1 deletion lib/test.js
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
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 @@ -750,9 +751,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 @@ -776,9 +775,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
@@ -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);
};
};

0 comments on commit e064b5a

Please sign in to comment.