From 4e30df5f66a0f78cf4141657f909209a86411da5 Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Mon, 23 Sep 2024 13:58:14 +0200 Subject: [PATCH 1/2] improve assert include --- lib/assert/include.js | 54 ++++++++++++++++++++++--------------------- test/helper/webapi.js | 2 +- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/lib/assert/include.js b/lib/assert/include.js index 3899f4ca0..2a758c04e 100644 --- a/lib/assert/include.js +++ b/lib/assert/include.js @@ -6,33 +6,36 @@ const output = require('../output') const MAX_LINES = 10 class InclusionAssertion extends Assertion { - constructor(params) { + constructor(params = {}) { params.jar = params.jar || 'string' - const comparator = function (needle, haystack) { + super(InclusionAssertion.createComparator(), params) + this.params.type = 'to include' + this.template = undefined + } + + static createComparator() { + return (needle, haystack) => { if (Array.isArray(haystack)) { - return haystack.filter((part) => part.indexOf(needle) >= 0).length > 0 + return haystack.some((part) => part.includes(needle)) } - return haystack.indexOf(needle) >= 0 + return haystack.includes(needle) } - super(comparator, params) - this.params.type = 'to include' } getException() { - const params = this.params + const { params } = this params.jar = template(params.jar, params) const err = new AssertionFailedError(params, '{{customMessage}}expected {{jar}} {{type}} "{{needle}}"') err.expected = params.needle - err.actual = params.haystack - if (Array.isArray(this.params.haystack)) { - this.params.haystack = this.params.haystack.join('\n___(next element)___\n') - } - err.cliMessage = function () { + err.actual = Array.isArray(params.haystack) ? params.haystack.join('\n___(next element)___\n') : params.haystack + + err.cliMessage = () => { const msg = this.template .replace('{{jar}}', output.colors.bold('{{jar}}')) .replace('{{needle}}', output.colors.bold('{{needle}}')) - return template(msg, this.params) + return template(msg, params) } + return err } @@ -49,27 +52,26 @@ class InclusionAssertion extends Assertion { getFailedNegation() { this.params.type = 'not to include' const err = this.getException() - const pattern = new RegExp(`^.*?\n?^.*?\n?^.*?${escapeRegExp(this.params.needle)}.*?$\n?.*$\n?.*$`, 'm') - const matched = this.params.haystack.match(pattern) - if (!matched) return err - err.actual = matched[0].replace(this.params.needle, output.colors.bold(this.params.needle)) - err.actual = `------\n${err.actual}\n------` + const needlePattern = new RegExp(`^.*?\n?^.*?\n?^.*?${escapeRegExp(this.params.needle)}.*?$\n?.*$\n?.*$`, 'm') + const matched = this.params.haystack.match(needlePattern) + + if (matched) { + err.actual = `------\n${matched[0].replace(this.params.needle, output.colors.bold(this.params.needle))}\n------` + } + return err } - addAssertParams() { - this.params.needle = arguments[0] - this.params.haystack = arguments[1] - this.params.customMessage = arguments[2] ? `${arguments[2]}\n\n` : '' + addAssertParams(needle, haystack, customMessage = '') { + this.params.needle = needle + this.params.haystack = haystack + this.params.customMessage = customMessage ? `${customMessage}\n\n` : '' } } module.exports = { Assertion: InclusionAssertion, - includes: (needleType) => { - needleType = needleType || 'string' - return new InclusionAssertion({ jar: needleType }) - }, + includes: (needleType = 'string') => new InclusionAssertion({ jar: needleType }), fileIncludes: (file) => new InclusionAssertion({ file, jar: 'file {{file}}' }), } diff --git a/test/helper/webapi.js b/test/helper/webapi.js index 7d01fb482..cb4333f69 100644 --- a/test/helper/webapi.js +++ b/test/helper/webapi.js @@ -99,7 +99,7 @@ module.exports.tests = function () { it('should check text on site', async () => { await I.amOnPage('/') await I.see('Welcome to test app!') - await I.see('A wise man said: "debug!"') + await I.see('A wise man said: "debug!!"') await I.dontSee('Info') }) From 8aa6c05976e4afeaf1f6bfd989abb023e77eb736 Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Mon, 23 Sep 2024 14:46:26 +0200 Subject: [PATCH 2/2] improve assert include --- test/helper/webapi.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/helper/webapi.js b/test/helper/webapi.js index cb4333f69..7d01fb482 100644 --- a/test/helper/webapi.js +++ b/test/helper/webapi.js @@ -99,7 +99,7 @@ module.exports.tests = function () { it('should check text on site', async () => { await I.amOnPage('/') await I.see('Welcome to test app!') - await I.see('A wise man said: "debug!!"') + await I.see('A wise man said: "debug!"') await I.dontSee('Info') })