getDefaultStore and testing without Provider #2747
-
Hello good people of Jotai world Recently we removed the Provider from our application. Doing so, some of the tests were started failing because the atom values are persisted(or previous value was kept is a better wording here) between the test blocks. We fixed the issue with broken tests by using getDefaultStore and reset necessary atoms to their initial values before each test. const defaultStore = getDefaultStore()
defaultStore.set(someAtom, someInitialValue) With that being said i have couple questions regarding this: 1 - When not using any Provider, does the approach that i mention above regarding resetting atoms to their initial state make sense? Is there any other approach that is a "better practice"? 2 - Is it possible to export the return type of example usage of why: // here the type is accessed via ReturnType and generic. Maybe its better with exported Store type?
export const _dev_resetSomeAtom = (defaultStore: ReturnType<typeof getDefaultStore>) => {
defaultStore.set(someAtom, initialAtom)
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
2: We generally prefer 1: Resetting the default store would be challenging. I wonder if something like jest.mock can do it. Otherwise, you solution would be only the way. This shouldn't be Jotai-only problem. I wonder what's the best practice for resetting module variable in general in tests. |
Beta Was this translation helpful? Give feedback.
2: We generally prefer
ReturnType<>
until we learn some unavoidable issues.1: Resetting the default store would be challenging. I wonder if something like jest.mock can do it. Otherwise, you solution would be only the way. This shouldn't be Jotai-only problem. I wonder what's the best practice for resetting module variable in general in tests.