【发布时间】:2019-12-09 00:17:26
【问题描述】:
我正在使用 React Testing Library 和 Jest 来测试我的 React/Redux 应用程序。
我想在多个测试中测试同一个组件,而不是在每个实例之间共享组件的状态。
这样的……
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
});
});
我遇到的问题是第一个 <App /> 的状态“泄漏”到下一个测试中,我希望我的每个测试都是独立的。实现此目的的正确方法是什么?
这里是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"
}
}
【问题讨论】:
标签: reactjs jestjs integration-testing react-testing-library