Unit testing Linaria-based components #152
Replies: 4 comments 3 replies
-
|
note: I'm hoping to spend some time looking into whether we can get Jest performing the necessary CSS extraction as it runs tests or not. If not, I'll look at whether it's easier to do with Vite/Vitest. |
Beta Was this translation helpful? Give feedback.
-
|
I've been able to get it working with Vite + Vitest, but not Jest, though I find Jest much harder to configure than Vitest, so maybe it's still possible. With Vitest, since it uses Vite to process each test file, we kind of get all the behaviour we need out of the box. A quick 5 minute POC let me achieve the testing of the new TextArea (see #150 and #151) that I would have expected to be able to get via Jest. In particular, the
I'm not sure if we're open to switching from Jest to Vitest though... |
Beta Was this translation helpful? Give feedback.
-
|
@willmcvay Keen to hear your thoughts on the above when you have a chance. I notice that we currently rely on:
All of which involve their own configs. Not sure if this is just some technical debt in Elements or not; are we wanting to consolidate on a single toolchain for builds across Storybook, production assets and our tests? |
Beta Was this translation helpful? Give feedback.
-
|
@kurtdoherty We moved the Go app repo to vitest after some tests showed it was faster. Once we moved the larger sections of code we found noticeably slower performance in Github builds (nearly twice as long) but local test speed is faster. Vitest in github builds does not seem to like 'barrel files' with 'export * from my/dir'. It takes longer to process and a test showed that cleaning dependency structures can help. DOM tests may also be slower. We reverted the biggest chunk of code back to jest to speed up the builds and plan to clean up dependency structures and try Vitest there again. There are numerous articles online about Vitest being slower after being told it was quicker but if Jest is not being actively updated then it seems likely that Jest will be replaced in a similar way to enzyme being replaced by testing-library |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
While implementing #150 and #151, I ran up against some unexpected constraints in the unit tests for
TextAreawhen using React Testing Library (RTL) to check whether the component behaved as I expected.<textarea>element the component renders viascreen.getByRole('textbox');Investigating these a bit further:
styledandcssutilities. It appears as though we can remove these safely, though we'll need update every single snapshot because the HTML tags and CSS class names captured will no longer have themock-styled.ormock-css.prefixes. 🥳So (1) is easy to solve, but (2) does not appear as straightforward. I haven't been able to find much documentation around testing with Linaria, so it's hard to know if there's something simple I'm missing or not.
Why is this a problem? Personally, I want to be able to sufficiently test shared UI components without resorting to browser-based tests, and at the moment, it seems like we can't do that with Linaria 😬 To give a concrete (though contrived) example:
Running the above unit test will result in the following failure. You can see that our
<button>is correctly rendered (with issue 1 above fixed locally), but theclassattribute is a fake class name, not the real one and thus, the dynamic styles are not being applied and we cannot assert the correct behaviour ofisVisuallyHidden😞I'm very interested to know if anyone else is concerned about what this means for our ability to unit test component behaviour that cannot be captured via simplistic DOM snapshots. I suspect this is probably a small part of a larger conversation around testing practices in Elements and what kind of coverage we expect to have for our components.
Beta Was this translation helpful? Give feedback.
All reactions