I'm trying to figure out how to test state change in a Stencil web component using jest. I'm new to testing in general and have found that you can you use Enzyme to test state change within React components but I haven't been able to figure out how to do it with a Stencil component. How would I go about doing this?
Asked
Active
Viewed 2,260 times
1 Answers
0
Generally, you are not recommended to test the internal logic of component (state) but test public API(props, events) instead.
But if you wish to test it anyway, I suggest you check Testing documentation on stencils original website. If by testing a component you mean test it by instantiating a component explicitly then it means you are going to test an instance of plain javascript class. So if your state variable marked as private (which is a best practice) then you will not able to compile it, since TS will throw compilation errors. So, as an option (and the only as I see now) you can make those state members public and check them in your expects().

Valikhan Akhmedov
- 961
- 1
- 10
- 14
-
2You can still access `private` Typescript properties by using `instance['propertyName']`, and if you wish just cast the result e.g `as IModelType`, to get the TS typings in your test – Drenai May 02 '20 at 09:52