【问题标题】:Jest `toMatchSnapshot` causes "Maximum call stack size exceeded"开玩笑`toMatchSnapshot`导致“超出最大调用堆栈大小”
【发布时间】:2018-08-24 15:08:40
【问题描述】:

我正在尝试测试组件上的快照,但收到错误 RangeError: Maximum call stack size exceeded,但当我删除 toMatchSnapshot 时,错误消失了。这发生在我身上有多个组件,这里是一个例子。

我的项目是使用 Create React App 创建的并使用 Jest + Enzyme

堆栈调用

FAIL  src/components/Dashboard/Pipeline/Pipeline.test.js
  ● Pipeline Component › Pipeline Item › should render

    RangeError: Maximum call stack size exceeded

      at Object.it (src/components/Dashboard/Pipeline/Pipeline.test.js:20:181)
          at new Promise (<anonymous>)
      at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

这是我要测试的组件

import React from 'react';
import PropTypes from 'prop-types';
import { Circle } from 'rc-progress';
import Color from 'color';
import './PipelineItem.css';

const PipelineItem = ({ role, color, count, total, value }) => (
  <div className="pipeline">
    <span className="role" style={{ borderColor: color }}>
      {role}
    </span>
    <div className="progress">
      <Circle
        percent={count / total * 100}
        gapDegree={70}
        gapPosition="bottom"
        strokeWidth="6"
        strokeLinecap="square"
        strokeColor={color}
      />
      <div className="completed">
        <span className="count">
          {count} / {total}
        </span>
      </div>
    </div>
    <div className="value" style={{ backgroundColor: Color(color).fade(0.85) }}>
      <div className="wrap" style={{ borderColor: color }}>
        <span className="title">Value</span>
        <span className="value">${value}</span>
      </div>
    </div>
  </div>
);

PipelineItem.defaultProps = {
  value: 0,
  total: 0,
  count: 0,
};

PipelineItem.propTypes = {
  role: PropTypes.string,
  color: PropTypes.string,
  count: PropTypes.number,
  total: PropTypes.number,
  value: PropTypes.number
};

export default PipelineItem;

这是测试

import React from 'react';
import Pipeline from './Pipeline';
import PipelineItem from './PipelineItem';

describe('Pipeline Component', () => {
  describe('Pipeline Wrap', () => {
    it('should render', () => {
      expect(shallow(<Pipeline />)).toMatchSnapshot();
    });
  });
  describe('Pipeline Item', () => {
    const props = {
      role: 'Loan Officer',
      color: '#2ae',
      count: 100,
      total: 150,
      value: 15000
    };
    it('should render', () => {
      expect(shallow(<PipelineItem {...props}/>)).toMatchSnapshot();
    });
  });
});

第一次测试运行良好

【问题讨论】:

    标签: javascript reactjs testing jestjs


    【解决方案1】:

    经过数小时的查找 + 删除线后,我发现是 color 导致了问题 here 但这是因为 bug with older jest。

    鉴于我正在使用 Create React App,它们尚未更新到包含修复程序的 Jest 22(自 v22.0.4 起)

    【讨论】:

    • 谢天谢地。如果其他人遇到这种情况,inputmask (npmjs.com/package/inputmask) 也会导致此问题。如果这对某人有帮助,也许我的一天不会被完全浪费掉。
    • 我在开玩笑 23 日,但这个问题仍然存在 - 不确定是什么原因造成的
    猜你喜欢
    • 1970-01-01
    • 2023-01-26
    • 2014-11-17
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    相关资源
    最近更新 更多