Why is my custom hook with Jotai atom returning undefined in tests? #2739
Answered
by
rothsandro
ArthurGuez
asked this question in
Q&A
-
Hello there, I'm facing an issue with testing a custom hook that relies on an atomWithStorage initialized with a promise.
|
Beta Was this translation helpful? Give feedback.
Answered by
rothsandro
Sep 20, 2024
Replies: 1 comment 3 replies
-
Maybe, you are creating the configAtom before you mock the function. export const configAtom = atomWithStorage<Promise<Config | null>>('config', getLocalConfig());
// is equal to
const initialConfig = getLocalConfing(); // runs before `mockResolvedValue()`
export const configAtom = atomWithStorage<Promise<Config | null>>('config', initialConfig); |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's not related to promises and would happen with sync functions as well. As dai-shi explained, at the time you are calling
mockResolvedValue()
the config function has already been called and will therefore not be mocked anymore:vi.mock('@/services/config')
with an empty mock functiongetLocalConfig()
to get the initial value (that is passed toatomWithStorage()
). This calls the empty mock that you providedmockResolvedValue()
which is now already too late, the function has already been calledIf you don't need different mocks per test, you can simple define the val…