React native:Shallow渲染器的警告

时间:2017-04-19 11:37:57

标签: react-native jestjs

我正在使用JEST框架进行单元测试。我使用Shallow从酶进行DOM测试。运行jest命令后,我收到以下警告。

通过测试 /CheckboxWithLabel-test.js

●控制台     console.error node_modules / fbjs / lib / warning.js:36       警告:浅层渲染器已移至react-test-renderer / shallow。更新引用以删除此警告。

我的测试是



import React from 'react';
import {shallow} from 'enzyme';
import CheckboxWithLabel from '../app/Component8/CheckboxWithLabel';

test('CheckboxWithLabel changes the text after click', () => {
  // Render a checkbox with label in the document
  const checkbox = shallow(
    <CheckboxWithLabel labelOn="On" labelOff="Off" />
  );
    
  expect(checkbox.text()).toEqual('Off');

  checkbox.find('input').simulate('change');

  expect(checkbox.text()).toEqual('On');
});
&#13;
&#13;
&#13;

我的package.json

&#13;
&#13;
{
  "name": "myapp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "linkify-it": "^2.0.3",
    "prop-types": "^15.5.8",
    "react": "16.0.0-alpha.6",
    "react-dom": "^15.4.2",
    "react-native": "0.43.1"
  },
  "devDependencies": {
    "babel-jest": "19.0.0",
    "babel-preset-es2015": "^6.24.0",
    "babel-preset-react-native": "1.9.1",
    "enzyme": "^2.8.2",
    "jest": "19.0.2",
    "react-addons-test-utils": "~15.1.0",
    "react-test-renderer": "16.0.0-alpha.6"
  },
  "jest": {
    "preset": "react-native"
  }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

来自Enzyme docs:

如果您使用React&gt; = 15.5,除了酶之外,您还必须确保安装了以下npm模块(如果它们尚未安装):

npm i --save-dev react-test-renderer react-dom