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
Copy file name to clipboardExpand all lines: src/guide/scaling-up/testing.md
+22-4Lines changed: 22 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ We will briefly discuss what each of these are, how they can be implemented for
30
30
31
31
Unit tests are written to verify that small, isolated units of code are working as expected. A unit test usually covers a single function, class, composable, or module. Unit tests focus on logical correctness and only concern themselves with a small portion of the application's overall functionality. They may mock large parts of your application's environment (e.g. initial state, complex classes, 3rd party modules, and network requests).
32
32
33
-
Unit tests will catch issues with a function's business logic and logical correctness.
33
+
In general, unit tests will catch issues with a function's business logic and logical correctness.
34
34
35
35
Take for example this `increment` function:
36
36
@@ -67,13 +67,31 @@ describe('increment', () => {
67
67
})
68
68
```
69
69
70
-
As mentioned previously, unit testing is typically applied to self-contained business logic, classes, modules, or functions that do not involve UI rendering, network requests, or other environmental concerns.
70
+
As mentioned previously, unit testing is typically applied to self-contained business logic, components, classes, modules, or functions that do not involve UI rendering, network requests, or other environmental concerns.
71
71
72
-
These are typically plain JavaScript / TypeScript modules unrelated to Vue. In general, writing unit tests in Vue applications does not differ significantly from applications using other frameworks.
72
+
These are typically plain JavaScript / TypeScript modules unrelated to Vue. In general, writing unit tests for business logic in Vue applications does not differ significantly from applications using other frameworks.
73
+
74
+
There are two instances where you DO unit test Vue-specific features:
75
+
76
+
1. Composables
77
+
2. Components
78
+
79
+
### Composables
73
80
74
81
One category of functions specific to Vue applications are [Composables](/guide/reusability/composables.html), which may require special handling during tests.
75
82
See [Testing Composables](#testing-composables) below for more details.
76
83
84
+
### Components
85
+
86
+
A component can be tested in two ways:
87
+
88
+
1. Whitebox: Unit Testing
89
+
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.
91
+
92
+
2. Blackbox: Component Testing
93
+
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.
94
+
77
95
### Recommendation
78
96
79
97
-[Vitest](https://vitest.dev/)
@@ -96,7 +114,7 @@ In Vue applications, components are the main building blocks of the UI. Componen
96
114
97
115
Component tests should catch issues relating to your component's props, events, slots that it provides, styles, classes, lifecycle hooks, and more.
98
116
99
-
They should not mock child components, but instead test the interactions between your component and its children by interacting with the components as a user would. For example, a component test should click on an element like a user would instead of programmatically interacting with the component.
117
+
Component tests should not mock child components, but instead test the interactions between your component and its children by interacting with the components as a user would. For example, a component test should click on an element like a user would instead of programmatically interacting with the component.
100
118
101
119
Component tests should focus on the component's public interfaces rather than internal implementation details. For most components, the public interface is limited to: events emitted, props, and slots. When testing, remember to **test what a component does, not how it does it**.
0 commit comments