我有一个从create-react-app启动的react-redux应用程序。据我了解,运行jest所需的内容已经在create-react-app(即webpack文件,babel等)中为您配置了。
因此,我创建了一个名为CreateQueryActions.test.js
的测试套件,并且此文件包含一个测试套件,其中包含所有必需的组件(据我所知)-describe / it / expect。
当我运行npm run test
时,开玩笑地看到了该文件,但失败了,并显示了Test suite failed to run Your test suite must contain at least one test.
,但确实有测试。那么这里发生了什么?
import { tableList } from "../../utils/CreateQueryMockData";
import * as createQueryActions from "./CreateQueryActions";
import thunk from "redux-thunk";
import fetchMock from "fetch-mock";
import configureMockStore from 'redux-mock-store';
//Async test dependencies
const middleware = [thunk];
const mockStore = configureMockStore(middleware);
describe("create query API calls", () => {
afterEach(() => {
/*Its importatnt to keep this test atomic.
* This initialized fetchMock for each test -
* needs to be reset after each test has ran
* */
fetchMock.restore();
});
it("should create a 'GET_TABLE_LIST' action", () => {
fetchMock.mock("*", {
body: tableList,
headers: { "content-type": "application/json" }
});
const expectedAction = [
{ type: "GET_TABLE_LIST" }
];
const store = mockStore({ tableList: [] });
return store.dispatch(createQueryActions.getTableList()).then(() => {
expect(store.getActions()).toEqual(expectedAction);
});
});
});
PACKAGE.JSON
{
"name": "queryapp",
"version": "1.0.0",
"private": true,
"dependencies": {
"axios": "^0.19.0",
"bootstrap": "^4.3.1",
"history": "^4.9.0",
"http-proxy-middleware": "^0.19.1",
"jest": "^24.7.1",
"oidc-client": "^1.8.2",
"react": "^16.8.6",
"react-bootstrap": "^1.0.0-beta.5",
"react-csv": "^1.1.1",
"react-dom": "^16.8.6",
"react-redux": "^5.1.1",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.1",
"react-table": "^6.10.0",
"redux": "^4.0.4",
"redux-form": "^7.4.2",
"redux-form-validators": "^2.7.5",
"redux-oidc": "^3.1.2",
"redux-thunk": "^2.3.0"
},
"devDependencies": {
"fetch-mock": "^7.3.9",
"redux-immutable-state-invariant": "^2.1.0",
"redux-logger": "^3.0.6",
"redux-mock-store": "^1.5.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
答案 0 :(得分:0)
afterEach
不是测试用例。它用于在每个测试用例之后执行某些任务。您的测试用例应如下所示:
describe("create query API calls", () => {
it("should create a 'GET_TABLE_LIST' action", () => {
fetchMock.mock("*", {
body: tableList,
headers: { "content-type": "application/json" }
});
const expectedAction = [
{ type: "GET_TABLE_LIST" }
];
const store = mockStore({ tableList: [] });
return store.dispatch(createQueryActions.getTableList()).then(() => {
expect(store.getActions()).toEqual(expectedAction);
});
});