Jest 和 Enzyme 中出现“无法读取包含未定义属性”的错误

pra*_*ami 2 tdd reactjs jestjs enzyme

我有一个用于渲染复选框的 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;
        });
    };
Run Code Online (Sandbox Code Playgroud)

渲染函数如下所示:

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

            <div>Bandwidth</div>
                {generateCheckboxes(props.bandwidthFilters, "bandwidth")}
        </div>   
    );
Run Code Online (Sandbox Code Playgroud)

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

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}
            />
        );
    });
});
Run Code Online (Sandbox Code Playgroud)

但我的测试失败并出现错误: TypeError: Cannot read property 'includes' of undefined

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

use*_*902 9

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

下面的代码会报错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",
  });
});
Run Code Online (Sandbox Code Playgroud)

使用toMatchObject它可以解决这个问题:

// ...
  expect(response).toMatchObject({
// ...
Run Code Online (Sandbox Code Playgroud)