Skip to content

Commit 4e30df5

Browse files
committed
improve assert include
1 parent 641e674 commit 4e30df5

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

lib/assert/include.js

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,36 @@ const output = require('../output')
66
const MAX_LINES = 10
77

88
class InclusionAssertion extends Assertion {
9-
constructor(params) {
9+
constructor(params = {}) {
1010
params.jar = params.jar || 'string'
11-
const comparator = function (needle, haystack) {
11+
super(InclusionAssertion.createComparator(), params)
12+
this.params.type = 'to include'
13+
this.template = undefined
14+
}
15+
16+
static createComparator() {
17+
return (needle, haystack) => {
1218
if (Array.isArray(haystack)) {
13-
return haystack.filter((part) => part.indexOf(needle) >= 0).length > 0
19+
return haystack.some((part) => part.includes(needle))
1420
}
15-
return haystack.indexOf(needle) >= 0
21+
return haystack.includes(needle)
1622
}
17-
super(comparator, params)
18-
this.params.type = 'to include'
1923
}
2024

2125
getException() {
22-
const params = this.params
26+
const { params } = this
2327
params.jar = template(params.jar, params)
2428
const err = new AssertionFailedError(params, '{{customMessage}}expected {{jar}} {{type}} "{{needle}}"')
2529
err.expected = params.needle
26-
err.actual = params.haystack
27-
if (Array.isArray(this.params.haystack)) {
28-
this.params.haystack = this.params.haystack.join('\n___(next element)___\n')
29-
}
30-
err.cliMessage = function () {
30+
err.actual = Array.isArray(params.haystack) ? params.haystack.join('\n___(next element)___\n') : params.haystack
31+
32+
err.cliMessage = () => {
3133
const msg = this.template
3234
.replace('{{jar}}', output.colors.bold('{{jar}}'))
3335
.replace('{{needle}}', output.colors.bold('{{needle}}'))
34-
return template(msg, this.params)
36+
return template(msg, params)
3537
}
38+
3639
return err
3740
}
3841

@@ -49,27 +52,26 @@ class InclusionAssertion extends Assertion {
4952
getFailedNegation() {
5053
this.params.type = 'not to include'
5154
const err = this.getException()
52-
const pattern = new RegExp(`^.*?\n?^.*?\n?^.*?${escapeRegExp(this.params.needle)}.*?$\n?.*$\n?.*$`, 'm')
53-
const matched = this.params.haystack.match(pattern)
54-
if (!matched) return err
55-
err.actual = matched[0].replace(this.params.needle, output.colors.bold(this.params.needle))
56-
err.actual = `------\n${err.actual}\n------`
55+
const needlePattern = new RegExp(`^.*?\n?^.*?\n?^.*?${escapeRegExp(this.params.needle)}.*?$\n?.*$\n?.*$`, 'm')
56+
const matched = this.params.haystack.match(needlePattern)
57+
58+
if (matched) {
59+
err.actual = `------\n${matched[0].replace(this.params.needle, output.colors.bold(this.params.needle))}\n------`
60+
}
61+
5762
return err
5863
}
5964

60-
addAssertParams() {
61-
this.params.needle = arguments[0]
62-
this.params.haystack = arguments[1]
63-
this.params.customMessage = arguments[2] ? `${arguments[2]}\n\n` : ''
65+
addAssertParams(needle, haystack, customMessage = '') {
66+
this.params.needle = needle
67+
this.params.haystack = haystack
68+
this.params.customMessage = customMessage ? `${customMessage}\n\n` : ''
6469
}
6570
}
6671

6772
module.exports = {
6873
Assertion: InclusionAssertion,
69-
includes: (needleType) => {
70-
needleType = needleType || 'string'
71-
return new InclusionAssertion({ jar: needleType })
72-
},
74+
includes: (needleType = 'string') => new InclusionAssertion({ jar: needleType }),
7375
fileIncludes: (file) => new InclusionAssertion({ file, jar: 'file {{file}}' }),
7476
}
7577

test/helper/webapi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ module.exports.tests = function () {
9999
it('should check text on site', async () => {
100100
await I.amOnPage('/')
101101
await I.see('Welcome to test app!')
102-
await I.see('A wise man said: "debug!"')
102+
await I.see('A wise man said: "debug!!"')
103103
await I.dontSee('Info')
104104
})
105105

0 commit comments

Comments
 (0)