Skip to content

Commit da852b9

Browse files
committed
Improvements to integrations
Borrowed largely from elastic/elasticsearch-serverless-js#38
1 parent d3f22f1 commit da852b9

File tree

3 files changed

+119
-52
lines changed

3 files changed

+119
-52
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ npm-debug.log
33
test/benchmarks
44
elasticsearch
55
.git
6+
lib
7+
junit-output

test/integration/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ async function start ({ client, isXPack }) {
317317
if (name === 'setup' || name === 'teardown') continue
318318
if (options.test && !name.endsWith(options.test)) continue
319319

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

322322
stats.total += 1
323323
if (shouldSkip(isXPack, file, name)) {
@@ -336,6 +336,7 @@ async function start ({ client, isXPack }) {
336336
junitTestSuite.end()
337337
junitTestSuites.end()
338338
generateJunitXmlReport(junit, isXPack ? 'platinum' : 'free')
339+
err.meta = JSON.stringify(err.meta ?? {}, null, 2)
339340
console.error(err)
340341

341342
if (options.bail) {
@@ -374,6 +375,7 @@ async function start ({ client, isXPack }) {
374375
- Total: ${stats.total}
375376
- Skip: ${stats.skip}
376377
- Pass: ${stats.pass}
378+
- Fail: ${stats.total - (stats.pass + stats.skip)}
377379
- Assertions: ${stats.assertions}
378380
`)
379381
}

test/integration/test-runner.js

Lines changed: 114 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -593,13 +593,14 @@ function build (opts = {}) {
593593
const key = Object.keys(action.match)[0]
594594
match(
595595
// in some cases, the yaml refers to the body with an empty string
596-
key === '$body' || key === ''
596+
key.split('.')[0] === '$body' || key === ''
597597
? response
598598
: delve(response, fillStashedValues(key)),
599-
key === '$body'
599+
key.split('.')[0] === '$body'
600600
? action.match[key]
601601
: fillStashedValues(action.match)[key],
602-
action.match
602+
action.match,
603+
response
603604
)
604605
}
605606

@@ -608,7 +609,8 @@ function build (opts = {}) {
608609
const key = Object.keys(action.lt)[0]
609610
lt(
610611
delve(response, fillStashedValues(key)),
611-
fillStashedValues(action.lt)[key]
612+
fillStashedValues(action.lt)[key],
613+
response
612614
)
613615
}
614616

@@ -617,7 +619,8 @@ function build (opts = {}) {
617619
const key = Object.keys(action.gt)[0]
618620
gt(
619621
delve(response, fillStashedValues(key)),
620-
fillStashedValues(action.gt)[key]
622+
fillStashedValues(action.gt)[key],
623+
response
621624
)
622625
}
623626

@@ -626,7 +629,8 @@ function build (opts = {}) {
626629
const key = Object.keys(action.lte)[0]
627630
lte(
628631
delve(response, fillStashedValues(key)),
629-
fillStashedValues(action.lte)[key]
632+
fillStashedValues(action.lte)[key],
633+
response
630634
)
631635
}
632636

@@ -635,7 +639,8 @@ function build (opts = {}) {
635639
const key = Object.keys(action.gte)[0]
636640
gte(
637641
delve(response, fillStashedValues(key)),
638-
fillStashedValues(action.gte)[key]
642+
fillStashedValues(action.gte)[key],
643+
response
639644
)
640645
}
641646

@@ -648,7 +653,8 @@ function build (opts = {}) {
648653
: delve(response, fillStashedValues(key)),
649654
key === '$body'
650655
? action.length[key]
651-
: fillStashedValues(action.length)[key]
656+
: fillStashedValues(action.length)[key],
657+
response
652658
)
653659
}
654660

@@ -657,7 +663,8 @@ function build (opts = {}) {
657663
const isTrue = fillStashedValues(action.is_true)
658664
is_true(
659665
delve(response, isTrue),
660-
isTrue
666+
isTrue,
667+
response
661668
)
662669
}
663670

@@ -666,7 +673,8 @@ function build (opts = {}) {
666673
const isFalse = fillStashedValues(action.is_false)
667674
is_false(
668675
delve(response, isFalse),
669-
isFalse
676+
isFalse,
677+
response
670678
)
671679
}
672680
}
@@ -679,46 +687,67 @@ function build (opts = {}) {
679687
* Asserts that the given value is truthy
680688
* @param {any} the value to check
681689
* @param {string} an optional message
690+
* @param {any} debugging metadata to attach to any assertion errors
682691
* @returns {TestRunner}
683692
*/
684-
function is_true (val, msg) {
685-
assert.ok(val, `expect truthy value: ${msg} - value: ${JSON.stringify(val)}`)
693+
function is_true (val, msg, response) {
694+
try {
695+
assert.ok((typeof val === 'string' && val.toLowerCase() === 'true') || val, `expect truthy value: ${msg} - value: ${JSON.stringify(val)}`)
696+
} catch (err) {
697+
err.response = JSON.stringify(response)
698+
throw err
699+
}
686700
}
687701

688702
/**
689703
* Asserts that the given value is falsey
690704
* @param {any} the value to check
691705
* @param {string} an optional message
706+
* @param {any} debugging metadata to attach to any assertion errors
692707
* @returns {TestRunner}
693708
*/
694-
function is_false (val, msg) {
695-
assert.ok(!val, `expect falsey value: ${msg} - value: ${JSON.stringify(val)}`)
709+
function is_false (val, msg, response) {
710+
try {
711+
assert.ok((typeof val === 'string' && val.toLowerCase() === 'false') || !val, `expect falsey value: ${msg} - value: ${JSON.stringify(val)}`)
712+
} catch (err) {
713+
err.response = JSON.stringify(response)
714+
throw err
715+
}
696716
}
697717

698718
/**
699719
* Asserts that two values are the same
700720
* @param {any} the first value
701721
* @param {any} the second value
722+
* @param {any} debugging metadata to attach to any assertion errors
702723
* @returns {TestRunner}
703724
*/
704-
function match (val1, val2, action) {
705-
// both values are objects
706-
if (typeof val1 === 'object' && typeof val2 === 'object') {
707-
assert.deepEqual(val1, val2, typeof action === 'object' ? JSON.stringify(action) : action)
708-
// the first value is the body as string and the second a pattern string
709-
} else if (
710-
typeof val1 === 'string' && typeof val2 === 'string' &&
711-
val2.startsWith('/') && (val2.endsWith('/\n') || val2.endsWith('/'))
712-
) {
713-
const regStr = val2
714-
.replace(/(^|[^\\])#.*/g, '$1')
715-
.replace(/(^|[^\\])\s+/g, '$1')
716-
.slice(1, -1)
717-
// 'm' adds the support for multiline regex
718-
assert.match(val1, new RegExp(regStr, 'm'), `should match pattern provided: ${val2}, but got: ${val1}`)
719-
// everything else
720-
} else {
721-
assert.equal(val1, val2, `should be equal: ${val1} - ${val2}, action: ${JSON.stringify(action)}`)
725+
function match (val1, val2, action, response) {
726+
try {
727+
// both values are objects
728+
if (typeof val1 === 'object' && typeof val2 === 'object') {
729+
assert.deepEqual(val1, val2, typeof action === 'object' ? JSON.stringify(action) : action)
730+
// the first value is the body as string and the second a pattern string
731+
} else if (
732+
typeof val1 === 'string' && typeof val2 === 'string' &&
733+
val2.startsWith('/') && (val2.endsWith('/\n') || val2.endsWith('/'))
734+
) {
735+
const regStr = val2
736+
.replace(/(^|[^\\])#.*/g, '$1')
737+
.replace(/(^|[^\\])\s+/g, '$1')
738+
.slice(1, -1)
739+
// 'm' adds the support for multiline regex
740+
assert.match(val1, new RegExp(regStr, 'm'), `should match pattern provided: ${val2}, but got: ${val1}: ${JSON.stringify(action)}`)
741+
} else if (typeof val1 === 'string' && typeof val2 === 'string') {
742+
// string comparison
743+
assert.include(val1, val2, `should include pattern provided: ${val2}, but got: ${val1}: ${JSON.stringify(action)}`)
744+
} else {
745+
// everything else
746+
assert.equal(val1, val2, `should be equal: ${val1} - ${val2}, action: ${JSON.stringify(action)}`)
747+
}
748+
} catch (err) {
749+
err.response = JSON.stringify(response)
750+
throw err
722751
}
723752
}
724753

@@ -727,62 +756,92 @@ function match (val1, val2, action) {
727756
* It also verifies that the two values are numbers
728757
* @param {any} the first value
729758
* @param {any} the second value
759+
* @param {any} debugging metadata to attach to any assertion errors
730760
* @returns {TestRunner}
731761
*/
732-
function lt (val1, val2) {
733-
;[val1, val2] = getNumbers(val1, val2)
734-
assert.ok(val1 < val2)
762+
function lt (val1, val2, response) {
763+
try {
764+
;[val1, val2] = getNumbers(val1, val2)
765+
assert.ok(val1 < val2)
766+
} catch (err) {
767+
err.response = JSON.stringify(response)
768+
throw err
769+
}
735770
}
736771

737772
/**
738773
* Asserts that the first value is greater than the second
739774
* It also verifies that the two values are numbers
740775
* @param {any} the first value
741776
* @param {any} the second value
777+
* @param {any} debugging metadata to attach to any assertion errors
742778
* @returns {TestRunner}
743779
*/
744-
function gt (val1, val2) {
745-
;[val1, val2] = getNumbers(val1, val2)
746-
assert.ok(val1 > val2)
780+
function gt (val1, val2, response) {
781+
try {
782+
;[val1, val2] = getNumbers(val1, val2)
783+
assert.ok(val1 > val2)
784+
} catch (err) {
785+
err.response = JSON.stringify(response)
786+
throw err
787+
}
747788
}
748789

749790
/**
750791
* Asserts that the first value is less than or equal the second
751792
* It also verifies that the two values are numbers
752793
* @param {any} the first value
753794
* @param {any} the second value
795+
* @param {any} debugging metadata to attach to any assertion errors
754796
* @returns {TestRunner}
755797
*/
756-
function lte (val1, val2) {
757-
;[val1, val2] = getNumbers(val1, val2)
758-
assert.ok(val1 <= val2)
798+
function lte (val1, val2, response) {
799+
try {
800+
;[val1, val2] = getNumbers(val1, val2)
801+
assert.ok(val1 <= val2)
802+
} catch (err) {
803+
err.response = JSON.stringify(response)
804+
throw err
805+
}
759806
}
760807

761808
/**
762809
* Asserts that the first value is greater than or equal the second
763810
* It also verifies that the two values are numbers
764811
* @param {any} the first value
765812
* @param {any} the second value
813+
* @param {any} debugging metadata to attach to any assertion errors
766814
* @returns {TestRunner}
767815
*/
768-
function gte (val1, val2) {
769-
;[val1, val2] = getNumbers(val1, val2)
770-
assert.ok(val1 >= val2)
816+
function gte (val1, val2, response) {
817+
try {
818+
;[val1, val2] = getNumbers(val1, val2)
819+
assert.ok(val1 >= val2)
820+
} catch (err) {
821+
err.response = JSON.stringify(response)
822+
throw err
823+
}
771824
}
772825

773826
/**
774827
* Asserts that the given value has the specified length
775828
* @param {string|object|array} the object to check
776829
* @param {number} the expected length
830+
* @param {any} debugging metadata to attach to any assertion errors
777831
* @returns {TestRunner}
778832
*/
779-
function length (val, len) {
780-
if (typeof val === 'string' || Array.isArray(val)) {
781-
assert.equal(val.length, len)
782-
} else if (typeof val === 'object' && val !== null) {
783-
assert.equal(Object.keys(val).length, len)
784-
} else {
785-
assert.fail(`length: the given value is invalid: ${val}`)
833+
function length (val, len, response) {
834+
try {
835+
if (typeof val === 'string' || Array.isArray(val)) {
836+
assert.equal(val.length, len)
837+
} else if (typeof val === 'object' && val !== null) {
838+
assert.equal(Object.keys(val).length, len)
839+
} else {
840+
assert.fail(`length: the given value is invalid: ${val}`)
841+
}
842+
} catch (err) {
843+
err.response = JSON.stringify(response)
844+
throw err
786845
}
787846
}
788847

@@ -813,6 +872,10 @@ function length (val, len) {
813872
*/
814873
function parseDo (action) {
815874
action = JSON.parse(JSON.stringify(action))
875+
876+
if (typeof action === 'string') action = {[action]: {}}
877+
if (Array.isArray(action)) action = action[0]
878+
816879
return Object.keys(action).reduce((acc, val) => {
817880
switch (val) {
818881
case 'catch':

0 commit comments

Comments
 (0)