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
* fix: allow css file from http(s) source
Allows user to specify a css file at an absolute path, rather than just relative paths from the
build folder
* docs: updating the docs for how to load styling
Copy file name to clipboardExpand all lines: README.md
+35-21Lines changed: 35 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
# Testing Library Visualizer
2
2
3
3
## Motivation
4
+
4
5
[React Testing Library](https://testing-library.com/docs/react-testing-library/intro/) and the whole testing library family of tools has made testing frontends more rigorous and much simpler than it used to be. However, one of the persistent challenges when writing these tests is their debug-ability. When a test fails, testing library dumps the HTML into the console. It's often difficult to parse, and difficult to understand what made the test fail. Furthermore, interactively building up tests is complicated.
5
6
6
7
The goal of this library is to make writing and debugging tests iteratively easier by showing you what your test is doing, and allowing you to interact with it.
@@ -9,14 +10,12 @@ The goal of this library is to make writing and debugging tests iteratively easi
9
10
10
11
Install the package via:
11
12
12
-
```npm install testing-library-visualizer```
13
+
`npm install testing-library-visualizer`
13
14
14
-
If you want to have your app's styling and assets available while debugging then you should build the application and put the following in a jest setup file. If you're using Create React App the following can go into `setupTests.js`. If you don't have a file that sets up the jest context, you can [specify one](https://jestjs.io/docs/configuration#setupfiles-array).
15
+
If you want to have your app's styling and assets available while debugging then you should build the application and put the following in a jest setup file. If you're using Create React App the following can go into `setupTests.js`. If you don't have a file that sets up the jest context, you can [specify one](https://jestjs.io/docs/configuration#setupfiles-array).
@@ -27,6 +26,13 @@ setup(path.join(__dirname, "..", "build")); // This should point to wherever you
27
26
registerCommands({ screen, within, fireEvent, userEvent, expect }); // This should include any commands you want to run. See the custom command section below.
28
27
```
29
28
29
+
For users using Create React App we automatically use the generated `asset-manifest.json` file to:
30
+
31
+
1. Determine the compiled CSS file for the application
32
+
2. Translate requests for assets (images etc) so the debug view pulls from the correct URL
33
+
34
+
If you're not using CRA these things may not work out of the box. Feel free to create an issue as we'd like to support many different setups.
35
+
30
36
## Debugging a test
31
37
32
38
Once setup, debugging a test is simple. In the test file with the test you want to debug add:
From this interface you can run commands to interact with your test. Specifically, you can run the following built in commands:
62
65
63
66
```
64
67
highlight
65
68
refresh
69
+
console
66
70
```
67
71
68
72
And if you configured your test setup as specified above you'll have access to:
@@ -72,6 +76,7 @@ screen
72
76
within
73
77
userEvent
74
78
fireEvent
79
+
expect
75
80
```
76
81
77
82
Highlight and refresh are defined by Testing Library Visualizer. `highlight` takes a HTML element(s) and draws boxes around them. This is a good way to understand what components you're selecting with your testing library commands. `refresh` asks for the latest state of the application. This is useful when an operation might take some time to complete, and you want to see the most up to date version of the component you're testing.
@@ -82,10 +87,8 @@ Using these commands you can build up a full test interactively.
82
87
83
88
All errors are caught by the library and shown via the interactive editor. This enables you to see why commands failed while building up your test.
The library also reads from console.error and reports any errors printed out while running a command. These errors cannot be linked to a specific line, since they happen asynchronously, but the library reports them to the user as soon as it detects them. This is especially useful for catching [act warnings](https://kentcdodds.com/blog/fix-the-not-wrapped-in-act-warning).
90
93
91
94
## History
@@ -94,32 +97,44 @@ The library maintains the list of commands run in the upper read only code edito
94
97
95
98
## Restarting Tests
96
99
97
-
Clicking the `Reset Test` button will clear the dom and rerun the test code specified in the test file. This can be useful if you have lost track of the state of your test and would like to start over. Note, this doesn't actually cause Jest to rerun the test. Any changes made to the test code or underlying component will not be picked up. To get those changes you need to stop the test by clicking the `Stop Test` button and restarting it with Jest. Furthermore, if there is any other state in the test, that is not reset which can cause errors. (i.e. no before or after blocks are run.)
100
+
Clicking the `Reset Test` button will clear the dom and rerun the test code specified in the test file. This can be useful if you have lost track of the state of your test and would like to start over. Note, this doesn't actually cause Jest to rerun the test. Any changes made to the test code or underlying component will not be picked up. To get those changes you need to stop the test by clicking the `Stop Test` button and restarting it with Jest. Furthermore, if there is any other state in the test, clicking reset test will not clear that state which can cause errors. (i.e. no before or after blocks are run when you reset the test.)
98
101
99
102
## Custom Styling
100
103
101
-
In many projects there are multiple sources for styling. While the default looks for a `manifest.json` generated by create react app's build process, you can add styling from other sources as well.
104
+
In many projects there are multiple sources for styling. While by default we look for a `manifest.json` generated by Create React App's build process, you can add styling from other sources as well.
102
105
103
106
To do this you'll need to add the following to your jest test setup file.
You can specify a URL hosted by a server, or a URL that points to the static build assets defined by the `setup` command above. This means you can just copy a CSS file into your build folder, and specify the URL path using the `registerStyling` function to pick it up.
114
+
You can specify a URL hosted by a server, or a path that points to the static built assets in a directory defined by the `setup` command above. This means you can just copy a CSS file into your build folder, and specify the path using the `registerStyling` function to pick it up.
115
+
116
+
Specifying a file in the build directory would look something like this:
117
+
118
+
```
119
+
registerStyling("static/css/test.css")
120
+
```
121
+
122
+
Where the build folder has sub folders `static/css` and where `test.css` is in that folder.
It must start with an `http` or `https` to be considered an absolute URL.
114
131
115
132
## Custom Commands
116
133
117
134
In many projects you'll define custom commands that you will want to run in the interactive debug mode. To add commands you'll need to add the following to you jest setup file.
"Could not find asset-manifest.json, this likely means you haven't run `npm run build` or you haven't called the setup function. Run `npm run build` in order to see styling and assets."
@@ -117,15 +118,20 @@ export function replaceFilePaths(html, manifest) {
0 commit comments