【问题标题】:"Cannot read property includes of undefined" error in Jest and EnzymeJest 和 Enzyme 中的“无法读取属性包括未定义”错误
【发布时间】:2018-10-27 11:10:54
【问题描述】:

我有一个用于渲染复选框的 React 组件。组件代码有一个如下所示的函数:

const generateCheckboxes = (array, filterType) => {
        return array.map((filter, i) => {
            if (!isNullOrUndefined(filter) && filter !== "") {
                const applied = props.appliedFilterList[filterType].includes(filter);
                console.log("yes", props.appliedFilterList[filterType]);
                return (
                    <Row key={i} noLine={i === 0}>
                        <Checkbox checked={applied} onClick={() => props.toggleFilter(filterType, filter)}>
                            {filter}
                        </Checkbox>
                    </Row>
                );
            }
            return false;
        });
    };

渲染函数如下所示:

return (
        <div>
            <div>Access</div>
                {generateCheckboxes(props.accessFilters, "access")}

            <div>Bandwidth</div>
                {generateCheckboxes(props.bandwidthFilters, "bandwidth")}
        </div>   
    );

现在我正在尝试为此组件编写一个测试,我只是将组件与 Enzyme 的浅层方法中的道具一起传递。我只想通过在测试用例中传递一些模拟数据来检查组件是否正确呈现.测试看起来像:

import React from "react";
import { mount, shallow } from "enzyme";
import Checkbox from "../../lib/Checkbox";
import FilterDropdownContent, { Header } from "../components/FilterDropdownContent";
import { generateCheckboxes } from "../components/FilterDropdownContent";
import { access } from "fs";

const accessData = ["Access Type Of The Service"];
const bandwidthData = ["the allowed band width", "the allowed band width"];
const termsFiltersData = ["term associated with the service"];
const appliedFilters = [
    {
        access: ["Access Type Of The Service"],
        bandwidth: ["the allowed band width", "the allowed band width"],
        term: ["term associated with the service"]
    }
];

describe("test the FilterDropdown component", () => {
    it("renders correctly", () => {
        const wrapper = mount(
            <FilterDropdownContent
                accessFilters={accessData}
                bandwidthFilters={bandwidthData}
                appliedFilterList={appliedFilters}
                termsFilters={termsFiltersData}
                toggleFilter={false}
            />
        );
    });
});

但我的测试失败并出现错误: TypeError:无法读取未定义的属性“包含”

includes 属性在 generateCheckboxes() 函数中。我在测试用例中通过了所需的道具。但我不确定是什么导致了问题。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: reactjs tdd enzyme jestjs


    【解决方案1】:

    使用 Jest 时,使用 toEqual 而不是 toMatchObject 时也会出现错误TypeError: Cannot read property 'includes' of undefined。

    以下代码将给出Cannot read property 'includes' of undefined 错误:

    test("should return the correct data", async () => {
      const response = await fetch({ url: "http://localhost/" });
    
      expect(response).toEqual({
        body: new Blob(["raw data"]),
        status: 200,
        statusText: "OK",
      });
    });
    

    改用toMatchObject 会修复它:

    // ...
      expect(response).toMatchObject({
    // ...
    

    【讨论】:

      【解决方案2】:

      因为applyFilters是一个数组:

      const appliedFilters = [
          {
              access: ["Access Type Of The Service"],
              bandwidth: ["the allowed band width", "the allowed band width"],
              term: ["term associated with the service"]
          }
      ];
      

      这部分代码将解析为undefined:

      props.appliedFilterList[filterType]
      

      因为filterType === 'access' 而不是数组索引。

      尝试将appliedFilters 更改为:

      const appliedFilters = {
          access: ["Access Type Of The Service"],
          bandwidth: ["the allowed band width", "the allowed band width"],
          term: ["term associated with the service"]
      };
      

      【讨论】:

      • 哇,成功了。非常感谢。我已经被这些困了好几个小时了。你救了我的命。再次非常感谢:)
      猜你喜欢
      • 1970-01-01
      • 2020-02-09
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 2021-04-30
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多