Skip to content

Commit 247dd58

Browse files
committed
fix: check for element in text
1 parent a0bef79 commit 247dd58

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/wrappers/wrapper.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ export default class Wrapper implements BaseWrapper {
312312
* Return text of wrapper element
313313
*/
314314
text (): string {
315+
if (!this.element) {
316+
throwError('cannot call wrapper.text() on a wrapper without an element')
317+
}
318+
315319
return this.element.textContent
316320
}
317321

test/unit/specs/mount/Wrapper/text.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,13 @@ describe('text', () => {
99

1010
expect(wrapper.text()).to.equal(text)
1111
})
12+
13+
it('throws error if wrapper does not contain eleemnt', () => {
14+
const compiled = compileToFunctions(`<div />`)
15+
const wrapper = mount(compiled)
16+
wrapper.element = null
17+
const fn = () => wrapper.text()
18+
const message = '[vue-test-utils]: cannot call wrapper.text() on a wrapper without an element'
19+
expect(fn).to.throw().with.property('message', message)
20+
})
1221
})

0 commit comments

Comments
 (0)