-
-
Notifications
You must be signed in to change notification settings - Fork 649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] How do you unit test Jotai atoms? #252
Comments
It would be really nice to have
Guess so. Do some files in If you were just testing the functions for atom like
Not sure what you mean by this. If you are testing a component behavior, you don't need to mock
Yeah. I'm not super experienced on testing best practices. Hope someone to volunteer. Otherwise, I'd try my best... |
Maybe mock |
@Noitidart Would you be interested in investigating further and create docs and/or examples? |
Yes totally I'll write a test for the above and share here and add it into docs if everyone likes it. |
I'm still not quite sure what would be best practices for unit testing atoms. But this is what I have been doing so far. I would love some feedback on how to improve this as it does seem a bit clunky. Since atoms (afaik) can ONLY be used with react-hooks, I create a dummy component that stores the "get" data, Then expect the component with the get data to update to the correct snapshots.
I feel like there has to be a much cleaner way to do this? Would appreciate some feedback. EDIT: I improved it somewhat by making a generic AtomTestComponent:
and my test now looks like this:
But of course that only works for a simple use-case. Not for reducerAtoms etc. |
This is correct. Atom values only exist in Provider. I don't see any How is your Hope to get some feedbacks from others. |
I think we can test const getter = (get) => 1
const setter = (get,set,update) => set(justAtom,3)
const justAtom = atom(getter,setter)
console.log(justAtom.read === getter, justAtom.write === setter) // true true
// testing phase
const justAtom = atom(
(get) => get(justAtom),
(get, set, update) => set(justAtom, get(justAtom) + 1)
);
justAtom.init = 0
const get = (a) => {
return a.testValue || a.init;
};
const set = (a, value) => {
a.testValue = value;
};
console.log(justAtom.read(get)); // 0
justAtom.write(get, set);
justAtom.write(get, set);
console.log(justAtom.read(get)); // 2 I'm not saying that we should make |
I thought about that too. Yeah, if it's the goal, it would be fine. It totally depends on what we would like to test. Let's say if we replace |
Our plan for this issue for v1 is to add some guide in docs about testing. |
related: #496 |
I believe we should echo Therefore we could encourage:
I'm happy to make a write-up in the new docs. |
That's great news. Please go ahead. |
Closing this as https://github.com/pmndrs/website/pull/92 is merged. Although the original question may not be fully answered, please open a new issue or discussion instead of continuing discussions here. |
What if you have a custom atom with a custom getter |
Also running into what @sergiotlITX is talking about. We'd love a way to test our custom atoms as pure functions! |
@brammerl Testing atoms as pure functions is probably not possible (I guess) and I don't see the advantage of doing so. You would have to mock Jotai's If you want to avoid hooks in your tests, you could use the store directly: test("example", () => {
const store = createStore();
store.set(doubleCounterAtom, 5);
expect(store.get(counterAtom)).toBe(10);
}); |
I recently starting using Jotai. I like it a lot, but have also run into the issue described here. I've adopted a pattern in my code to make testing easy without getting too much in the way: Counter.tsx:
In my test file, I pass in a jest function and use a
Since the saveCount prop is optional, the rest of my code remains unchanged. |
@richcrad The approach requires changes to the production code to be testable and ships that code also to production. The test also doesn't guarantee that the component actually works, if you refactor the component and forget the |
@rothsandro Yes that's very true. The tradeoff is working well for me, as the added code is minimal and idiomatic, so pretty easy to avoid mistakes. I'd prefer it if you could directly |
Considering using Jotai for our production application.
It seems to fit all of our needs perfectly, but I haven't been able to find any docs on testing Jotai atoms.
E.g. I created a simple themeAtom and setThemeAtom like this:
How would I go about writing a unit test that makes sure when setTheme is called with a valid theme name, that the themeAtom updates to the expected theme?
Since Jotai only functions inside "React" do I have to write a dummy component with a dummy button to test my state management atoms?
Also interested in how I would test components that use
useAtom
. I'm guessing in this case I mock the useAtom, but it would be very helpful to have a guide/docs on the recommended way to do this.Thanks!
The text was updated successfully, but these errors were encountered: