Skip to content

Commit 3e85d6e

Browse files
authored
fix: pretty print wrapper.html() output (#510)
Mimics v1 output by using "pretty" on HTML output before returning it from a wrapper. Fixes #498
1 parent cd1b708 commit 3e85d6e

File tree

7 files changed

+167
-43
lines changed

7 files changed

+167
-43
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@rollup/plugin-replace": "^2.4.2",
2222
"@types/jest": "26.0.22",
2323
"@types/node": "14.14.37",
24+
"@types/pretty": "^2.0.0",
2425
"@vue/compiler-sfc": "3.0.11",
2526
"babel-jest": "^26.6.3",
2627
"babel-preset-jest": "^26.6.2",
@@ -31,6 +32,7 @@
3132
"jsdom-global": "^3.0.2",
3233
"lint-staged": "^10.5.4",
3334
"prettier": "^2.2.1",
35+
"pretty": "^2.0.0",
3436
"reflect-metadata": "^0.1.13",
3537
"rollup": "^2.44.0",
3638
"rollup-plugin-typescript2": "^0.30.0",

src/vueWrapper.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ComponentPublicInstance, nextTick, App } from 'vue'
22
import { ShapeFlags } from '@vue/shared'
33
// @ts-ignore todo - No DefinitelyTyped package exists for this
44
import eventTypes from 'dom-event-types'
5+
import pretty from 'pretty'
56

67
import { config } from './config'
78
import { DOMWrapper } from './domWrapper'
@@ -90,10 +91,10 @@ export class VueWrapper<T extends ComponentPublicInstance>
9091
html() {
9192
// cover cases like <Suspense>, multiple root nodes.
9293
if (this.parentElement['__vue_app__']) {
93-
return this.parentElement.innerHTML
94+
return pretty(this.parentElement.innerHTML)
9495
}
9596

96-
return this.element.outerHTML
97+
return pretty(this.element.outerHTML)
9798
}
9899

99100
find<K extends keyof HTMLElementTagNameMap>(

tests/html.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ describe('html', () => {
2424

2525
const wrapper = mount(Component)
2626

27-
expect(wrapper.html()).toBe('<div>foo</div><div>bar</div><div>baz</div>')
27+
expect(wrapper.html()).toBe(
28+
'<div>foo</div>\n' + '<div>bar</div>\n' + '<div>baz</div>'
29+
)
2830
})
2931

3032
it('returns the html when mounting a Suspense component', () => {

tests/mountingOptions/global.components.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ describe('global.components', () => {
4141
expect(spy).not.toHaveBeenCalled()
4242
spy.mockRestore()
4343
expect(wrapper.html()).toBe(
44-
`<div><global-component-stub></global-component-stub></div>`
44+
'<div>\n' +
45+
' <global-component-stub></global-component-stub>\n' +
46+
'</div>'
4547
)
4648
})
4749

tests/mountingOptions/stubs.global.spec.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('mounting options: stubs', () => {
3434
}
3535
})
3636

37-
expect(wrapper.html()).toBe('<div></div><foo-stub></foo-stub>')
37+
expect(wrapper.html()).toBe('<div></div>\n' + '<foo-stub></foo-stub>')
3838
})
3939

4040
// https://github.com/vuejs/vue-test-utils-next/issues/249
@@ -57,7 +57,7 @@ describe('mounting options: stubs', () => {
5757
})
5858

5959
expect(wrapper.html()).toBe(
60-
'<div><foo-stub></foo-stub><a></a><span></span></div>'
60+
'<div>\n' + ' <foo-stub></foo-stub><a></a><span></span>\n' + '</div>'
6161
)
6262
expect(wrapper.getComponent(RouterLinkStub).vm.to).toBe('/foo')
6363
})
@@ -79,7 +79,7 @@ describe('mounting options: stubs', () => {
7979
})
8080

8181
expect(wrapper.html()).toEqual(
82-
'<div><functional-foo-stub></functional-foo-stub></div>'
82+
'<div>\n' + ' <functional-foo-stub></functional-foo-stub>\n' + '</div>'
8383
)
8484
})
8585

@@ -94,7 +94,9 @@ describe('mounting options: stubs', () => {
9494
}
9595
})
9696

97-
expect(wrapper.html()).toEqual('<div><foo-stub></foo-stub></div>')
97+
expect(wrapper.html()).toEqual(
98+
'<div>\n' + ' <foo-stub></foo-stub>\n' + '</div>'
99+
)
98100
})
99101

100102
it('passes all attributes to stubbed components', () => {
@@ -116,7 +118,9 @@ describe('mounting options: stubs', () => {
116118
})
117119

118120
expect(wrapper.html()).toEqual(
119-
'<div><foo-stub class="bar" test-id="foo" dynamic="[object Object]"></foo-stub></div>'
121+
'<div>\n' +
122+
' <foo-stub class="bar" test-id="foo" dynamic="[object Object]"></foo-stub>\n' +
123+
'</div>'
120124
)
121125
})
122126

@@ -141,7 +145,7 @@ describe('mounting options: stubs', () => {
141145
}
142146
})
143147

144-
expect(wrapper.html()).toBe('<div></div><foo-stub></foo-stub>')
148+
expect(wrapper.html()).toBe('<div></div>\n' + '<foo-stub></foo-stub>')
145149
})
146150

147151
it('prevents lifecycle hooks triggering in a stub', () => {
@@ -236,7 +240,7 @@ describe('mounting options: stubs', () => {
236240

237241
expect(created).not.toHaveBeenCalled()
238242
expect(wrapper.html()).toBe(
239-
'<div id="root"><div id="msg">Hello world</div></div>'
243+
'<div id="root">\n' + ' <div id="msg">Hello world</div>\n' + '</div>'
240244
)
241245
})
242246

@@ -274,7 +278,7 @@ describe('mounting options: stubs', () => {
274278
}
275279
})
276280

277-
expect(wrapper.html()).toBe('<span></span><div></div>')
281+
expect(wrapper.html()).toBe('<span></span>\n' + '<div></div>')
278282
})
279283

