I'm using React Testing Library with Jest to test my React/Redux App.
I want to test the same component in multiple tests without the component's state being shared across each instance.
Something like this...
import React from "react";
import {renderWithRedux} from '../testUtils';
import App from "../components/App";
describe("App", () => {
test("does something as expected", async () => {
const {container} = renderWithRedux(<App />);
// interact with App
});
test("does something ELSE as expected", async () => {
const {container} = renderWithRedux(<App />); //I DONT WANT THE STATE FROM PREVIOUS TEST
});
});
The problem I'm running into is that the state of the first <App />
is "leaking" into the next test and I want each of my tests to be independent. What is the right way to accomplish this?
Here is the definition of renderWithRedux
:
import React from "react";
import { Provider } from "react-redux";
import { createStore, applyMiddleware } from 'redux';
import {reducer, initialState} from "./store";
import thunkMiddleware from 'redux-thunk';
import { render } from '@testing-library/react';
import { Router } from 'react-router-dom'
import { createMemoryHistory } from 'history'
function renderWithRedux(
ui,
{ initialState, store = createStore(reducer, initialState, applyMiddleware(thunkMiddleware)) } = {}
) {
return {
...render(<Provider store={store}>{ui}</Provider>),
store
}
}
package.json:
{
"dependencies": {
"react": "^16.11.0",
"react-dom": "^16.11.0",
"react-redux": "^7.1.3",
"react-router-dom": "^5.1.2",
"react-scripts": "2.1.5",
"redux": "^4.0.4",
"redux-thunk": "^2.3.0"
},
"devDependencies": {
"@testing-library/jest-dom": "^4.2.3",
"@testing-library/react": "^9.3.2",
"redux-mock-store": "^1.5.3",
"typescript": "^3.7.2"
}
}