-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Would it make sense to checkin Cypress e2e snapshots? #3491
Comments
Hey @elliot-nelson have you tried And I agree with all your points and as I am new to mermaid I'm unsure of the current state or history of e2e tests. I have been nervous to use them because I am get errors on a fresh checkout - not sure if this is normal or not. But it gives me pause. |
You read our minds :) We had a discussion about this a few days ago, copied below from #3459 (comment):
I've actually got a similar project that checks in snapshots in a git repo (remark-mermaid-dataurl), and it's a always a pain since the images are always a few percent different from my local PC compared to GitHub Actions. To avoid those issues, the current plan is two have two different types of visual regression/snapshot testing done only on images generated by GitHub Actions:
Since PR #3459, e2e tests have been fixed so that they all pass! We've even got a CI action that runs e2e tests: https://github.com/mermaid-js/mermaid/actions/workflows/e2e.yml |
Thanks for the detailed background! I'll admit I was coming from a corporate perspective and had not considered cost 😓 . There is cheaper storage available than LFS; for example, 50GB on S3 is $1.15/mo compared to GH LFS at $5/mo. Buuuut, LFS can magically show image diffs, which I think is a killer feature in a PR. Your other point is well taken though: I think you really can't do PR-uploaded snapshots unless you can dockerize your cypress setup and run it all inside a fixed environment, so that every developer produces an identical snapshot. (maybe a someday experiment!) |
Would it be help to offload some e2e tests into jest DOM tests? |
That is all awesome to hear! |
This is something we should do if it speeds up the tests. |
Even Docker might not be enough. ARM computers (e.g. one of those new Macs) often calculate shadows/shading very slightly differently from x86_64 computers (and it'll probably affect LoongArch CPUs too). I honestly wouldn't be surprised if your computer's GPU might also affect things. Hence why I feel like just letting CI handle it for you is so much easier 😉 It's probably also why all the visual regression SaaS companies can charge so much money.
100%. Jest (or vitest) unit tests are much much faster than Cypress. Jest (or vitest) uses JSDom, which doesn't support the CSS/layouting Mermaid needs, so most of the Mermaid rendering code needs to tested in a real browser engine, but everything else ideally should also be unit tested. |
Updating this comment because vitest addresses this in a much better way. I have what I think could be important. I was able to get jest unit tests working with jest challengesThe show stopper is that jest checks each ES module loaded by our tests transitively. If any loaded jest patch scriptI wrote a
to return false; Here is the script: #!/bin/bash
shouldLoadAsEsm="node_modules/jest-resolve/build/shouldLoadAsEsm.js"
line_number=87 # the source at line 71 ends up being at line 87 when distributed.
sed -i "''" "${line_number}s/return cachedLookup/return false/" ${shouldLoadAsEsm} ⇥ This patch unblocks us to test mermaid with jest and Using this I was able to fully unit test markers which I believe would obsolete 5 tests in |
I had a look at the new vitest looks like a great choice and it removes all the jest/ESM module loading issues that have been an obstacle AFAIK to unit testing. So my comment above about a jest workaround is now mute. vitest solves this in a much more robust way. |
So, with Here are some opportunities that I see:
|
I agree with of these 👍 Some comments I the image snapshot parts:
Cypress e2e image snapshots actually use the same library as the recommended library for vitest image snapshots! The only issue is that the latest version of
I tried to set this up with https://github.com/mermaid-js/mermaid-cli, and didn't have much success for two reasons:
|
Yes. The improvement I am foreseeing is not from the library but from running the tests via vitest rather than Cypress. I would like to experiment with this as I see Cypress tests taking ~ 1 second and vitest tests taking ~ 1 millisec. Having faster tests means developers would get faster feedback.
Good to know! Maybe fonts are why I can't get e2e tests to all pass on my iMac. It is complexity like this that motivates a decreasing reliance on image snapshots. My suggestion here was for data based snapshots (not image snapshots) where we could extract say the generated SVG as a data snapshot. So, lack of rendering support is not an issue. I submitted my first PR to mermaid. It removes mocks and enables both |
Generating the SVG still requires layout support, which JSDom does not yet support, see https://github.com/jsdom/jsdom#unimplemented-parts-of-the-web-platform (although I could be wrong! I last checked this a few years ago) For example, Mermaid might decide to place boxes side-by-side with narrow fonts, while they might be on top of each other for wider fonts. I'd love to see Mermaid render SVGs in Node.JS, though! Maybe it might be possible by combining JSDom and svgdom, and if we use a fixed embedded open-source font https://github.com/svgdotjs/svgdom#fonts.
Congrats 🚀 Removing mocks is always great (issues with mocks is a big reason why we switched from the faster |
I have results from using vitest non-image snapshots. So in a snapshot the cypress: 667 KB, 93 secs Snapshot File SizeThe 32% snapshot file size reduction is ok but modest, as you predicted @aloisklink. Further reduction is possible by selecting a subset of the document to snapshot and/or writing a custom snapshot serializer. I did not try either but both look straight forward and might be worth checking out. A more promising diet plan might be to refactor some cypress tests into smaller, more focused tests. For instance, a test that just includes the shapes the diagram supports. This would reduce the storage consumed by duplicate drawing elements in our current e2e test suite snapshots. I think the benefits are deeper but this would be a wider discussion especially as I don't have all the context and experience that you all have :). Test SpeedThe 150x speed improvement is really nice! Unit TestsThe main thing I learned from this is that by allowing actual DOM access in our tests we can write fast, understandable, focused tests without mocking. (To be clear, I am not dissing mocking. It has its place for sure.) I think this will make it easier to write unit tests so we should expect that devs would be more inclined to write them. And that would be good thing! |
|
Something to consider meanwhile: Perhaps something along the lines of: Update the current cypress snapshots with the develop branch. When you run the e2e (end-to-end) tests against your code in your branch, cypress will compare the screenshots generated with your code against these snapshots. Running this locally will ensure that things like diagrams are generated using the fonts on your system (instead of fonts used on the CI systems, for example). Here's how to do it:
pnpm cypress --env updateSnapshots=true
|
Help us help you!
After interacting with the e2e tests a little bit, I'm wondering whether you would want to checkin these snapshots.
Normally with these kinds of tests, I'd expect:
It doesn't seem like that is what's happening right now. (But, maybe I'm misunderstanding what's happening, or there's a reason it's not currently doing this?)
The text was updated successfully, but these errors were encountered: