【问题标题】:Create-React-App Jest failing: test suite must contain at least one test - but it doesCreate-React-App Jest 失败:测试套件必须包含至少一个测试 - 但确实如此
【发布时间】:2019-08-26 16:30:03
【问题描述】:

我有一个从 create-react-app 启动的 react-redux 应用程序。据我了解,运行 jest 所需的内容已经在 create-react-app 中为您配置(即 webpack 文件、babel 等)。

所以我创建了一个名为CreateQueryActions.test.js 的测试套件,该文件包含一个包含所有必要组件的测试套件(据我所知) - 描述/它/预期。

当我运行 npm run test 时,jest 看到了该文件,但它以 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"
    ]
  }
}

【问题讨论】:

    标签: redux react-redux jestjs


    【解决方案1】:

    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);
            });
    });
    

    【讨论】:

    • 谢谢@Mukesh - 这不是问题,但你确实让我看看出了什么问题。我确实需要 after each 语句,因为它对保持测试原子性很重要 - 每次测试后初始化 fetchMock.restore 所需的函数,问题是我也将测试包装在 afterEach 函数中,正如你指出的那样,这是不正确的.现在测试运行成功。
    • 欢迎...很高兴为您提供帮助
    猜你喜欢
    • 2018-11-23
    • 2019-06-25
    • 2021-06-26
    • 2017-01-01
    • 2018-08-01
    • 2022-12-11
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    相关资源
    最近更新 更多