jss icon indicating copy to clipboard operation
jss copied to clipboard

Css properties order are different between nodejs versions

Open justbrody opened this issue 7 years ago • 10 comments

Expected behavior: I've set up snapshot-testing for CSS that is generated by cssinjs. When there is no change made in the source, I expect to have the same css-snapshot output on different nodejs environments.

Describe the bug: Sadly the snapshots do differ on different nodejs version. The order of the properties in a stylesheet are different between node 10 and node 11.

Codesandbox link: I don't know how to reproduce it as a codesandbox. But I have created a project on github. The project has one simple react component and a snapshot test for the component. The snapshot is also committed and is made with nodejs 10. Then I created two Pull-Request one with node9 and one with node11. You can see that nodejs 11 has a failed snapshot-test with some properties on different positions compared with the snapshot of node 10.

Snapshot testcase on GitHub

Versions (please complete the following information):

  • jss: [email protected]
  • Browser [e.g. chrome, safari]:Not in the browser, testing in node
  • OS [e.g. Windows, macOS]:Linux/Windows/iMac Feel free to add any additional versions which you may think are relevant to the bug.

justbrody avatar Apr 09 '19 18:04 justbrody

The nodejs 11 has an updated v8 engine, which has a changed implementation of Array.sort https://github.com/nodejs/node/issues/24294

justbrody avatar Apr 09 '19 18:04 justbrody

We have discussed this on gitter. Our assumption is that between those node versions a different V8 version is used which has a change in sort implementation https://github.com/nodejs/node/issues/24294

We use sort in props-sort plugin

kof avatar Apr 09 '19 18:04 kof

We are basically sorting based on property name, I haven't learned yet how and why stable/unstable sort is influencing thouse. Anyone wants to look into it?

kof avatar Apr 09 '19 18:04 kof

This is the reason for the problem: here the order is only different for style property names with the same length.

We sort the props with the same length by the call order of the sort. I think we should compare the string when they have the same length.

HenriBeck avatar Apr 09 '19 18:04 HenriBeck

I checked in my production code and it is always the case when the length is the same.

justbrody avatar Apr 09 '19 18:04 justbrody

@justbrody for you a fix would be to align your node versions across the project, for example via https://github.com/creationix/nvm or https://github.com/Schniz/fnm

We will try out the fix with the comparing strings when the props have the same length

HenriBeck avatar Apr 09 '19 18:04 HenriBeck

Thanks, a fix comparing the strings sounds good.

I've setup nvm today on my machine and that is a good solution for us. I'll inform my collegues tomorrow. Thank you for the time and help.

justbrody avatar Apr 09 '19 18:04 justbrody

If I can help, please let me know.

justbrody avatar Apr 09 '19 18:04 justbrody

#1084 will fix your problem. I added some logic to return 1 or -1 when the strings have the same length to ensure consistent sorting between different node versions.

HenriBeck avatar Apr 10 '19 11:04 HenriBeck

Thank you @HenriBeck, I will keep an eye on the PR and see in with version it will be release.

justbrody avatar Apr 10 '19 13:04 justbrody