Skip to content

Commit 6f3928f

Browse files
authoredJan 15, 2021
Merge pull request #699 from jpbochi/new-stack-trace
produces edited stack that starts with error name
2 parents fe2368b + 0288826 commit 6f3928f

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed
 

‎lib/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function wrapAssertFn(assertFn) {
8080
var err = assertFn(res);
8181
if (err && err.stack) {
8282
badStack = err.stack.replace(err.message, '').split('\n').slice(1);
83-
err.stack = [err.message, savedStack, '----', badStack].flat().join('\n');
83+
err.stack = [err.toString(), savedStack, '----', badStack].flat().join('\n');
8484
}
8585
return err;
8686
};

‎test/supertest.js

+27-22
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ const nock = require('nock');
1212

1313
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
1414

15+
function shouldIncludeStackWithThisFile(err) {
16+
err.stack.should.match(/test\/supertest.js:/);
17+
err.stack.should.startWith(err.name + ':');
18+
}
19+
1520
describe('request(url)', function () {
1621
it('should be supported', function (done) {
1722
const app = express();
@@ -359,7 +364,7 @@ describe('request(app)', function () {
359364
.expect(404)
360365
.end(function (err, res) {
361366
err.message.should.equal('expected 404 "Not Found", got 200 "OK"');
362-
err.stack.should.match(/test\/supertest.js:/);
367+
shouldIncludeStackWithThisFile(err);
363368
done();
364369
});
365370
});
@@ -420,7 +425,7 @@ describe('request(app)', function () {
420425
.expect(200, '')
421426
.end(function (err, res) {
422427
err.message.should.equal('expected \'\' response body, got \'foo\'');
423-
err.stack.should.match(/test\/supertest.js:/);
428+
shouldIncludeStackWithThisFile(err);
424429
done();
425430
});
426431
});
@@ -442,7 +447,7 @@ describe('request(app)', function () {
442447
.expect('hey')
443448
.end(function (err, res) {
444449
err.message.should.equal('expected \'hey\' response body, got \'{"foo":"bar"}\'');
445-
err.stack.should.match(/test\/supertest.js:/);
450+
shouldIncludeStackWithThisFile(err);
446451
done();
447452
});
448453
});
@@ -462,7 +467,7 @@ describe('request(app)', function () {
462467
.expect('hey')
463468
.end(function (err, res) {
464469
err.message.should.equal('expected 200 "OK", got 500 "Internal Server Error"');
465-
err.stack.should.match(/test\/supertest.js:/);
470+
shouldIncludeStackWithThisFile(err);
466471
done();
467472
});
468473
});
@@ -495,7 +500,7 @@ describe('request(app)', function () {
495500
.expect({ foo: 'baz' })
496501
.end(function (err, res) {
497502
err.message.should.equal('expected { foo: \'baz\' } response body, got { foo: \'bar\' }');
498-
err.stack.should.match(/test\/supertest.js:/);
503+
shouldIncludeStackWithThisFile(err);
499504

500505
request(app)
501506
.get('/')
@@ -527,7 +532,7 @@ describe('request(app)', function () {
527532
.expect({ stringValue: 'foo', numberValue: 3, nestedObject: { innerString: 5 } })
528533
.end(function (err, res) {
529534
err.message.should.equal('expected {\n stringValue: \'foo\',\n numberValue: 3,\n nestedObject: { innerString: 5 }\n} response body, got {\n stringValue: \'foo\',\n numberValue: 3,\n nestedObject: { innerString: \'5\' }\n}'); // eslint-disable-line max-len
530-
err.stack.should.match(/test\/supertest.js:/);
535+
shouldIncludeStackWithThisFile(err);
531536

532537
request(app)
533538
.get('/')
@@ -548,7 +553,7 @@ describe('request(app)', function () {
548553
.expect(/^bar/)
549554
.end(function (err, res) {
550555
err.message.should.equal('expected body \'foobar\' to match /^bar/');
551-
err.stack.should.match(/test\/supertest.js:/);
556+
shouldIncludeStackWithThisFile(err);
552557
done();
553558
});
554559
});
@@ -567,7 +572,7 @@ describe('request(app)', function () {
567572
.expect('hey tj')
568573
.end(function (err, res) {
569574
err.message.should.equal("expected 'hey' response body, got 'hey tj'");
570-
err.stack.should.match(/test\/supertest.js:/);
575+
shouldIncludeStackWithThisFile(err);
571576
done();
572577
});
573578
});
@@ -600,7 +605,7 @@ describe('request(app)', function () {
600605
.expect('Content-Foo', 'bar')
601606
.end(function (err, res) {
602607
err.message.should.equal('expected "Content-Foo" header field');
603-
err.stack.should.match(/test\/supertest.js:/);
608+
shouldIncludeStackWithThisFile(err);
604609
done();
605610
});
606611
});
@@ -618,7 +623,7 @@ describe('request(app)', function () {
618623
.end(function (err, res) {
619624
err.message.should.equal('expected "Content-Type" of "text/html", '
620625
+ 'got "application/json; charset=utf-8"');
621-
err.stack.should.match(/test\/supertest.js:/);
626+
shouldIncludeStackWithThisFile(err);
622627
done();
623628
});
624629
});
@@ -650,7 +655,7 @@ describe('request(app)', function () {
650655
.end(function (err) {
651656
err.message.should.equal('expected "Content-Type" matching /^application/, '
652657
+ 'got "text/html; charset=utf-8"');
653-
err.stack.should.match(/test\/supertest.js:/);
658+
shouldIncludeStackWithThisFile(err);
654659
done();
655660
});
656661
});
@@ -667,7 +672,7 @@ describe('request(app)', function () {
667672
.expect('Content-Length', 4)
668673
.end(function (err) {
669674
err.message.should.equal('expected "Content-Length" of "4", got "3"');
670-
err.stack.should.match(/test\/supertest.js:/);
675+
shouldIncludeStackWithThisFile(err);
671676
done();
672677
});
673678
});
@@ -694,7 +699,7 @@ describe('request(app)', function () {
694699
})
695700
.end(function (err) {
696701
err.message.should.equal('failed');
697-
err.stack.should.match(/test\/supertest.js:/);
702+
shouldIncludeStackWithThisFile(err);
698703
done();
699704
});
700705
});
@@ -720,7 +725,7 @@ describe('request(app)', function () {
720725
})
721726
.end(function (err) {
722727
err.message.should.equal('some descriptive error');
723-
err.stack.should.match(/test\/supertest.js:/);
728+
shouldIncludeStackWithThisFile(err);
724729
(err instanceof Error).should.be.true;
725730
done();
726731
});
@@ -761,7 +766,7 @@ describe('request(app)', function () {
761766
.expect('Content-Type', /json/)
762767
.end(function (err) {
763768
err.message.should.match(/Content-Type/);
764-
err.stack.should.match(/test\/supertest.js:/);
769+
shouldIncludeStackWithThisFile(err);
765770
done();
766771
});
767772
});
@@ -805,7 +810,7 @@ describe('request(app)', function () {
805810
.end(function (err) {
806811
err.message.should.equal('expected "Content-Type" matching /bloop/, '
807812
+ 'got "text/html; charset=utf-8"');
808-
err.stack.should.match(/test\/supertest.js:/);
813+
shouldIncludeStackWithThisFile(err);
809814
done();
810815
});
811816
});
@@ -824,7 +829,7 @@ describe('request(app)', function () {
824829
.end(function (err) {
825830
err.message.should.equal('expected "Content-Type" matching /bloop/, '
826831
+ 'got "text/html; charset=utf-8"');
827-
err.stack.should.match(/test\/supertest.js:/);
832+
shouldIncludeStackWithThisFile(err);
828833
done();
829834
});
830835
});
@@ -843,7 +848,7 @@ describe('request(app)', function () {
843848
.end(function (err) {
844849
err.message.should.equal('expected "Content-Type" matching /bloop/, '
845850
+ 'got "text/html; charset=utf-8"');
846-
err.stack.should.match(/test\/supertest.js:/);
851+
shouldIncludeStackWithThisFile(err);
847852
done();
848853
});
849854
});
@@ -1002,7 +1007,7 @@ describe('assert ordering by call order', function () {
10021007
.end(function (err, res) {
10031008
err.message.should.equal('expected \'hey\' response body, '
10041009
+ 'got \'{"message":"something went wrong"}\'');
1005-
err.stack.should.match(/test\/supertest.js:/);
1010+
shouldIncludeStackWithThisFile(err);
10061011
done();
10071012
});
10081013
});
@@ -1022,7 +1027,7 @@ describe('assert ordering by call order', function () {
10221027
.expect('hey')
10231028
.end(function (err, res) {
10241029
err.message.should.equal('expected 200 "OK", got 500 "Internal Server Error"');
1025-
err.stack.should.match(/test\/supertest.js:/);
1030+
shouldIncludeStackWithThisFile(err);
10261031
done();
10271032
});
10281033
});
@@ -1043,7 +1048,7 @@ describe('assert ordering by call order', function () {
10431048
.end(function (err, res) {
10441049
err.message.should.equal('expected "content-type" matching /html/, '
10451050
+ 'got "application/json; charset=utf-8"');
1046-
err.stack.should.match(/test\/supertest.js:/);
1051+
shouldIncludeStackWithThisFile(err);
10471052
done();
10481053
});
10491054
});
@@ -1216,7 +1221,7 @@ describe('request.get(url).query(vals) works as expected', function () {
12161221
.end(function (err, res) {
12171222
err.should.be.an.instanceof(Error);
12181223
err.message.should.match(/Nock: Disallowed net connect/);
1219-
err.stack.should.match(/test\/supertest.js:/);
1224+
shouldIncludeStackWithThisFile(err);
12201225
done();
12211226
});
12221227

0 commit comments

Comments
 (0)
Please sign in to comment.