-
I would like some help, as well as understanding testing a React Application in Typescript using Zustand with Jest. I've been trying to follow discussions either on this repo, SO, as well as the Zustand documentation, but I'm failing to get past a certain point. The root issue I'm running into is: My test is simply: import useStore from '../src/store';
describe('test store', () => {
const initialStoreState = useStore.getState();
beforeEach(() => {
useStore.setState(initialStoreState, true);
});
test('renders HomePage', () => {
render(<Router><HomePage/></Router>);
useStore.setState({ countries: [] });
const countriesList = screen.getByText('Countries List');
expect(countriesList).toBeInTheDocument();
});
}); where My store is created like this: import create from 'zustand';
const useStore = create<IStore>((set, get) => ({
countries: [],
fetchCountries: async () => { ... },
...
}));
export default useStore; where As I've also read, I've placed a import { StateCreator } from "zustand";
import { act } from "@testing-library/react";
const actualCreate = jest.requireActual('zustand');
// a variable to hold reset functions for all stores declared in the app
const storeResetFns = new Set<() => void>();
// when creating a store, we get its initial state, create a reset function and add it in the set
const create = () => <S,>(createState: StateCreator<S>) => {
const store = actualCreate(createState);
const initialState = store.getState();
storeResetFns.add(() => {
store.setState(initialState, true);
});
return store;
}
// Reset all stores after each test run
beforeEach(async () => {
await act(() =>
storeResetFns.forEach((resetFn: any) => {
resetFn();
})
)
})
export default create I do notice here that it does execute The versions that I have are: {
"jest": "^29.3.1",
"react": "^18.2.0",
"typescript": "^4.9.3",
"zustand": "^4.1.4"
} Any ideas or helping me understand this a bit more would be greatly appreciated, thank you in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
The default export is deprecated. Can you try the latest version and use named exports for everything? |
Beta Was this translation helpful? Give feedback.
The default export is deprecated. Can you try the latest version and use named exports for everything?