I'm currently creating a react native app using Zustand for my state management. Zustand has the ability to persist the state into storage, in my case the async storage of react native. It works fine, however when running tests the persist wrapper of Zustand causes my jest suite to fail.
Simple snippet of my Zustand state:
persist(
(set) => ({
variable: "something",
changeVariable: (variable: string) => {
set({ variable });
},
}),
{
name: "async-storage",
getStorage: () => AsyncStorage,
}
)
My Test:
describe("<SomeComponent />", () => {
it("renders default elements", () => {
const { getByText } = render(<SomeComponent />);
expect(getByText("Hello World")).toBeInTheDocument();
});
});
If I comment out the persist wrapper, the test truns through, in this state however I get the error message
TypeError: Cannot read properties of undefined (reading 'bind')
14 | }
15 |
> 16 | export const useStore = create<ZustandState>()(
| ^
17 | persist(
18 | (set) => ({
I tried to mock zustand as a whole using https://docs.pmnd.rs/zustand/guides/testing, but the binding error just moves to the mock then. I'm not sure if the store itself in undefined not allowing binding or if its just the persist itself. I however don't know how to tackle this. Anyone has ideas on how to counter this?