【问题标题】:Refs are null in Jest snapshot tests with react-test-renderer使用 react-test-renderer 的 Jest 快照测试中的 Refs 为空
【发布时间】:2017-04-12 15:17:56
【问题描述】:

目前我在 componentDidMount 上手动初始化 Quill 编辑器,但我的玩笑测试失败了。看起来我得到的 ref 值在 jsdom 中为空。这里有问题:https://github.com/facebook/react/issues/7371 但看起来 refs 应该可以工作。有什么想法我应该检查吗?

组件:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {

  componentDidMount() {
    console.log(this._p)
  }
  
  render() {
    return (
      <div className="App">
        <div className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h2>Welcome to React</h2>
        </div>
        <p className="App-intro" ref={(c) => { this._p = c }}>
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

测试:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import renderer from 'react-test-renderer'

it('snapshot testing', () => {
    const tree = renderer.create(
        <App />
    ).toJSON()
    expect(tree).toMatchSnapshot()  
})

因此,console.log 输出 null。但我希望 P 标记

【问题讨论】:

    标签: javascript reactjs jestjs


    【解决方案1】:

    我使用来自 repo 的基于酶的测试来解决这个问题:

    import { shallow } from 'enzyme'
    import toJson from 'enzyme-to-json'
    
    describe('< SomeComponent />', () => {
      it('renders', () => {
    
        const wrapper = shallow(<SomeComponent />);
    
        expect(toJson(wrapper)).toMatchSnapshot();
      });
    });
    

    【讨论】:

      【解决方案2】:

      由于测试渲染器没有与 React DOM 耦合,它不知道 ref 应该是什么样子。 React 15.4.0 增加了为测试渲染器模拟 refs 的能力但你应该自己提供这些模拟React 15.4.0 release notes 包括一个这样做的例子。

      import React from 'react';
      import App from './App';
      import renderer from 'react-test-renderer';
      
      function createNodeMock(element) {
        if (element.type === 'p') {
          // This is your fake DOM node for <p>.
          // Feel free to add any stub methods, e.g. focus() or any
          // other methods necessary to prevent crashes in your components.
          return {};
        }
        // You can return any object from this method for any type of DOM component.
        // React will use it as a ref instead of a DOM node when snapshot testing.
        return null;
      }
      
      it('renders correctly', () => {
        const options = {createNodeMock};
        // Don't forget to pass the options object!
        const tree = renderer.create(<App />, options);
        expect(tree).toMatchSnapshot();
      });
      

      请注意,它仅适用于 React 15.4.0 及更高版本

      【讨论】:

      • 感谢您的评论。我的用例是,一旦安装了组件,我想在 DOM 元素中呈现 Quill 编辑器。我可能会返回类似 document.createElement("div") 的东西。但在这种情况下,渲染部分不会成为快照测试的一部分。有没有办法包含它?
      • 使用测试渲染器的快照测试不适用于依赖 DOM 的部分。如果你需要测试 DOM 本身而不是 React 组件,请考虑在 jsdom 环境中使用 Enzyme 的mount()
      • 感谢分享这个答案,这就是我要找的。​​span>
      • 这个答案和发行说明链接都很完美,但是有什么理由实际的create 文档没有记录options(例如createNodeMock)可用的内容吗?如果没有,也许我会创建一张票。
      猜你喜欢
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 2020-06-21
      • 1970-01-01
      • 2018-12-17
      • 1970-01-01
      相关资源
      最近更新 更多