简体   繁体   English

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

[英]Jest shows console.warn for Accessing PropTypes and createClass

I'm testing a React component using Jest. 我正在使用Jest测试React组件。 The test runs fine, but I get a few console.warn messages that are very annoying. 测试运行正常,但我得到一些非常烦人的console.warn消息。 I'm not using either PropTypes or createClass myself, so I'm suspecting this comes from some library. 我自己没有使用PropTypes或createClass,所以我怀疑这来自某个库。 Is there some way to figure out where they are coming from, or suppress them? 有没有办法弄清楚他们来自哪里,或压制他们?

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 

The test looks like this 测试看起来像这样

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");
  });
});

and the component looks like this 该组件看起来像这样

import * as React from "react";

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

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

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

}

Importing using * ( import * as ) access all the props on React , including PropTypes and createClass , which is eventually causing the warnings. 使用*( import * asimport * as访问React上的所有道具,包括PropTypescreateClass ,最终导致警告。

Try to write import React from 'react' instead. 尝试import React from 'react'编写import React from 'react'

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检测 console.warn 消息 - Detect a console.warn message 如何在 JavaScript 中正确覆盖 console.warn - How to properly override console.warn in JavaScript 如何最好地安全地使用console.log和console.warn? - How to best safely use console.log and console.warn? 如何在 Chrome 开发者控制台中使用 console.log、console.warn 等? - How do I use console.log, console.warn, etc in chromium developer console? 我无法在浏览器中使用 console.log、console.warn、console.error - I cannot use console.log, console.warn, console.error in browser React-Native:不推荐使用propTypes和createClass - React-Native: propTypes and createClass deprecated 使用 vue-test-utils 时,“名称为‘某物’的路由不存在”vue-router console.warn - 'Route with name 'something' does not exist' vue-router console.warn when using vue-test-utils 在babel反应中禁用React.createClass和PropTypes不推荐使用的警告 - Disable React.createClass and PropTypes deprecated warnings in babel react present 笑话和酵素| 在componentDidMount中测试PropTypes函数 - Jest & Enzyme | Test PropTypes Function in componentDidMount 不推荐通过主React包访​​问PropTypes - Accessing PropTypes via the main React package is deprecated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM