Replies: 2 comments
-
|
Hi @romankoho! I'm Dosu and I’m helping the storybook team. The error
If you still see the error, double-check that:
References: Try these steps and see if the error resolves. If not, please share the exact import lines from both your To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
|
The issue is just how the hook is imported. Even though sb.mock() spies on the module at runtime, if you do: import { useUser } from '../src/hooks/useUser'TypeScript still sees useUser as a normal function, not a mock, so mocked(useUser) fails. You need to import the whole module instead: import * as userHook from '../src/hooks/useUser'Then mock it via the module: beforeEach(() => {
vi.spyOn(userHook, 'useUser').mockReturnValue({
user: { name: 'The mocked name' },
})
})That way TypeScript and Storybook agree that useUser is actually a mock, and the error disappears. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Hello,
I am trying to setup storybook with particular attention to testing.
Obviously I've read the documentation.
I got MSW working for mocking away API calls.
Next up I want to mock away a custom hook but I just can't get it right.
I created a minimal reproduction repo. It includes a simple hook
in the preview.ts I setup the mock
in the story I want to define the return value
this is the error I'm getting:

I guess it's something easy and stupid but maybe someone can help me (please).
Again here is the repo
Additional information
https://github.com/romankoho/storybook-test/tree/master
Create a reproduction
No response
Beta Was this translation helpful? Give feedback.
All reactions