Jest显示了用于访问PropTypes和createClass的console.warn

时间:2017-07-23 09:11:36

标签: javascript reactjs typescript jestjs

我正在使用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>;
  }

}

1 个答案:

答案 0 :(得分:2)

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

尝试改为编写import React from 'react'

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