Skip to content

Why is my custom hook with Jotai atom returning undefined in tests? #2739

Answered by rothsandro
ArthurGuez asked this question in Q&A
Discussion options

You must be logged in to vote

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:

  1. You call vi.mock('@/services/config') with an empty mock function
  2. Vitest imports the module that contains the atom
  3. Importing the module will call getLocalConfig() to get the initial value (that is passed to atomWithStorage()). This calls the empty mock that you provided
  4. Vitest runs your test
  5. You call mockResolvedValue() which is now already too late, the function has already been called

If you don't need different mocks per test, you can simple define the val…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@ArthurGuez
Comment options

@rothsandro
Comment options

Answer selected by ArthurGuez
@ArthurGuez
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants