【问题标题】:How to use Enzyme Shallow with Jest snapshots如何在 Jest 快照中使用 Enzyme Shallow
【发布时间】:2017-11-17 08:51:08
【问题描述】:

我正在尝试同时使用 enzyme 中的 shallowjest 中的 snapshots

我面临的问题是我需要使用 setState()enzyme 更改状态,然后将结果与快照匹配。

查看我的组件的代码:

....

getPrevProduct = () => {
  const key = this.state.indexCurrent > 0 &&
   this.productsKeys[this.state.indexCurrent - 1];

  let product = null;

  if (key) {
    product = this.props.products[key];
  }

  return product;
}

renderPrev = () => this.getPrevProduct() && <PrevButton />;

render() {
  return (
    <div>
      ...
      {this.renderPrev()}
      ...
    <div>
  )
}

前面的代码检查从 currentIndex 中的 props 传递的产品是否存在。

这就是为什么我需要 Enzyme 来改变状态。然后,如果匹配,&lt;PrevButton /&gt; 必须被渲染。

这个测试是我想将组件与快照匹配:

const nextProps = {
  products: {
    test: 'test'
  }
};

const tree = create(<Component nextProps={nextProps} />).toJSON();

expect(tree).toMatchSnapshot();

但我需要更改状态。我怎么能这样做?这不起作用:

it('should render component with prev button', () => {
  const nextProps = {
    products: {
      test: 'test'
    }
  };
  const instance = shallow(<Component nextProps={nextProps} />).instance()

  instance.setState({
    indexCurrent: 1
  });

  const tree = create(<Component nextProps={nextProps} />).toJSON();

  expect(tree).toMatchSnapshot();
});

我还尝试将组件的声明保存到变量中,就像下一个未完成的代码一样,并在shallowcreate中使用它:

const component = <Component nextProps={nextProps} />;

 shallow(component).instance()).instance()
 create(component).toJSON()`

我也试过了,没有运气enzyme-to-json

你会怎么做?

【问题讨论】:

    标签: javascript reactjs unit-testing jestjs enzyme


    【解决方案1】:

    可能,它不是应该使用enzyme-to-json 的方式。试试这个方法:

    import { shallowToJson  } from 'enzyme-to-json';
    import { shallow } from 'enzyme';
    

    然后在你的测试中:

    const wrapper = shallow(<Component />);
    expect(shallowToJson(wrapper)).toMatchSnapshot();
    

    【讨论】:

      【解决方案2】:

      安装enzyme-to-json

      npm install --save-dev enzyme-to-json
      

      更新你的笑话配置:

      "jest": {
        "snapshotSerializers": [
          "enzyme-to-json/serializer"
        ]
      }
      

      测试

      it('works', () => {
        const wrapper = shallow(<MyComponent />)
        expect(wrapper).toMatchSnapshot()
      })
      

      原文:https://devhints.io/enzyme#installing

      【讨论】:

        【解决方案3】:

        经过反复试验,我发现酶有一种方法,没有记录(或者我没有找到)称为getRenderOutput

        该方法以 JSON 格式返回组件,因此我可以这样做:

        it('should render component with prev button', () => {
          const nextProps = {
            products: {
              test: 'test'
            }
          };
          const render = shallow(mockComponent(nextProps))
          const instance = render.instance();
        
          instance.setState({
            indexCurrent: 1
          });
        
          const tree = render.renderer.getRenderOutput();
        
          expect(tree).toMatchSnapshot();
        });
        

        这就是我如何将 Jest 中的快照与酶一起使用。

        getRenderOutput 唯一的问题是,如果我放一个控制台日志,它会被打印两次。那是因为instance()getRenderOutput() 都会触发测试。

        这是快照的输出:

        exports[`ProductNavigator should render component with prev button 1`] = `
          <div>
            <FloatingActionButton
              className="NavigatorButton left"
              onTouchTap={[Function]}
              secondary={true}
            >
              <KeyboardArrowLeft />
            </FloatingActionButton>
          </div>
        `;
        

        如果有人知道更好的方法,请添加评论。

        谢谢!

        【讨论】:

        • 使用“酶-json”。然后你可以将其转换为类似于 shallow().toJson.... 的 json
        • 是的,我试过了,正如我在问题中所说的那样,但我遇到了这个错误:Cannot find module 'enzyme/build/RSTTraversal' from 'shallow.js' 我没有找到解决方案
        • 是的,我遵循了这些说明,但什么也没做。也许与我的配置有关。我需要时间来好好看看它。
        • 嗨@AlbertOlivé 你让它工作了吗?无法从“shallow.js”中找到模块“enzyme/build/RSTTraversal”。我也遇到了同样的错误!
        猜你喜欢
        • 2018-09-13
        • 2020-03-03
        • 2019-06-22
        • 1970-01-01
        • 2020-04-22
        • 2019-09-20
        • 1970-01-01
        • 2021-05-29
        • 1970-01-01
        相关资源
        最近更新 更多