【问题标题】:Jest shows console.warn for Accessing PropTypes and createClassJest 显示用于访问 PropTypes 和 createClass 的 console.warn
【发布时间】:2017-12-29 00:37:35
【问题描述】:

我正在使用 Jest 测试一个 React 组件。测试运行良好,但我收到一些非常烦人的 console.warn 消息。我自己没有使用 PropTypes 或 createClass,所以我怀疑这来自某个库。有什么方法可以找出它们来自哪里,或者压制它们?

PASS  src/__tests__/title.test.ts
  ● Console

    console.warn node_modules/react/lib/lowPriorityWarning.js:40
      Warning: Accessing PropTypes via the main React package is deprecated, and will be removed in  React v16.0. Use the latest available v15.* prop-types package from npm instead. For info on usage, compatibility, migration and more, see
    console.warn node_modules/react/lib/lowPriorityWarning.js:40
      Warning: Accessing createClass via the main React package is deprecated, and will be removed in React v16.0. Use a plain JavaScript class instead. If you're not yet ready to migrate, create-react-class v15.* is available on npm as a temporary, drop-in replacement. For more info see 

测试看起来像这样

import { shallow } from "enzyme";
import * as React from "react";
import {Title} from "../components/title";

describe("Testing title component", () => {
  it("renders", () => {
    const titleElement: React.ReactElement<{}> = React.createElement(Title);
    const component = shallow(titleElement);
    const text = component.text();
    expect(text).toBe("test");
  });
});

组件看起来像这样

import * as React from "react";

export class Title extends React.Component<{}, {}> {

  constructor(props: {}) {
    super(props);
  }

  public render(): React.ReactElement<{}> {
    return <p>test</p>;
  }

}

【问题讨论】:

    标签: javascript reactjs typescript jestjs


    【解决方案1】:

    使用 * (import * as) 导入访问 React 上的所有道具,包括 PropTypescreateClass,最终导致警告。

    尝试改写import React from 'react'

    https://github.com/airbnb/enzyme/issues/1065

    【讨论】:

      猜你喜欢
      • 2017-09-21
      • 1970-01-01
      • 2014-11-25
      • 2019-01-07
      • 2016-11-12
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 2017-10-02
      相关资源
      最近更新 更多