Skip to content

Commit e7fd16f

Browse files
authored
docs: use useByRole in example and fix typos
getByRole validates the role of an element, giving us an extra free assertion https://testing-library.com/docs/queries/about/#priority
1 parent a6c8282 commit e7fd16f

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/guide/scaling-up/testing.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ When designing your Vue application's testing strategy, you should leverage the
2424
- **Component**: Checks that your component mounts, renders, can be interacted with, and behaves as expected. These tests import more code than unit tests, are more complex, and require more time to execute.
2525
- **End-to-end**: Checks features that span multiple pages and makes real network requests against your production-built Vue application. These tests often involve standing up a database or other backend.
2626

27-
Each testing type plays a role in your application's testing strategy and each will protect you against different types of issues.
27+
Each testing type plays a role in your application's testing strategy, and each will protect you against different types of issues.
2828

2929
## Overview {#overview}
3030

@@ -82,7 +82,7 @@ There are two instances where you DO unit test Vue-specific features:
8282

8383
### Composables {#composables}
8484

85-
One category of functions specific to Vue applications are [Composables](/guide/reusability/composables), which may require special handling during tests.
85+
One category of functions specific to Vue applications is [Composables](/guide/reusability/composables), which may require special handling during tests.
8686
See [Testing Composables](#testing-composables) below for more details.
8787

8888
### Unit Testing Components {#unit-testing-components}
@@ -141,7 +141,7 @@ const { getByText } = render(Stepper, {
141141

142142
getByText('0') // Implicit assertion that "0" is within the component
143143

144-
const button = getByText('increment')
144+
const button = getByRole('button', { name: /increment/i })
145145

146146
// Dispatch a click event to our increment button.
147147
await fireEvent.click(button)
@@ -209,7 +209,7 @@ cy.get(valueSelector).should('be.visible').and('contain.text', '0')
209209

210210
- [Vitest](https://vitest.dev/) for components or composables that render headlessly (e.g. the [`useFavicon`](https://vueuse.org/core/useFavicon/#usefavicon) function in VueUse). Components and DOM can be tested using [`@vue/test-utils`](https://github.com/vuejs/test-utils).
211211

212-
- [Cypress Component Testing](https://on.cypress.io/component) for components whose expected behavior depends on properly rendering styles or triggering native DOM events. Can be used with Testing Library via [@testing-library/cypress](https://testing-library.com/docs/cypress-testing-library/intro).
212+
- [Cypress Component Testing](https://on.cypress.io/component) for components whose expected behavior depends on properly rendering styles or triggering native DOM events. It can be used with Testing Library via [@testing-library/cypress](https://testing-library.com/docs/cypress-testing-library/intro).
213213

214214
The main differences between Vitest and browser-based runners are speed and execution context. In short, browser-based runners, like Cypress, can catch issues that node-based runners, like Vitest, cannot (e.g. style issues, real native DOM events, cookies, local storage, and network failures), but browser-based runners are _orders of magnitude slower than Vitest_ because they do open a browser, compile your stylesheets, and more. Cypress is a browser-based runner that supports component testing. Please read [Vitest's comparison page](https://vitest.dev/guide/comparisons.html#cypress) for the latest information comparing Vitest and Cypress.
215215

@@ -227,7 +227,7 @@ We recommend using `@vue/test-utils` for testing components in applications. `@t
227227

228228
- [Nightwatch](https://nightwatchjs.org/) is an E2E test runner with Vue Component Testing support. ([Example Project](https://github.com/nightwatchjs-community/todo-vue))
229229

230-
- [WebdriverIO](https://webdriver.io/docs/component-testing/vue) for cross browser component testing that relies on native user interaction based on standardised automation. Can also be used with Testing Library.
230+
- [WebdriverIO](https://webdriver.io/docs/component-testing/vue) for cross-browser component testing that relies on native user interaction based on standardized automation. It can also be used with Testing Library.
231231

232232
## E2E Testing {#e2e-testing}
233233

@@ -237,11 +237,11 @@ End-to-end tests focus on multi-page application behavior that makes network req
237237

238238
End-to-end tests will often catch issues with your router, state management library, top-level components (e.g. an App or Layout), public assets, or any request handling. As stated above, they catch critical issues that may be impossible to catch with unit tests or component tests.
239239

240-
End-to-end tests do not import any of your Vue application's code, but instead rely completely on testing your application by navigating through entire pages in a real browser.
240+
End-to-end tests do not import any of your Vue application's code but instead rely completely on testing your application by navigating through entire pages in a real browser.
241241

242-
End-to-end tests validate many of the layers in your application. They can either target your locally built application, or even a live Staging environment. Testing against your Staging environment not only includes your frontend code and static server, but all associated backend services and infrastructure.
242+
End-to-end tests validate many of the layers in your application. They can either target your locally built application or even a live Staging environment. Testing against your Staging environment not only includes your frontend code and static server but all associated backend services and infrastructure.
243243

244-
> The more your tests resemble the way your software is used, the more confidence they can give you. - [Kent C. Dodds](https://twitter.com/kentcdodds/status/977018512689455106) - Author of the Testing Library
244+
> The more your tests resemble how your software is used, the more confidence they can give you. - [Kent C. Dodds](https://twitter.com/kentcdodds/status/977018512689455106) - Author of the Testing Library
245245
246246
By testing how user actions impact your application, E2E tests are often the key to higher confidence in whether an application is functioning properly or not.
247247

@@ -251,19 +251,19 @@ While end-to-end (E2E) testing on the web has gained a negative reputation for u
251251

252252
#### Cross-browser testing {#cross-browser-testing}
253253

254-
One of the primary benefits that end-to-end (E2E) testing is known for is its ability to test your application across multiple browsers. While it may seem desirable to have 100% cross-browser coverage, it is important to note that cross browser testing has diminishing returns on a team's resources due the additional time and machine power required to run them consistently. As a result, it is important to be mindful of this trade-off when choosing the amount of cross-browser testing your application needs.
254+
One of the primary benefits that end-to-end (E2E) testing is known for is its ability to test your application across multiple browsers. While it may seem desirable to have 100% cross-browser coverage, it is important to note that cross browser testing has diminishing returns on a team's resources due to the additional time and machine power required to run them consistently. As a result, it is important to be mindful of this trade-off when choosing the amount of cross-browser testing your application needs.
255255

256256
#### Faster feedback loops {#faster-feedback-loops}
257257

258-
One of the primary problems with end-to-end (E2E) tests and development is that running the entire suite takes a long time. Typically, this is only done in continuous integration and deployment (CI/CD) pipelines. Modern E2E testing frameworks have helped to solve this by adding features like parallelization, which allows for CI/CD pipelines to often run magnitudes faster than before. In addition, when developing locally, the ability to selectively run a single test for the page you are working on while also providing hot reloading of tests can help to boost a developer's workflow and productivity.
258+
One of the primary problems with end-to-end (E2E) tests and development is that running the entire suite takes a long time. Typically, this is only done in continuous integration and deployment (CI/CD) pipelines. Modern E2E testing frameworks have helped to solve this by adding features like parallelization, which allows for CI/CD pipelines to often run magnitudes faster than before. In addition, when developing locally, the ability to selectively run a single test for the page you are working on while also providing hot reloading of tests can help boost a developer's workflow and productivity.
259259

260260
#### First-class debugging experience {#first-class-debugging-experience}
261261

262-
While developers have traditionally relied on scanning logs in a terminal window to help determine what went wrong in a test, modern end-to-end (E2E) test frameworks allow developers to leverage tools that they are already familiar with, e.g. browser developer tools.
262+
While developers have traditionally relied on scanning logs in a terminal window to help determine what went wrong in a test, modern end-to-end (E2E) test frameworks allow developers to leverage tools they are already familiar with, e.g. browser developer tools.
263263

264264
#### Visibility in headless mode {#visibility-in-headless-mode}
265265

266-
When end-to-end (E2E) tests are run in continuous integration / deployment pipelines, they are often run in headless browsers (i.e., no visible browser is opened for the user to watch). A critical feature of modern E2E testing frameworks is the ability to see snapshots and/or videos of the application during testing, providing some insight into why errors are happening. Historically, it was tedious to maintain these integrations.
266+
When end-to-end (E2E) tests are run in continuous integration/deployment pipelines, they are often run in headless browsers (i.e., no visible browser is opened for the user to watch). A critical feature of modern E2E testing frameworks is the ability to see snapshots and/or videos of the application during testing, providing some insight into why errors are happening. Historically, it was tedious to maintain these integrations.
267267

268268
### Recommendation {#recommendation-2}
269269

@@ -308,7 +308,7 @@ export default defineConfig({
308308
```
309309

310310
:::tip
311-
If you are using TypeScript, add `vitest/globals` to the `types` field in your `tsconfig.json`.
311+
If you use TypeScript, add `vitest/globals` to the `types` field in your `tsconfig.json`.
312312

313313
```json
314314
// tsconfig.json
@@ -322,7 +322,7 @@ If you are using TypeScript, add `vitest/globals` to the `types` field in your `
322322

323323
:::
324324

325-
Then create a file ending in `*.test.js` in your project. You can place all test files in a test directory in project root, or in test directories next to your source files. Vitest will automatically search for them using the naming convention.
325+
Then, create a file ending in `*.test.js` in your project. You can place all test files in a test directory in the project root or in test directories next to your source files. Vitest will automatically search for them using the naming convention.
326326

327327
```js
328328
// MyComponent.test.js
@@ -367,7 +367,7 @@ A composable depends on a host component instance when it uses the following API
367367
- Lifecycle hooks
368368
- Provide / Inject
369369

370-
If a composable only uses Reactivity APIs, then it can be tested by directly invoking it and asserting its returned state / methods:
370+
If a composable only uses Reactivity APIs, then it can be tested by directly invoking it and asserting its returned state/methods:
371371

372372
```js
373373
// counter.js
@@ -414,7 +414,7 @@ export function withSetup(composable) {
414414
})
415415
app.mount(document.createElement('div'))
416416
// return the result and the app instance
417-
// for testing provide / unmount
417+
// for testing provide/unmount
418418
return [result, app]
419419
}
420420
```

0 commit comments

Comments
 (0)