question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to reset atom in unit test?

See original GitHub issue

I know I can follow the doc here: https://docs.pmnd.rs/jotai/guides/resettable in my code to achieve this. But I’m writing unit tests in my project, which it appears that the atom value would be persisted somehow, but I think all the atom values used in different unit tests should not be the same.

my hook is defined below:

export const stepAtom = atom(0);

export function useOnBoardingService() {
  const { width } = useWindowDimensions();
  const [currentIndex, updateStep] = useAtom(stepAtom);

  const slidePrev = useCallback(() => {
    if (currentIndex !== 0) {
      const step = currentIndex - 1;
      updateStep(step);
    }
  }, [currentIndex, updateStep]);

  const slideNext = useCallback(() => {
    if (currentIndex !== 3) {
      const step = currentIndex + 1;
      updateStep(step);
    }
  }, [currentIndex, updateStep]);

  return {
    slidePrev,
    slideNext,
  };
}

and my unit test is below:

import { renderHook, act } from '@testing-library/react-hooks';
import { useAtomValue } from 'jotai/utils';
import { useOnBoardingService, stepAtom } from './useOnBoardingService';

test('should useOnBoardingService', () => {
  const { result } = renderHook(() => useOnBoardingService());
  expect(typeof result.current.slidePrev).toBe('function');
});

test('should step be 1', () => {
  const { result } = renderHook(() => useOnBoardingService());
  const { result: stepResult } = renderHook(() => useAtomValue(stepAtom));

  expect(stepResult.current).toBe(0);

  act(() => {
    result.current.slideNext();
  });

  expect(stepResult.current).toBe(1);
});

test('should step be 1 too', () => {
  const { result } = renderHook(() => useOnBoardingService());
  const { result: stepResult } = renderHook(() => useAtomValue(stepAtom));

  // I want this to be 0 but it actually is 1 because atom value changed after the above test executed
  expect(stepResult.current).toBe(0); 

  act(() => {
    result.current.slideNext();
  });

  // actual value would be 2
  expect(stepResult.current).toBe(1);
});

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, May 25, 2021

atomWithReset is not primarily for testing. Provider is. jotai is based on react context and to isolate state within memory, we need to use <Provider>.

According to https://react-hooks-testing-library.com/usage/advanced-hooks#context

import { Provider } from 'jotai'

const wrapper = ({ children }) => <Provider>{children}</Provider>

This may help.

0reactions
dai-shicommented, May 26, 2021

I’d personally do that even with useState. It’s more about how you organize your code in React in general. The library is rather unopinionated about the code structure. Happy to see someone come up with best practices.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to reset unit test? - angularjs - Stack Overflow
Since you are appending the element to the body, you need to manually remove it too. Add this line to end of each...
Read more >
Testing - Recoil
Testing Recoil state inside of a React component. ... useRecoilValue and useEffect , to observe an atom / selector 's changes and execute...
Read more >
Clean Unit Tests with Mockito - Reflectoring
Normally, you don't need to reset your mocks, just create new mocks for each test method. We better create simple and small test...
Read more >
atomWithReset - Jotai
Creates an atom that could be reset to its initialValue with useResetAtom hook. It works exactly the same way as primitive atom would,...
Read more >
Reset Unit Tests - Khan Academy Help Center
Why can't we reset the unit tests/quizzes after messing up? I've been at this for hours and cannot get 100% because it makes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found