reactjs - TypeError : expect. any 不是函数

标签 reactjs jestjs create-react-app

我正在使用 create-react-app,我正在尝试为我的 action creator 编写一个 Jest 测试。该操作添加的字段之一是随机 ID,因此我想使用 expect.any(String) 以便测试通过。但是我收到 TypeError: expect.any is not a function

这是我的测试:

describe('actions', () => {

it('should create an action to add a ticket', () => {
 const payload = {
     summary: 'A summary',
     description: 'A description',
     status:'open',
     priority: 'minor',
  };

  const expected = {
    type: actions.ADD_TICKET,
    ticket: {
          ...payload,
          id: expect.any(String),
      },
  };

  const actual = addTicket(payload);

  expect(actual).toEqual(expected)
 })
})

最佳答案

expect.any 是在 Jest 18 中添加的,它没有随这个版本的 create-react-app 一起发布。他们刚推的版本现在支持了

关于reactjs - TypeError : expect. any 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41942411/

相关文章:

javascript - 如何在 React/jsx 中以三元组的形式显示真实情况下的组件和变量?

reactjs - 如何使用 Reactstrap 检查复选框的选中值

javascript - 类型错误 : Cannot read property 'prototype' of undefined in jest test

typescript - Jest (ESM) 在单元测试中从 React Native 加载文件时出现问题

create-react-app - 带有 react-scripts@next 和 css 模块的故事书

node.js - 如何修复react-scripts启动时的 “Error watching file for changes: ECONNRESET”错误?

javascript - ReactCSSTransitionGroup componentWillLeave 未调用

css - 我们如何将 css 属性添加到样式组件中的类?

javascript - Jest/ typescript : Mock class dependencies in Jest and Typescript

javascript - 是否可以使用reactjs CRA为不同的环境渲染不同的html