【问题标题】:TypeError: Cannot read property 'equal' of undefinedTypeError:无法读取未定义的属性“等于”
【发布时间】:2017-02-17 00:00:45
【问题描述】:

我正在尝试使用enzyme 来断言 DOM 节点。我的Component 看起来像

import React, {Component} from 'react';
import TransactionListRow from './TransactionListRow';
import {Table, TableBody, TableHeader, TableHeaderColumn, TableRow} from 'material-ui/Table';

export default class TransactionList extends Component {
  render() {
    const { transactions } = this.props;

    return (
      <Table>
        <TableHeader displaySelectAll={false}>
          <TableRow>
            <TableHeaderColumn>Name</TableHeaderColumn>
            <TableHeaderColumn>Amount</TableHeaderColumn>
            <TableHeaderColumn>Transaction</TableHeaderColumn>
            <TableHeaderColumn>Category</TableHeaderColumn>
          </TableRow>
        </TableHeader>
        <TableBody>
          {transactions.map(transaction =>
            <TransactionListRow key={transaction.id} transaction={transaction}/>
          )}
        </TableBody>
      </Table>
    );
  }
};

我的test 看起来像

import expect from 'expect';
import React from 'react';
import {mount} from 'enzyme';
import TransactionList from '../TransactionList';
import {TableHeaderColumn} from 'material-ui/Table';
import getMuiTheme from 'material-ui/styles/getMuiTheme';

describe("<TransactionList />", () => {
  const mountWithContext = (node) => mount(node, {
    context: {
      muiTheme: getMuiTheme(),
    },
    childContextTypes: {
      muiTheme: React.PropTypes.object.isRequired,
    }
  });


  it('renders five <TableHeaderColumn /> components', () => {
    const wrapper = mountWithContext(<TransactionList transactions={[]}/>)

    console.log(wrapper.html());
    // expect(wrapper.find('thead').length).toBe(1);
    expect(wrapper.contains(<TableHeaderColumn>Name</TableHeaderColumn>)).to.equal(true)
  });
});

当我运行它时,我得到了

  ● <TransactionList /> › renders five <TableHeaderColumn /> components

    TypeError: Cannot read property 'equal' of undefined

      at Object.<anonymous> (src/components/transactions/__tests__/TransactionList.test.js:24:250)
      at process._tickCallback (internal/process/next_tick.js:103:7)

根据Enzymedocs

.contains() 需要一个 ReactElement,而不是一个选择器(像许多其他 方法)。确保当您调用它时,您正在调用它 使用 ReactElement 或 JSX 表达式。

我做错了什么?

谢谢

更新
我删除了import expect from 'expect 并将其运行为

import React from 'react';
import {mount} from 'enzyme';
import TransactionList from '../TransactionList';
import TableHeaderColumn from 'material-ui/Table';
import getMuiTheme from 'material-ui/styles/getMuiTheme';

describe("<TransactionList />", () => {
  const mountWithContext = (node) => mount(node, {
    context: {
      muiTheme: getMuiTheme(),
    },
    childContextTypes: {
      muiTheme: React.PropTypes.object.isRequired,
    }
  });


  it('renders five <TableHeaderColumn /> components', () => {
    const wrapper = mountWithContext(<TransactionList transactions={[]}/>)

    // console.log(wrapper.html());
    expect(wrapper.find('thead').length).toBe(1);
    expect(wrapper.find('td').length).toBe(0);

    // this is not working
    expect(wrapper.contains(<TableHeaderColumn/>)).toEqual(true);
  });
});

现在失败了

 FAIL  src/components/transactions/__tests__/TransactionList.test.js
  ● <TransactionList /> › renders five <TableHeaderColumn /> components

    expect(received).toEqual(expected)

    Expected value to equal:
      true
    Received:
      false

      at Object.<anonymous> (src/components/transactions/__tests__/TransactionList.test.js:26:164)

expect(wrapper.contains(<TableHeaderColumn/>)).to.equal(true);

我明白了

      Warning: Unknown props `onMouseEnter`, `onMouseLeave`, `onClick` on <th> tag. Remove these props from the element. 
 FAIL  src/components/transactions/__tests__/TransactionList.test.js
  ● <TransactionList /> › renders five <TableHeaderColumn /> components

    TypeError: Cannot read property 'equal' of undefined

      at Object.<anonymous> (src/components/transactions/__tests__/TransactionList.test.js:26:166)
      at process._tickCallback (internal/process/next_tick.js:103:7)

我仍然无法断言ReactElement

【问题讨论】:

    标签: javascript reactjs jestjs enzyme chai


    【解决方案1】:

    这不是Enzyme 的问题。

    expect(...).toundefined,因为您已经安装了expect.js 并且您使用的是chai 语法。

    这个

    expect(wrapper.contains(<TableHeaderColumn>Name</TableHeaderColumn>)).to.equal(true)
    

    应该是

    expect(wrapper.contains(<TableHeaderColumn>Name</TableHeaderColumn>)).toEqual(true)
    

    【讨论】:

    • 感谢您的帮助,请查看我的更新。我还是一头雾水
    • 您尝试过使用expect(wrapper.find('TableHeaderColumn').length === 5).toEqual(true); / expect(wrapper.find(TableHeaderColumn).length === 5).toEqual(true); 吗?
    • 感谢@QoP,您的前两行立即帮助了我。我怀疑对于像我这样从使用 expect 进行单元测试到使用辅助库、chia 等,却没有注意到语法细微差别的人来说,这种情况会发生很多。
    • 又一个被绊倒的 chai 用户来了!谢谢QoP!
    • 有趣的是,Enzyme docs 显示了 to.equal() 语法用法,没有任何对 Chai 的引用。 ?
    猜你喜欢
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 2019-08-19
    • 1970-01-01
    相关资源
    最近更新 更多