280284
it('stubs a component with a kabeb-case name', () => {
@@ -341,7 +345,9 @@ describe('mounting options: stubs', () => {
341345
const wrapper = mount(Comp)
342346

343347
expect(wrapper.html()).toBe(
344-
'<transition-stub><div id="content"></div></transition-stub>'
348+
'<transition-stub>\n' +
349+
' <div id="content"></div>\n' +
350+
'</transition-stub>'
345351
)
346352
})
347353

@@ -440,7 +446,9 @@ describe('mounting options: stubs', () => {
440446
}
441447
})
442448
expect(wrapper.html()).toBe(
443-
`<div><component-with-slots-stub>Default</component-with-slots-stub></div>`
449+
'<div>\n' +
450+
' <component-with-slots-stub>Default</component-with-slots-stub>\n' +
451+
'</div>'
444452
)
445453
})
446454

@@ -452,7 +460,9 @@ describe('mounting options: stubs', () => {
452460
}
453461
})
454462
expect(wrapper.html()).toBe(
455-
'<div><component-with-slots-stub></component-with-slots-stub></div>'
463+
'<div>\n' +
464+
' <component-with-slots-stub></component-with-slots-stub>\n' +
465+
'</div>'
456466
)
457467
})
458468

@@ -485,10 +495,10 @@ describe('mounting options: stubs', () => {
485495
})
486496

487497
expect(wrapper.html()).toEqual(
488-
'<component-with-slots-stub>' +
489-
'<component-with-slots-stub>' +
490-
'<simple-slot-stub>nested content</simple-slot-stub>' +
491-
'</component-with-slots-stub>' +
498+
'<component-with-slots-stub>\n' +
499+
' <component-with-slots-stub>\n' +
500+
' <simple-slot-stub>nested content</simple-slot-stub>\n' +
501+
' </component-with-slots-stub>\n' +
492502
'</component-with-slots-stub>'
493503
)
494504
})

tests/shallowMount.spec.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,25 @@ describe('shallowMount', () => {
4040
it('stubs all components automatically using { shallow: true }', () => {
4141
const wrapper = mount(ComponentWithChildren, { shallow: true })
4242
expect(wrapper.html()).toEqual(
43-
'<div class="ComponentWithChildren">' +
44-
'<hello-stub></hello-stub>' +
45-
'<component-with-input-stub></component-with-input-stub>' +
46-
'<component-without-name-stub></component-without-name-stub>' +
47-
'<script-setup-stub></script-setup-stub>' +
48-
'<with-props-stub></with-props-stub>' +
43+
'<div class="ComponentWithChildren">\n' +
44+
' <hello-stub></hello-stub>\n' +
45+
' <component-with-input-stub></component-with-input-stub>\n' +
46+
' <component-without-name-stub></component-without-name-stub>\n' +
47+
' <script-setup-stub></script-setup-stub>\n' +
48+
' <with-props-stub></with-props-stub>\n' +
4949
'</div>'
5050
)
5151
})
5252

5353
it('stubs all components automatically using shallowMount', () => {
5454
const wrapper = shallowMount(ComponentWithChildren)
5555
expect(wrapper.html()).toEqual(
56-
'<div class="ComponentWithChildren">' +
57-
'<hello-stub></hello-stub>' +
58-
'<component-with-input-stub></component-with-input-stub>' +
59-
'<component-without-name-stub></component-without-name-stub>' +
60-
'<script-setup-stub></script-setup-stub>' +
61-
'<with-props-stub></with-props-stub>' +
56+
'<div class="ComponentWithChildren">\n' +
57+
' <hello-stub></hello-stub>\n' +
58+
' <component-with-input-stub></component-with-input-stub>\n' +
59+
' <component-without-name-stub></component-without-name-stub>\n' +
60+
' <script-setup-stub></script-setup-stub>\n' +
61+
' <with-props-stub></with-props-stub>\n' +
6262
'</div>'
6363
)
6464
})
@@ -73,12 +73,12 @@ describe('shallowMount', () => {
7373
}
7474
})
7575
expect(wrapper.html()).toEqual(
76-
'<div class="ComponentWithChildren">' +
77-
'<div>Override</div>' +
78-
'<component-with-input-stub></component-with-input-stub>' +
79-
'<component-without-name-stub></component-without-name-stub>' +
80-
'<script-setup-stub></script-setup-stub>' +
81-
'<with-props-stub></with-props-stub>' +
76+
'<div class="ComponentWithChildren">\n' +
77+
' <div>Override</div>\n' +
78+
' <component-with-input-stub></component-with-input-stub>\n' +
79+
' <component-without-name-stub></component-without-name-stub>\n' +
80+
' <script-setup-stub></script-setup-stub>\n' +
81+
' <with-props-stub></with-props-stub>\n' +
8282
'</div>'
8383
)
8484
})
@@ -93,10 +93,10 @@ describe('shallowMount', () => {
9393
}
9494
})
9595
expect(wrapper.html()).toEqual(
96-
'<div>Override</div>' +
97-
'<component-with-input-stub></component-with-input-stub>' +
98-
'<stub></stub>' +
99-
'<stub></stub>' +
96+
'<div>Override</div>\n' +
97+
'<component-with-input-stub></component-with-input-stub>\n' +
98+
'<stub></stub>\n' +
99+
'<stub></stub>\n' +
100100
'<with-props-stub></with-props-stub>'
101101
)
102102
})

0 commit comments

Comments
 (0)