Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve assert include #4507

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions lib/assert/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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}}' }),
}

Expand Down
Loading