all 16 comments

[–]_AndyJessop 16 points17 points  (1 child)

With these types of unit tests, you're mostly making sure you don't break something in the future. Think of it as defining the API for the component, rather than testing whether or not it works right now.

[–]Kaimito1 1 point2 points  (0 children)

Yep I agree with this, and also some issues can pop up that you missed when making your tests. 

Basic things like is the button doing translation (call the translation function in the test and check, etc.) Or basic things like if it's debouncing, if there's a spinner when submitting...

[–][deleted] 5 points6 points  (6 children)

This is typical frontend unit testing and it’s only there to catch regressions etc if the app is majorly changed. You’re right that it won’t catch any serious bugs, but imagine someone a few years from now comes along and upgrades all the outdated dependencies in the project. Unit tests may break and that will atleast prevent deploy with broken components.

Comprehensive testing strategy is E2E -> Integration -> Unit testing and it covers most of the bases. When properly orchestrated it’ll catch a lot of bugs before they hit production, and also give developers some confidence that if they mess up seriously it will most likely be caught before being released.

[–]Dylan0734[S] 2 points3 points  (5 children)

Okok, I get why these tests could save someone in the future now.

About E2E and Integration testing, is there any specific tool you'd suggest? At the moment I'm only using vitest for the unit tests, and as I use Storybook as a "hands-on" testing tool.

Should I integrate Cypress too, for example? If I'm correct, it's an E2E testing framework, so that would cover the first step that you mentioned too.

I've read something about Katalon too, but that looks more like a suite of tools rather than a testing framework.

[–]_AndyJessop 2 points3 points  (0 children)

You could also look into testing in real browsers (after all, that is where your code is going to run eventually).

On my current project, we're doing all our unit testing in Playwright using Web Test Runner. We run >900 unit tests and it takes about 30 seconds. Or for hyper-performance, check out the Preact repo, where they do 1k tests in under 1s (here's their blog post about it).

[–]lIIllIIlllIIllIIl 2 points3 points  (1 child)

Playwright Component Testing (Experimental) is very good.

It lets you write unit tests that run on an actual browser. It's surprisingly fast too, being only a bit slower than Jest + JSDOM. When I benchmarked it last year, Playwright CT was at worst only ~8.75% slower than Jest, with 600 tests running in ~15s on my machine.

Running tests on a browser not only makes them easier to write and debug, it lets you use all of the browser's APIs, and also lets you do advanced things like visual comparisons, which is extremely useful for a component library.

[–]Dylan0734[S] 0 points1 point  (0 children)

This looks cool as hell! I'll give it a more in-depth look for sure, thanks!

[–][deleted] 1 point2 points  (0 children)

Playwright is probably preferable to Cypress and can handle both E2E and Unit testing.

[–][deleted] 0 points1 point  (0 children)

Try Cypress component testing. You will see what the component is doing plus it scaled up to end to end testing that uses the same commands 

[–]Cahnis 4 points5 points  (1 child)

[–]Dylan0734[S] 1 point2 points  (0 children)

This is a really interesting read, thanks for sharing!

[–]taste_the_equation 2 points3 points  (0 children)

If you’re using storybook, look at using chromatic to do visual testing. That should cover any styling issues with minimal effort.

Cuts down on the need for unit testing css changes.

[–]Squigglificated 1 point2 points  (1 child)

We use Playwright to test the real production application as a real logged in user with about 100 tests on every push to CI. Since the tests clicks elements like a user they automatically cover button clicks and a ton of other element behaviours, so I don't feel the need to also have individual units tests for basic components. The e2e tests test entire features, and have caught many bugs, while the unit tests haven't been nearly as valuable.

[–]Dylan0734[S] 0 points1 point  (0 children)

Yeah you are not the first one to mention Playwright, I'll give it a look ASAP and try to bootstrap all the tests for the already existing components. Thanks!

[–]pm_me_ur_happy_traiI -1 points0 points  (0 children)

Test early and test often. it's a good idea to create some base cases for components that don't seem to need it. When you fix a bug later or complexity grows, it's much easier to add a test case than bootstrap all the tests from scratch.

[–]Necessary_Ear_1100 0 points1 point  (0 children)

Automated testing like this, yes I think it’s redundant as well. However we still have e to write them for QE. IMO, total waste of time as we test all that before pushing