Skip to content

Commit 2f8613d

Browse files
author
Pooya Parsa
committed
test: fix jest utils to return a function for error messages
1 parent 6b3838f commit 2f8613d

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

tests/utils.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,18 @@ const throwIfNotVueInstance = vm => {
5656
// debugging breadcrumbs in case a non-Vue instance gets erroneously passed
5757
// makes the error easier to fix than example: "Cannot read _prevClass of undefined"
5858
console.error(vm)
59-
throw new TypeError(`The matcher function expects Vue instance. Given ${typeof vm}`)
59+
throw new TypeError(
60+
`The matcher function expects Vue instance. Given ${typeof vm}`
61+
)
6062
}
6163
}
6264

6365
const throwIfNotHTMLElement = el => {
6466
if (!isHTMLElement(el)) {
6567
console.error(el)
66-
throw new TypeError(`The matcher function expects an HTML Element. Given ${typeof el}`)
68+
throw new TypeError(
69+
`The matcher function expects an HTML Element. Given ${typeof el}`
70+
)
6771
}
6872
}
6973

@@ -93,17 +97,21 @@ const elHasClass = (el, className) => {
9397
* @param {string} className
9498
* @return {boolean}
9599
*/
96-
const hasClass = (node, className) => (isVueInstance(node) ? vmHasClass(node, className) : elHasClass(node, className))
100+
const hasClass = (node, className) =>
101+
isVueInstance(node)
102+
? vmHasClass(node, className)
103+
: elHasClass(node, className)
97104

98105
const getVmTag = vm => vm.$options._componentTag
99106
const getHTMLTag = el => String(el.tagName).toLowerCase()
100-
const getTagName = node => (isVueInstance(node) ? getVmTag(node) : getHTMLTag(node))
107+
const getTagName = node =>
108+
isVueInstance(node) ? getVmTag(node) : getHTMLTag(node)
101109

102110
// Extend Jest marchers
103111
expect.extend({
104112
toHaveClass (node, className) {
105113
return {
106-
message: `expected <${getTagName(node)}> to have class '${className}'`,
114+
message: () => `expected <${getTagName(node)}> to have class '${className}'`,
107115
pass: hasClass(node, className)
108116
}
109117
},
@@ -127,25 +135,29 @@ expect.extend({
127135

128136
return {
129137
// more debugging breadcrumbs
130-
message: `Expected <${tagName}> to have all classes in [${classStr}], but was missing [${missingClassStr}] class${plural
131-
? 'es'
132-
: ''}.`,
138+
message: () => `Expected <${tagName}> to have all classes in [${classStr}], but was missing [${missingClassStr}] class${
139+
plural ? 'es' : ''
140+
}.`,
133141
pass
134142
}
135143
},
136144
toBeComponent (vm, componentTag) {
137145
throwIfNotVueInstance(vm)
138146

139147
return {
140-
message: `Expected to be <${componentTag}>. Received: ${getVmTag(vm)}`,
148+
message: () =>
149+
`Expected to be <${componentTag}>. Received: ${getVmTag(vm)}`,
141150
pass: getVmTag(vm) === componentTag
142151
}
143152
},
144153
toBeElement (el, tagName) {
145154
throwIfNotHTMLElement(el)
146155

147156
return {
148-
message: `Expected to be <${String(tagName).toLowerCase()}>. Received: ${el.tagName.toLowerCase()}`,
157+
message: () =>
158+
`Expected to be <${String(
159+
tagName
160+
).toLowerCase()}>. Received: ${el.tagName.toLowerCase()}`,
149161
pass: el.tagName === String(tagName).toUpperCase()
150162
}
151163
}

0 commit comments

Comments
 (0)