Skip to content

Instantly share code, notes, and snippets.

@joernb
Created May 17, 2019 13:48
Show Gist options
  • Save joernb/a9e9e0dfd0dd9aaf76865b8c3fab3e52 to your computer and use it in GitHub Desktop.
Save joernb/a9e9e0dfd0dd9aaf76865b8c3fab3e52 to your computer and use it in GitHub Desktop.
Mock useState for async act
jest.mock('react', () => {
const react = jest.requireActual('react');
const reactTestingLibrary = jest.requireActual('react-testing-library');
return {
...react,
useState: initial => {
const [state, setState] = react.useState(initial);
return [
state,
newState => reactTestingLibrary.act(() => setState(newState))
];
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment