Using store types & typing initialState
inside of tests
#2788
Unanswered
Sensanaty
asked this question in
Help and Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm running into a situation where testing is becoming a tad annoying, since we lose all type information in test setups when using
initialState
.What I've opted for is a custom type that lets me assert the type for each individual store, however this is quite cumbersome as the type isn't very legible and results in a lot of unnecessary info that is hard to debug. It also involves importing each store that is being configured, even if they're not used inside the tests themselves which is sometimes the case (for example, a config store that determines some behavior in a component but doesn't get changed in the component).
My solution looks something like this
Then in tests where I need to setup some pinia state
This does work, however the type itself ends up being super complex and basically impossible to understand.
What I did is add a key,
THIS_SHOULDNT_BE_HERE: boolean
to theinitialState
, like so:This is the resulting type error I get:
Perhaps I'm just bad at reading/parsing TS errors, but to me this is completely illegible, and if I weren't the one that implemented the
Store
type, I'd have absolutely no clue what was going on here and I'd have no idea how to fix it.Is there a better solution that I'm perhaps missing here? The reason I want to have these typed is so we can catch situations where, for example, a store's state type gets changed, and the one in
initialData
is no longer being set. Sure, you would probably catch this during the test run, but I'd rather TS itself pick it up for me and warn me before I ever even have to run any tests.An alternative option could be to simply expose in each store an interface/type that would match the state, however this is very manual and repetitive, and if you're using CompAPI syntax for your stores rather than the OptionsAPI syntax, it's also a pretty annoying pattern.
What I mean is this
This is IMO not a great solution, especially if the pieces of state are of more complex types themselves.
So I'm curious, is there a good solution for this?
Beta Was this translation helpful? Give feedback.
All reactions