@@ -308,7 +308,7 @@ const Component = {
308
308
template: ' <div><global-component/></div>'
309
309
}
310
310
311
- test (' installs a component globally ' , () => {
311
+ test (' global.components ' , () => {
312
312
const wrapper = mount (Component , {
313
313
global: {
314
314
components: {
@@ -352,7 +352,7 @@ const Component = {
352
352
template: ' <div v-bar>Foo</div>'
353
353
}
354
354
355
- test (' installs a directive globally ' , () => {
355
+ test (' global.directives ' , () => {
356
356
const wrapper = mount (Component, {
357
357
global: {
358
358
directives: {
@@ -378,7 +378,7 @@ mixins?: ComponentOptions[]
378
378
` Component.spec.js ` :
379
379
380
380
``` js
381
- test (' adds a mixin ' , () => {
381
+ test (' global.mixins ' , () => {
382
382
const wrapper = mount (Component, {
383
383
global: {
384
384
mixins: [mixin]
@@ -424,7 +424,7 @@ export default {
424
424
` Component.spec.js ` :
425
425
426
426
``` js
427
- test (' mocks a vuex store ' , async () => {
427
+ test (' global. mocks' , async () => {
428
428
const $store = {
429
429
dispatch: jest .fn ()
430
430
}
@@ -460,7 +460,7 @@ plugins?: (Plugin | [Plugin, ...any[]])[]
460
460
``` js
461
461
import myPlugin from ' @/plugins/myPlugin'
462
462
463
- test (' installs a plugin ' , () => {
463
+ test (' global.plugins ' , () => {
464
464
mount (Component, {
465
465
global: {
466
466
plugins: [myPlugin]
@@ -474,7 +474,7 @@ To use plugin with options, an array of options can be passed.
474
474
` Component.spec.js ` :
475
475
476
476
``` js
477
- test (' installs plugins with and without options' , () => {
477
+ test (' global. plugins with options' , () => {
478
478
mount (Component, {
479
479
global: {
480
480
plugins: [Plugin , [PluginWithOptions, ' argument 1' , ' another argument' ]]
@@ -519,7 +519,7 @@ export default {
519
519
` Component.spec.js ` :
520
520
521
521
``` js
522
- test (' injects dark theme via provide mounting option ' , () => {
522
+ test (' global. provide' , () => {
523
523
const wrapper = mount (Component, {
524
524
global: {
525
525
provide: {
@@ -562,35 +562,52 @@ renderStubDefaultSlot?: boolean
562
562
563
563
Defaults to ** false** .
564
564
565
- Due to technical limitations, this behavior cannot be extended to slots other than the default one.
565
+ ` Component.vue `
566
566
567
- ``` js
568
- import { config , mount } from ' @vue/test-utils'
567
+ ``` vue
568
+ <template>
569
+ <slot />
570
+ <another-component />
571
+ </template>
569
572
570
- beforeAll (() => {
571
- config .renderStubDefaultSlot = true
572
- })
573
+ <script>
574
+ export default {
575
+ components: {
576
+ AnotherComponent
577
+ }
578
+ }
579
+ </script>
580
+ ```
573
581
574
- afterAll (() => {
575
- config .renderStubDefaultSlot = false
576
- })
582
+ ` AnotherComponent.vue `
577
583
578
- test (' shallow with stubs' , () => {
579
- const Component = {
580
- template: ` <div><slot /></div>`
581
- }
584
+ ``` vue
585
+ <template>
586
+ <p>Another component content</p>
587
+ </template>
588
+ ```
582
589
583
- const wrapper = mount (Component, {
584
- shallow: true
590
+ ` Component.spec.js `
591
+
592
+ ``` js
593
+ test (' global.renderStubDefaultSlot' , () => {
594
+ const wrapper = mount (ComponentWithSlots, {
595
+ slots: {
596
+ default: ' <div>My slot content</div>'
597
+ },
598
+ shallow: true ,
599
+ global: {
600
+ renderStubDefaultSlot: true
601
+ }
585
602
})
586
603
587
- expect (wrapper .html ()).toContain (' Content from the default slot' )
604
+ expect (wrapper .html ()).toBe (
605
+ ' <div>My slot content</div><another-component-stub></another-component-stub>'
606
+ )
588
607
})
589
608
```
590
609
591
- ::: tip
592
- This behavior is global, not on a mount by mount basis. Remember to enable/disable it before and after each test.
593
- :::
610
+ Due to technical limitations, ** this behavior cannot be extended to slots other than the default one** .
594
611
595
612
#### global.stubs
596
613
@@ -625,7 +642,7 @@ export default {
625
642
` Component.spec.js ` :
626
643
627
644
``` js
628
- test (' stubs a component using an array' , () => {
645
+ test (' global. stubs using array syntax ' , () => {
629
646
const wrapper = mount (Component, {
630
647
global: {
631
648
stubs: [' Foo' ]
@@ -635,7 +652,7 @@ test('stubs a component using an array', () => {
635
652
expect (wrapper .html ()).toEqual (' <div><foo-stub></div>' )
636
653
})
637
654
638
- test (' stubs a component using an Object boolean syntax' , () => {
655
+ test (' global. stubs using object syntax' , () => {
639
656
const wrapper = mount (Component, {
640
657
global: {
641
658
stubs: { Foo: true }
@@ -645,18 +662,19 @@ test('stubs a component using an Object boolean syntax', () => {
645
662
expect (wrapper .html ()).toEqual (' <div><foo-stub></div>' )
646
663
})
647
664
648
- test (' stubs a component using a custom component' , () => {
649
- const FooMock = {
650
- name: ' Foo ' ,
651
- template: ' <p>FakeFoo </p>'
665
+ test (' global. stubs using a custom component' , () => {
666
+ const CustomStub = {
667
+ name: ' CustomStub ' ,
668
+ template: ' <p>custom stub content </p>'
652
669
}
670
+
653
671
const wrapper = mount (Component, {
654
672
global: {
655
- stubs: { Foo: FooMock }
673
+ stubs: { Foo: CustomStub }
656
674
}
657
675
})
658
676
659
- expect (wrapper .html ()).toEqual (' <div><p>FakeFoo </p></div>' )
677
+ expect (wrapper .html ()).toEqual (' <div><p>custom stub content </p></div>' )
660
678
})
661
679
```
662
680
@@ -674,19 +692,28 @@ shallow?: boolean
674
692
675
693
Defaults to ** false** .
676
694
677
- ``` js
678
- test (' stubs all components automatically using { shallow: true }' , () => {
679
- const Component = {
680
- template: `
681
- <a-component />
682
- <another-component />
683
- ` ,
684
- components: {
685
- AComponent,
686
- AnotherComponent
687
- }
695
+ ` Component.vue `
696
+
697
+ ``` vue
698
+ <template>
699
+ <a-component />
700
+ <another-component />
701
+ </template>
702
+
703
+ <script>
704
+ export default {
705
+ components: {
706
+ AComponent,
707
+ AnotherComponent
688
708
}
709
+ }
710
+ </script>
711
+ ```
689
712
713
+ ` Component.spec.js `
714
+
715
+ ``` js
716
+ test (' shallow' , () => {
690
717
const wrapper = mount (Component, { shallow: true })
691
718
692
719
expect (wrapper .html ()).toEqual (
@@ -776,7 +803,9 @@ Returns an array of classes on an element.
776
803
test (' classes' , () => {
777
804
const wrapper = mount (Component)
778
805
779
- expect (wrapper .find (' .my-span' ).classes ()).toContain (' my-span' )
806
+ expect (wrapper .classes ()).toContain (' my-span' )
807
+ expect (wrapper .classes (' my-span' )).toBe (true )
808
+ expect (wrapper .classes (' not-existing' )).toBe (false )
780
809
})
781
810
` ` `
782
811
@@ -815,12 +844,12 @@ export default {
815
844
test (' emitted' , () => {
816
845
const wrapper = mount (Component)
817
846
847
+ // wrapper.emitted() equals to { greet: [ ['hello'], ['goodbye'] ] }
848
+
818
849
expect (wrapper .emitted ()).toHaveProperty (' greet' )
819
- expect (wrapper .emitted ().greet ).toHaveLength (2 ' )
850
+ expect (wrapper .emitted ().greet ).toHaveLength (2 )
820
851
expect (wrapper .emitted ().greet [0 ]).toEqual ([' hello' ])
821
852
expect (wrapper .emitted ().greet [1 ]).toEqual ([' goodbye' ])
822
-
823
- // { greet: [ [' hello' ], [' goodbye' ] ] }
824
853
})
825
854
` ` `
826
855
@@ -836,13 +865,13 @@ exists(): boolean
836
865
837
866
**Details:**
838
867
868
+ You can use the same syntax ` querySelector` implements.
869
+
839
870
` Component .vue ` :
840
871
841
872
` ` ` vue
842
873
< template>
843
- <div>
844
- <span />
845
- </div>
874
+ < span / >
846
875
< / template>
847
876
` ` `
848
877
@@ -1283,8 +1312,7 @@ setData(data: Record<string, any>): Promise<void>
1283
1312
1284
1313
**Details:**
1285
1314
1286
- Notice that ` setData` does not allow setting new properties that are not
1287
- defined in the component.
1315
+ ` setData` does not allow setting new properties that are not defined in the component.
1288
1316
1289
1317
Also, notice that ` setData` does not modify composition API ` setup ()` data.
1290
1318
@@ -1681,3 +1709,7 @@ it('uses global config', () => {
1681
1709
console .log (wrapper .html ()) // <p>message</p><div></div>
1682
1710
})
1683
1711
```
1712
+
1713
+ ::: tip
1714
+ Remember that this behavior is global, not on a mount by mount basis. You might need to enable/disable it before and after each test.
1715
+ :::
0 commit comments