You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -53,15 +57,15 @@ If any of these assertions fail, it's clear that the issue is contained within t
53
57
import { increment } from './helpers'
54
58
55
59
describe('increment', () => {
56
-
it('increments the current number by 1', () => {
60
+
test('increments the current number by 1', () => {
57
61
expect(increment(0, 10)).toBe(1)
58
62
})
59
63
60
-
it('does not increment the current number over the max', () => {
64
+
test('does not increment the current number over the max', () => {
61
65
expect(increment(10, 10)).toBe(10)
62
66
})
63
67
64
-
it('has a default max of 10', () => {
68
+
test('has a default max of 10', () => {
65
69
expect(increment(10)).toBe(10)
66
70
})
67
71
})
@@ -87,7 +91,7 @@ A component can be tested in two ways:
87
91
88
92
1. Whitebox: Unit Testing
89
93
90
-
Tests that are "Whitebox tests" are aware of the implementation details and dependencies of a component. Components be unit tested by using [`@vue/test-utils`'s](https://test-utils.vuejs.org)`shallowMount` command instead of the `mount` command. You can also make use of the `global.stubs` API. Please read the Vue Test Utils docs for some help on [how to decide](https://test-utils.vuejs.org/guide/advanced/stubs-shallow-mount.html#mount-shallow-and-stubs-which-one-and-when) if you want to Unit Test or Component Test your components. As stated above, unit tests may mock initial state and large parts of your application, when unit testing components, this includes 3rd party components, libraries, and all child components.
94
+
Tests that are "Whitebox tests" are aware of the implementation details and dependencies of a component. Components mustmbe unit tested by using [`@vue/test-utils`'s](https://test-utils.vuejs.org)`shallowMount` command instead of the `mount` command. You can also make use of the `global.stubs` API. Please read the Vue Test Utils docs for some help on [how to decide](https://test-utils.vuejs.org/guide/advanced/stubs-shallow-mount.html#mount-shallow-and-stubs-which-one-and-when) if you want to Unit Test or Component Test your components. As stated above, unit tests may mock initial state and large parts of your application, when unit testing components, this includes 3rd party components, libraries, and all child components.
91
95
92
96
2. Blackbox: Component Testing
93
97
Tests that are "Blackbox tests" are unaware of the implementation details of a component. These tests do not mock anything. They render all child components and are considered more of an "integration test" in which Components are the Subject Under Test. We will cover this in the next section.
@@ -127,6 +131,56 @@ In the below example, we demonstrate a Stepper component that has a DOM element
127
131
128
132
We know nothing about the implementation of Stepper, only that the "input" is the `max` prop and the "output" is the state of the DOM as the user will see it.
129
133
134
+
<TestingApiSwitcher>
135
+
136
+
<divclass="testing-library-api">
137
+
138
+
```js
139
+
render(Stepper, {
140
+
props: {
141
+
max:1
142
+
}
143
+
})
144
+
145
+
const { getByText } =render(Component)
146
+
147
+
getByText('0') // Implicit assertion that "0" is within the component
148
+
149
+
constbutton=getByText('increment')
150
+
151
+
// Dispatch a click event to our increment button.
Assert the private state of a component instance or test the private methods of a component. Testing implementation details makes the tests brittle, as they are more likely to break and require updates when the implementation changes.
@@ -162,21 +212,13 @@ cy.get(valueSelector)
162
212
163
213
### Recommendation
164
214
165
-
-[Vitest](https://vitest.dev/) for components or composables that render headlessly. (e.g. the [`useFavicon`](https://vueuse.org/core/useFavicon/#usefavicon) function in VueUse.
215
+
-[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 [@testing-library/vue](https://testing-library.com/docs/vue-testing-library/intro).
166
216
167
-
-[Cypress Component Testing](https://on.cypress.io/component) for components whose expected behavior depends on properly rendering styles or triggering native DOM events.
217
+
-[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).
168
218
169
-
Cypress relies on Vue's `@vue/test-utils` library under the hood and is easily integrated with the Testing Library API via `@testing-library/cypress`. It works for both Vite-based and Webpack-based applications.
170
-
171
-
The main differences between Vitest and Cypress are speed and execution context. In short, Cypress can catch issues that Vitest cannot (style issues, real native DOM events, cookies, local storage, and network failures), but is *orders of magnitude slower than Vitest* because it opens a browser and compiles your stylesheets. **The Cypress team recommends Vitest if you want to trade speed for coverage.**
172
-
173
-
Please read [Vitest's comparison page](https://vitest.dev/guide/comparisons.html#cypress) for the latest information.
174
-
175
-
:::warning In Active Development
176
-
Cypress Component Testing is still undergoing development. The Mount API is identical to [@vue/test-utils](https://github.com/vuejs/vue-test-utils) and has been stable for over a year now, but the Cypress team has not taken the project out of Alpha yet. Like Vitest, it can utilize your Vite config. It also has a Webpack adapter for use with Vue CLI or other build setups.
177
-
:::
219
+
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 doopen 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.
178
220
179
-
### Libraries
221
+
### Mounting Libraries
180
222
181
223
Component testing often involves mounting the component being tested in isolation, triggering simulated user input events, and asserting on the rendered DOM output. There are dedicated utility libraries that make these tasks simpler.
0 commit comments