easy-peasy icon indicating copy to clipboard operation
easy-peasy copied to clipboard

mockActions:true calls thunks

Open macrozone opened this issue 4 years ago • 3 comments

i want to check whether a certain action / thunk is dispatched, so i set mockActions: true.

but i see that if the action is a thunk, it executes it.

Is this intentional? i think no thunk should be executed

macrozone avatar Mar 08 '21 14:03 macrozone

What exactly are you testing?

An action is not the same thing as a thunk. This is why when mocking actions, thunks aren't mocked. That property is meant for when you're testing thunks.

If you're testing components that utilize said thunks, you should mock the hooks that import them instead.

LuisOsta avatar Mar 09 '21 20:03 LuisOsta

@LuisOsta i am testing a hook that uses a easy-peasy action and i am only interested in whether this action is triggered. it happens to be a thunk action, but that does not matter as it is transparent for the caller

macrozone avatar Mar 10 '21 08:03 macrozone

Hey @macrozone , Oh okay, that makes sense! The best practice when testing your own custom hook is to mock any other external hooks it relies upon.

So you'd do something like this in your test (assuming jest)

jest.mock("../../../store/", () => ({
  useStoreActions: () => mockedThunkName,
}));

Does that help?

PS I understand the confusion as to why thunks are not considered "actions" as you access them via the same hook as other actions. But fundamentally in Redux's actions and thunk are completely different things. This is why they're usually handled differently during mocking.

LuisOsta avatar Mar 10 '21 12:03 LuisOsta