Skip to content

Commit 64a12fd

Browse files
authored
fix: allow css file from http(s) source (#43)
* 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
1 parent b148e25 commit 64a12fd

File tree

3 files changed

+52
-27
lines changed

3 files changed

+52
-27
lines changed

README.md

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Testing Library Visualizer
22

33
## Motivation
4+
45
[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.
56

67
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
910

1011
Install the package via:
1112

12-
```npm install testing-library-visualizer```
13+
`npm install testing-library-visualizer`
1314

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).
1516

1617
```javascript
17-
import {
18-
setup,
19-
} from "testing-library-visualizer";
18+
import { setup } from "testing-library-visualizer";
2019
import path from "path";
2120
import { expect } from "@jest/globals";
2221
import { screen, within, fireEvent } from "@testing-library/react";
@@ -27,6 +26,13 @@ setup(path.join(__dirname, "..", "build")); // This should point to wherever you
2726
registerCommands({ screen, within, fireEvent, userEvent, expect }); // This should include any commands you want to run. See the custom command section below.
2827
```
2928

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+
3036
## Debugging a test
3137

3238
Once setup, debugging a test is simple. In the test file with the test you want to debug add:
@@ -49,20 +55,18 @@ debugTest("Test App", async () => {
4955

5056
When you run your test you'll see printed:
5157

52-
```Debug server is running, open at 127.0.0.1:3001```
58+
`Debug server is running, open at 127.0.0.1:3001`
5359

5460
Go to the URL and you should see the debug interface:
5561

56-
5762
https://user-images.githubusercontent.com/3885236/152245374-0a60bae9-974e-4d02-9979-001d2e8c9c8e.mp4
5863

59-
60-
6164
From this interface you can run commands to interact with your test. Specifically, you can run the following built in commands:
6265

6366
```
6467
highlight
6568
refresh
69+
console
6670
```
6771

6872
And if you configured your test setup as specified above you'll have access to:
@@ -72,6 +76,7 @@ screen
7276
within
7377
userEvent
7478
fireEvent
79+
expect
7580
```
7681

7782
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.
8287

8388
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.
8489

85-
8690
https://user-images.githubusercontent.com/3885236/152245412-e7f47199-a494-4c4d-bc0d-fbc82bc0a7fb.mp4
8791

88-
8992
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).
9093

9194
## History
@@ -94,32 +97,44 @@ The library maintains the list of commands run in the upper read only code edito
9497

9598
## Restarting Tests
9699

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.)
98101

99102
## Custom Styling
100103

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

103106
To do this you'll need to add the following to your jest test setup file.
104107

105108
```javascript
106-
import {
107-
registerStyling,
108-
} from "testing-library-visualizer";
109+
import { registerStyling } from "testing-library-visualizer";
109110

110111
registerStyling(/* <URL of styling> */);
111112
```
112113

113-
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.
123+
124+
Specifying a URL would look something like this:
125+
126+
```
127+
registerStyling("http://some-url.com/css/css-file.css)
128+
```
129+
130+
It must start with an `http` or `https` to be considered an absolute URL.
114131

115132
## Custom Commands
116133

117134
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.
118135

119136
```javascript
120-
import {
121-
registerCommands,
122-
} from "testing-library-visualizer";
137+
import { registerCommands } from "testing-library-visualizer";
123138

124139
registerCommands({
125140
test: () => {
@@ -129,4 +144,3 @@ registerCommands({
129144
```
130145

131146
You can pass an object to `registerCommands` where each key is the name of the command, and the value is the function it will run when invoked.
132-

package/src/testingUtil.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ var isListening = false;
8686
var manifest = {};
8787
var resetFunction = null;
8888

89-
async function getCssFiles() {
89+
function loadManifest() {
9090
const manifestFileLocation = path.join(buildDirectory, "asset-manifest.json");
9191

9292
if (fs.existsSync(manifestFileLocation)) {
9393
const rawdata = fs.readFileSync(manifestFileLocation);
9494
manifest = JSON.parse(rawdata).files;
95+
registerStyling(manifest["main.css"]);
9596
} else {
9697
console.warn(
9798
"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) {
117118
}
118119

119120
function addStyleLinks(html) {
120-
const cssFiles = [manifest["main.css"], ...registeredStyling];
121+
const cssFiles = registeredStyling.filter((cssFile) => cssFile);
121122
const parser = new DOMParser();
122123
const newDoc = parser.parseFromString(html, "text/html");
123124

124125
cssFiles.forEach((cssFile) => {
125126
const link = document.createElement("link");
126127
link.rel = "stylesheet";
127128
link.type = "text/css";
128-
link.href = `${ASSET_DIRECTORY}/` + cssFile;
129+
if (cssFile.indexOf("http://") === 0 || cssFile.indexOf("https://") === 0) {
130+
link.href = cssFile;
131+
} else {
132+
link.href = `${ASSET_DIRECTORY}/` + cssFile;
133+
}
134+
129135
newDoc.head.appendChild(link);
130136
});
131137

@@ -196,7 +202,7 @@ export const start = async (setupFunction) => {
196202
isListening = true;
197203
resetFunction = setupFunction;
198204

199-
await getCssFiles();
205+
loadManifest();
200206
await setupFunction();
201207
await fastify.listen(3001);
202208
console.log_without_reporting(

test-app/src/setupTests.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// learn more: https://github.com/testing-library/jest-dom
55
import "@testing-library/jest-dom";
66
import {
7-
registerStyling,
7+
// registerStyling,
88
setup,
99
registerCommands,
1010
} from "testing-library-visualizer";
@@ -14,6 +14,11 @@ import userEvent from "@testing-library/user-event";
1414

1515
setup(path.join(__dirname, "..", "build"));
1616

17-
registerStyling("static/css/test.css");
17+
// Example of importing styling placed in the build folder
18+
// registerStyling("static/css/test.css");
19+
// Example of importing styling from a URL
20+
// registerStyling(
21+
// "https://www.test-website.org/media/css/styling-file.css"
22+
// );
1823

1924
registerCommands({ screen, within, fireEvent, userEvent, expect });

0 commit comments

Comments
 (0)