-
-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No CSS output in snapshots when using preact #80
Comments
Hello @silverlight513, thanks for the interesting issue. Can you please provide a working example to repro the issue? I tried to run: and added this to my Jest config: "moduleNameMapper": {
"^react$": "preact-compat",
"^react-dom$": "preact-compat",
"^create-react-class$": "preact-compat/lib/create-react-class"
} but it doens't work (I get Thank you very much! |
Hey @MicheleBertoli, I've created a basic repo with a single component and snapshot test in it for you to look at. https://github.com/silverlight513/preact-jest-concept I thought that it might be the |
Thank you very much for providing an example, @silverlight513. It was really helpful. I can confirm the cause of the issue the way I'm going to leave this open because I'd be happy to fix the issue. |
I came across this package https://github.com/nathancahill/preact-render-to-json which I was hoping would solve the issue but it doesn't have any affect at all. If I have some free I'd like to have a go at trying to fix the issue but I have no idea of how I'd go about it. |
@gnarf and I have been taking a look at this today and found a sort of work around to get this working. The way jest-styled-components checks if an element is a vdom node or not looks like what's stopping us get context to the styled-component stylesheet. Here's what we've had to do as the work around - https://github.com/silverlight513/preact-jest-concept/compare/gnarf @MicheleBertoli, would you be opposed to a PR containing a change in how jest-styled-components tests if an element is a valid vdom node. Most likely something akin to how it's done here - https://github.com/mzgoddard/preact-render-spy/blob/master/snapshot.js#L17 |
Hello @silverlight513 @gnarf. I gave a quick look and it seems that:
I think if we figure out why the components have both |
Hey @MicheleBertoli Unlike React, Preact supports the use of both Correct me if I'm wrong but I thought the point of snapshots were that they failed if something changed in a component that was being tested? Is it more that the hash shouldn't be there and only the |
Thank you very much for the explanation, @silverlight513. Given Preact supports You are perfectly right: the purpose of snapshot testing is telling you when something changes. |
Quick update, @silverlight513. If it'll be merged, then we only need to apply the following diff to this package and it'll support Preact. @@ -82,8 +82,8 @@ const replaceHashes = (result, hashes) =>
hashes.reduce(
(acc, className) =>
acc.replace(
- new RegExp(`(className="[^"]*?)${className}\\s?([^"]*")`, 'g'),
- '$1$2'
+ new RegExp(`((class|className)="[^"]*?)${className}\\s?([^"]*")`, 'g'),
+ '$1$3'
),
result
) |
Merged and released as 3.6.6. |
Thank you very much, @nathancahill. @silverlight513 I updated the package and it works. |
I've recently switched a project from React to use Preact. When the switch happened, I noticed that the snapshots no longer contained any CSS, only the HTML with their classnames.
The app itself works perfectly with styled-components as I've aliased React for preact-compat. I assumed this module would have the same results in both React and Preact.
The text was updated successfully, but these errors were encountered: