Skip to content

Commit c944c0c

Browse files
committed
docs: add section on blackbox and whitebox component testing
1 parent dfbc234 commit c944c0c

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/guide/scaling-up/testing.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ We will briefly discuss what each of these are, how they can be implemented for
3030

3131
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).
3232

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.
3434

3535
Take for example this `increment` function:
3636

@@ -67,13 +67,31 @@ describe('increment', () => {
6767
})
6868
```
6969

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.
7171

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
7380

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

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+
7795
### Recommendation
7896

7997
- [Vitest](https://vitest.dev/)
@@ -96,7 +114,7 @@ In Vue applications, components are the main building blocks of the UI. Componen
96114

97115
Component tests should catch issues relating to your component's props, events, slots that it provides, styles, classes, lifecycle hooks, and more.
98116

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.
100118

101119
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**.
102120

0 commit comments

Comments
 (0)