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: docs/testing/cypress-component-testing.md
+21-7Lines changed: 21 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
---
2
-
title: Real-world component testing
2
+
title: Cypress component testing
3
3
category: Testing
4
-
order: 5
4
+
order: 3
5
5
---
6
6
7
-
# Real-world component testing
7
+
# Cypress component testing
8
8
9
9
Sometimes unit test behaviour doesn't match how our components work in the browser (e.g. no ResizeObserver)
10
-
InstUI uses [Cypress Component Testing](https://docs.cypress.io/guides/component-testing/overview)for these cases. These are located at `instructure-ui/cypress/component/`.
10
+
InstUI uses [Cypress Component Testing](https://docs.cypress.io/guides/component-testing/overview)in these cases to run tests in a real-world environment.
11
11
12
12
### Running tests
13
13
@@ -17,9 +17,14 @@ You can run them from the root with the following command:
17
17
npm run cy:component
18
18
```
19
19
20
+
Run specific test file:
21
+
```
22
+
npm run cy:component -- --spec "cypress/component/Alerts.cy.tsx"
23
+
```
20
24
### Creating new tests
21
25
22
-
New tests should be added under `instructure-ui/cypress/component/[ComponentName].cy.tsx`
26
+
New tests should be added under `cypress/component/`
27
+
By convention we name test files after the component they are testing like `cypress/component/[ComponentName].cy.tsx`
23
28
24
29
Cypress tests usually have a structure like this:
25
30
@@ -28,7 +33,7 @@ Cypress tests usually have a structure like this:
You can read more about cypress testing from their [docs](https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Writing-tests) and their [React Examples](https://docs.cypress.io/guides/component-testing/react/examples).
43
47
48
+
### Example
49
+
You can view our code base on GitHub. The component tests can be found [here](https://github.com/instructure/instructure-ui/tree/master/cypress/component).
50
+
51
+
44
52
### Debugging tests
45
53
46
54
To run cypress in a non-headless mode, use the following command:
This page provides an overview of the testing strategies we use to ensure the quality and stability of our components. We use a combination of modern tools to support our testing needs. Each tool serves a different purpose in our testing pyramid, from unit-level validations to full-blown visual and behavioral regression tests.
10
+
11
+
### Technologies Used:
12
+
13
+
#### Vitest + React Testing Library:
14
+
15
+
These tools are our primary stack for writing component-level unit tests. They are lightweight and fast. [Vitest](https://vitest.dev/guide/) is one of the fastest modern testing framework. It offers a Jest-like API and runs in a Node.js environment, making it ideal for testing individual components and functions in isolation. Paired with [React Testing Library](https://testing-library.com/docs/react-testing-library/intro), Vitest encourages accessible and maintainable test practices by querying elements the way users interact with them. It's best suited for testing component logic, rendering conditions, and props/state changes without needing a full browser environment. For a deeper dive into Vitest unit testing, check out our [detailed guides and examples.](/#vitest-unit-testing)
16
+
17
+
#### Cypress Component Testing:
18
+
19
+
[Cypress Component Testing](https://docs.cypress.io/app/component-testing/get-started) allows you to mount individual components in a real browser environment for precise interaction testing. Unlike traditional unit tests, it renders with full CSS and browser APIs, offering more realistic behavior. This makes it ideal for testing user interactions like clicks, keyboard navigation, focus traps, and animations. While a bit heavier than Vitest, it provides greate visibility and debugging capabilities for complex UI logic. For a deeper dive into Cypress component testing, check out our [detailed guides and examples.](#cypress-component-testing)
20
+
21
+
#### Chromatic Visual Regression Testing with Cypress:
22
+
23
+
We use [Cypress](https://docs.cypress.io/app/tooling/visual-testing) end-to-end tests to generate structured layouts and capture screenshots of components. [Chromatic](https://www.chromatic.com/docs/diff-inspector/) handles the visual diffing and review process. It's especially effective for catching layout shifts, styling regressions, or broken UI elements that don’t trigger functional test failures. Tests are run after changes are pushed, comparing the new screenshots to a baseline stored from a previously verified state. When differences are detected, the test fails and provides side-by-side diffs for easy review. This type of testing is particularly valuable for pixel-perfect components, design systems, or any feature with strict visual requirements.
24
+
25
+
#### Cypress Behavioral Regression Testing:
26
+
27
+
[Cypress Behavioral Regression Testing](https://docs.cypress.io/app/end-to-end-testing/writing-your-first-end-to-end-test) focuses on simulating realistic user flows across complex components or pages to ensure key behaviors remain stable. These tests often replicate critical workflows like navigation, and keyboard interactions. They are useful after feature changes or refactors, as they verify that the components still behaves correctly from the user’s perspective. Since the tests run in a real browser, they provide visibility into full-stack behavior, including browser APIs. These more complex end-to-end style tests are helps catch integration issues or regressions that unit and visual tests may overlook.
0 commit comments