41
41
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
42
42
43
43
- [ Installation] ( #installation )
44
- - [ A simple example] ( #a-simple -example )
44
+ - [ A basic example] ( #a-basic -example )
45
45
- [ More examples] ( #more-examples )
46
+ - [ Guiding Principles] ( #guiding-principles )
46
47
- [ Docs] ( #docs )
47
48
- [ Typings] ( #typings )
48
49
- [ ESLint support] ( #eslint-support )
@@ -67,10 +68,10 @@ npm install --save-dev @testing-library/vue
67
68
This library has ` peerDependencies ` listings for ` Vue ` and
68
69
` vue-template-compiler ` .
69
70
70
- You may also be interested in installing ` jest-dom ` so you can use
71
- [ the custom Jest matchers] ( https://github.com/testing-library/ jest-dom#readme ) .
71
+ You may also be interested in installing ` jest-dom ` so you can use [ the custom
72
+ Jest matchers] [ jest-dom ] .
72
73
73
- ## A simple example
74
+ ## A basic example
74
75
75
76
``` html
76
77
<!-- TestComponent.vue -->
@@ -83,6 +84,7 @@ You may also be interested in installing `jest-dom` so you can use
83
84
84
85
<script >
85
86
export default {
87
+ name: ' Button' ,
86
88
data : () => ({
87
89
count: 0 ,
88
90
}),
@@ -96,29 +98,42 @@ You may also be interested in installing `jest-dom` so you can use
96
98
```
97
99
98
100
``` js
99
- // TestComponent.spec.js
100
- import {render , fireEvent } from ' @testing-library/vue'
101
- import TestComponent from ' ./TestComponent.vue'
101
+ import {render , screen , fireEvent } from ' @testing-library/vue'
102
+ import Button from ' ./Button'
102
103
103
104
test (' increments value on click' , async () => {
104
- // The render method returns a collection of utilities to query the component.
105
- const {getByText } = render (TestComponent)
105
+ // The `render` method renders the component into the document.
106
+ // It also binds to `screen` all the available queries to interact with
107
+ // the component.
108
+ render (Button)
106
109
107
- // getByText returns the first matching node for the provided text, and
108
- // throws an error if no elements match or if more than one match is found .
109
- getByText ( ' Times clicked: 0' )
110
+ // queryByText returns the first matching node for the provided text
111
+ // or returns null .
112
+ expect ( screen . queryByText ( ' Times clicked: 0' )). toBeTruthy ( )
110
113
111
- // `button` is the actual DOM element.
112
- const button = getByText (' increment' )
114
+ // getByText returns the first matching node for the provided text
115
+ // or throws an error.
116
+ const button = screen .getByText (' increment' )
113
117
114
- // Dispatch a couple of native click events .
118
+ // Click a couple of times .
115
119
await fireEvent .click (button)
116
120
await fireEvent .click (button)
117
121
118
- getByText ( ' Times clicked: 2' )
122
+ expect ( screen . queryByText ( ' Times clicked: 2' )). toBeTruthy ( )
119
123
})
120
124
```
121
125
126
+ > You might want to install jest-dom[ jest-dom] to add handy assertions such as
127
+ > ` .toBeInTheDocument() ` :
128
+ > ` expect(screen.queryByText('Times clicked: 0')).toBeInTheDocument() ` .
129
+
130
+ > Using ` byText ` queries it's not the only nor the best way to query for
131
+ > elements. Read
132
+ > [ Which query should I use?] ( https://testing-library.com/docs/guide-which-query )
133
+ > to discover alternatives. In the example above,
134
+ > ` getByRole('button', {name: 'increment'}) ` is possibly the best option to get
135
+ > the button element.
136
+
122
137
### More examples
123
138
124
139
You'll find examples of testing with different situations and popular libraries
@@ -134,6 +149,27 @@ Some included are:
134
149
135
150
Feel free to contribute with more examples!
136
151
152
+ ## Guiding Principles
153
+
154
+ > [ The more your tests resemble the way your software is used, the more
155
+ > confidence they can give you.] [ guiding-principle ]
156
+
157
+ We try to only expose methods and utilities that encourage you to write tests
158
+ that closely resemble how your Vue components are used.
159
+
160
+ Utilities are included in this project based on the following guiding
161
+ principles:
162
+
163
+ 1 . If it relates to rendering components, it deals with DOM nodes rather than
164
+ component instances, nor should it encourage dealing with component
165
+ instances.
166
+ 2 . It should be generally useful for testing individual Vue components or
167
+ full Vue applications.
168
+ 3 . Utility implementations and APIs should be simple and flexible.
169
+
170
+ At the end of the day, what we want is for this library to be pretty
171
+ light-weight, simple, and understandable.
172
+
137
173
## Docs
138
174
139
175
[ ** Read the docs** ] [ docs ] | [ Edit the docs] [ docs-edit ]
@@ -215,6 +251,8 @@ instead of filling an issue on GitHub.
215
251
[ license ] : https://github.com/testing-library/vue-testing-library/blob/master/LICENSE
216
252
[ types ] : https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/testing-library__vue
217
253
[ discord ] : https://testing-library.com/discord
254
+ [ jest-dom ] : https://github.com/testing-library/jest-dom
255
+ [ guiding-principle ] : https://twitter.com/kentcdodds/status/977018512689455106
218
256
219
257
[ docs ] : https://testing-library.com/vue
220
258
[ docs-edit ] : https://github.com/testing-library/testing-library-docs
0 commit comments