Skip to content

Commit b3ac146

Browse files
committed
Include more debug information in AssertionError output
1 parent 594bc45 commit b3ac146

File tree

2 files changed

+111
-52
lines changed

2 files changed

+111
-52
lines changed

test/integration/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ async function start ({ client }) {
135135
if (name === 'setup' || name === 'teardown') continue
136136
if (options.test && !name.endsWith(options.test)) continue
137137

138-
const junitTestCase = junitTestSuite.testcase(name, `node_${process.version}/${cleanPath}`)
138+
const junitTestCase = junitTestSuite.testcase(name, `node_${process.version}: ${cleanPath}`)
139139

140140
stats.total += 1
141141
log(' - ' + name)
@@ -148,6 +148,7 @@ async function start ({ client }) {
148148
junitTestSuite.end()
149149
junitTestSuites.end()
150150
generateJunitXmlReport(junit, 'serverless')
151+
err.meta = JSON.stringify(err.meta ?? {}, null, 2)
151152
console.error(err)
152153

153154
if (options.bail) {

test/integration/test-runner.js

Lines changed: 109 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ function build (opts = {}) {
377377
* @returns {Promise}
378378
*/
379379
async function exec (name, actions, stats, junit) {
380+
// tap.comment(name)
380381
for (const action of actions) {
381382
if (action.skip) {
382383
if (shouldSkip(esVersion, action.skip)) {
@@ -411,7 +412,8 @@ function build (opts = {}) {
411412
key.split('.')[0] === '$body'
412413
? action.match[key]
413414
: fillStashedValues(action.match)[key],
414-
action.match
415+
action.match,
416+
response
415417
)
416418
}
417419

@@ -420,7 +422,8 @@ function build (opts = {}) {
420422
const key = Object.keys(action.lt)[0]
421423
lt(
422424
delve(response, fillStashedValues(key)),
423-
fillStashedValues(action.lt)[key]
425+
fillStashedValues(action.lt)[key],
426+
response
424427
)
425428
}
426429

@@ -429,7 +432,8 @@ function build (opts = {}) {
429432
const key = Object.keys(action.gt)[0]
430433
gt(
431434
delve(response, fillStashedValues(key)),
432-
fillStashedValues(action.gt)[key]
435+
fillStashedValues(action.gt)[key],
436+
response
433437
)
434438
}
435439

@@ -438,7 +442,8 @@ function build (opts = {}) {
438442
const key = Object.keys(action.lte)[0]
439443
lte(
440444
delve(response, fillStashedValues(key)),
441-
fillStashedValues(action.lte)[key]
445+
fillStashedValues(action.lte)[key],
446+
response
442447
)
443448
}
444449

@@ -447,7 +452,8 @@ function build (opts = {}) {
447452
const key = Object.keys(action.gte)[0]
448453
gte(
449454
delve(response, fillStashedValues(key)),
450-
fillStashedValues(action.gte)[key]
455+
fillStashedValues(action.gte)[key],
456+
response
451457
)
452458
}
453459

@@ -460,7 +466,8 @@ function build (opts = {}) {
460466
: delve(response, fillStashedValues(key)),
461467
key === '$body'
462468
? action.length[key]
463-
: fillStashedValues(action.length)[key]
469+
: fillStashedValues(action.length)[key],
470+
response
464471
)
465472
}
466473

@@ -469,7 +476,8 @@ function build (opts = {}) {
469476
const isTrue = fillStashedValues(action.is_true)
470477
is_true(
471478
delve(response, isTrue),
472-
isTrue
479+
isTrue,
480+
response
473481
)
474482
}
475483

@@ -478,7 +486,8 @@ function build (opts = {}) {
478486
const isFalse = fillStashedValues(action.is_false)
479487
is_false(
480488
delve(response, isFalse),
481-
isFalse
489+
isFalse,
490+
response
482491
)
483492
}
484493
}
@@ -491,48 +500,67 @@ function build (opts = {}) {
491500
* Asserts that the given value is truthy
492501
* @param {any} the value to check
493502
* @param {string} an optional message
503+
* @param {any} debugging metadata to attach to any assertion errors
494504
* @returns {TestRunner}
495505
*/
496-
function is_true (val, msg) {
497-
assert.ok(val, `expect truthy value: ${msg} - value: ${JSON.stringify(val)}`)
506+
function is_true (val, msg, response) {
507+
try {
508+
assert.ok((typeof val === 'string' && val.toLowerCase() === 'true') || val, `expect truthy value: ${msg} - value: ${JSON.stringify(val)}`)
509+
} catch (err) {
510+
err.response = JSON.stringify(response)
511+
throw err
512+
}
498513
}
499514

500515
/**
501516
* Asserts that the given value is falsey
502517
* @param {any} the value to check
503518
* @param {string} an optional message
519+
* @param {any} debugging metadata to attach to any assertion errors
504520
* @returns {TestRunner}
505521
*/
506-
function is_false (val, msg) {
507-
assert.ok(!val, `expect falsey value: ${msg} - value: ${JSON.stringify(val)}`)
522+
function is_false (val, msg, response) {
523+
try {
524+
assert.ok((typeof val === 'string' && val.toLowerCase() === 'false') || !val, `expect falsey value: ${msg} - value: ${JSON.stringify(val)}`)
525+
} catch (err) {
526+
err.response = JSON.stringify(response)
527+
throw err
528+
}
508529
}
509530

510531
/**
511532
* Asserts that two values are the same
512533
* @param {any} the first value
513534
* @param {any} the second value
535+
* @param {any} debugging metadata to attach to any assertion errors
514536
* @returns {TestRunner}
515537
*/
516-
function match (val1, val2, action) {
517-
// both values are objects
518-
if (typeof val1 === 'object' && typeof val2 === 'object') {
519-
assert.deepEqual(val1, val2, typeof action === 'object' ? JSON.stringify(action) : action)
520-
// the first value is the body as string and the second a pattern string
521-
} else if (
522-
typeof val1 === 'string' && typeof val2 === 'string' &&
523-
val2.startsWith('/') && (val2.endsWith('/\n') || val2.endsWith('/'))
524-
) {
525-
const regStr = val2
526-
.replace(/(^|[^\\])#.*/g, '$1')
527-
.replace(/(^|[^\\])\s+/g, '$1')
528-
.slice(1, -1)
529-
// 'm' adds the support for multiline regex
530-
assert.match(val1, new RegExp(regStr, 'm'), `should match pattern provided: ${val2}, but got: ${val1}`)
531-
// everything else
532-
} else if (typeof val1 === 'string' && typeof val2 === 'string') {
533-
assert.include(val1, val2, `should match pattern provided: ${val2}, but got: ${val1}`)
534-
} else {
535-
assert.equal(val1, val2, `should be equal: ${val1} - ${val2}, action: ${JSON.stringify(action)}`)
538+
function match (val1, val2, action, response) {
539+
try {
540+
// both values are objects
541+
if (typeof val1 === 'object' && typeof val2 === 'object') {
542+
assert.deepEqual(val1, val2, typeof action === 'object' ? JSON.stringify(action) : action)
543+
// the first value is the body as string and the second a pattern string
544+
} else if (
545+
typeof val1 === 'string' && typeof val2 === 'string' &&
546+
val2.startsWith('/') && (val2.endsWith('/\n') || val2.endsWith('/'))
547+
) {
548+
const regStr = val2
549+
.replace(/(^|[^\\])#.*/g, '$1')
550+
.replace(/(^|[^\\])\s+/g, '$1')
551+
.slice(1, -1)
552+
// 'm' adds the support for multiline regex
553+
assert.match(val1, new RegExp(regStr, 'm'), `should match pattern provided: ${val2}, but got: ${val1}: ${JSON.stringify(action)}`)
554+
} else if (typeof val1 === 'string' && typeof val2 === 'string') {
555+
// string comparison
556+
assert.include(val1, val2, `should include pattern provided: ${val2}, but got: ${val1}: ${JSON.stringify(action)}`)
557+
} else {
558+
// everything else
559+
assert.equal(val1, val2, `should be equal: ${val1} - ${val2}, action: ${JSON.stringify(action)}`)
560+
}
561+
} catch (err) {
562+
err.response = JSON.stringify(response)
563+
throw err
536564
}
537565
}
538566

@@ -541,62 +569,92 @@ function match (val1, val2, action) {
541569
* It also verifies that the two values are numbers
542570
* @param {any} the first value
543571
* @param {any} the second value
572+
* @param {any} debugging metadata to attach to any assertion errors
544573
* @returns {TestRunner}
545574
*/
546-
function lt (val1, val2) {
547-
;[val1, val2] = getNumbers(val1, val2)
548-
assert.ok(val1 < val2)
575+
function lt (val1, val2, response) {
576+
try {
577+
;[val1, val2] = getNumbers(val1, val2)
578+
assert.ok(val1 < val2)
579+
} catch (err) {
580+
err.response = JSON.stringify(response)
581+
throw err
582+
}
549583
}
550584

551585
/**
552586
* Asserts that the first value is greater than the second
553587
* It also verifies that the two values are numbers
554588
* @param {any} the first value
555589
* @param {any} the second value
590+
* @param {any} debugging metadata to attach to any assertion errors
556591
* @returns {TestRunner}
557592
*/
558-
function gt (val1, val2) {
559-
;[val1, val2] = getNumbers(val1, val2)
560-
assert.ok(val1 > val2)
593+
function gt (val1, val2, response) {
594+
try {
595+
;[val1, val2] = getNumbers(val1, val2)
596+
assert.ok(val1 > val2)
597+
} catch (err) {
598+
err.response = JSON.stringify(response)
599+
throw err
600+
}
561601
}
562602

563603
/**
564604
* Asserts that the first value is less than or equal the second
565605
* It also verifies that the two values are numbers
566606
* @param {any} the first value
567607
* @param {any} the second value
608+
* @param {any} debugging metadata to attach to any assertion errors
568609
* @returns {TestRunner}
569610
*/
570-
function lte (val1, val2) {
571-
;[val1, val2] = getNumbers(val1, val2)
572-
assert.ok(val1 <= val2)
611+
function lte (val1, val2, response) {
612+
try {
613+
;[val1, val2] = getNumbers(val1, val2)
614+
assert.ok(val1 <= val2)
615+
} catch (err) {
616+
err.response = JSON.stringify(response)
617+
throw err
618+
}
573619
}
574620

575621
/**
576622
* Asserts that the first value is greater than or equal the second
577623
* It also verifies that the two values are numbers
578624
* @param {any} the first value
579625
* @param {any} the second value
626+
* @param {any} debugging metadata to attach to any assertion errors
580627
* @returns {TestRunner}
581628
*/
582-
function gte (val1, val2) {
583-
;[val1, val2] = getNumbers(val1, val2)
584-
assert.ok(val1 >= val2)
629+
function gte (val1, val2, response) {
630+
try {
631+
;[val1, val2] = getNumbers(val1, val2)
632+
assert.ok(val1 >= val2)
633+
} catch (err) {
634+
err.response = JSON.stringify(response)
635+
throw err
636+
}
585637
}
586638

587639
/**
588640
* Asserts that the given value has the specified length
589641
* @param {string|object|array} the object to check
590642
* @param {number} the expected length
643+
* @param {any} debugging metadata to attach to any assertion errors
591644
* @returns {TestRunner}
592645
*/
593-
function length (val, len) {
594-
if (typeof val === 'string' || Array.isArray(val)) {
595-
assert.equal(val.length, len)
596-
} else if (typeof val === 'object' && val !== null) {
597-
assert.equal(Object.keys(val).length, len)
598-
} else {
599-
assert.fail(`length: the given value is invalid: ${val}`)
646+
function length (val, len, response) {
647+
try {
648+
if (typeof val === 'string' || Array.isArray(val)) {
649+
assert.equal(val.length, len)
650+
} else if (typeof val === 'object' && val !== null) {
651+
assert.equal(Object.keys(val).length, len)
652+
} else {
653+
assert.fail(`length: the given value is invalid: ${val}`)
654+
}
655+
} catch (err) {
656+
err.response = JSON.stringify(response)
657+
throw err
600658
}
601659
}
602660

0 commit comments

Comments
 (0)