ReactTestUtils 已被移动 [英] ReactTestUtils has been moved

查看:55
本文介绍了ReactTestUtils 已被移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始学习 React,在进行一些测试时,我注意到两条警告消息:

I'm starting learning React and while I was doing some tests i noticed two warning messages:

警告:ReactTestUtils 已移至 react-dom/test-utils.更新参考以删除此警告.

Warning: ReactTestUtils has been moved to react-dom/test-utils. Update references to remove this warning.

警告:浅渲染器已移至 react-test-renderer/shallow.更新参考以删除此警告.

Warning: Shallow renderer has been moved to react-test-renderer/shallow. Update references to remove this warning.

它们不会阻止测试运行或正确验证,但始终存在此错误.

They don't prevent the tests from running nor from properly validating, but there is always this error.

通过查看文档,我 找到此页面,即使在我添加了他们推荐的那些行之后,警告消息仍然显示.

By looking at the docs, I found this page, even after I included those lines they recommend, the warning message is still showing up.

我正在尝试一个非常简单的测试,这是我的代码:

I'm trying a very simple test to start with, this is my code:

import React from "react";
import toJson from "enzyme-to-json";
import { shallow } from "enzyme";
import { About } from "./About";

describe('Rendering test', () => {
    const component = shallow(<About />);
    const tree      = toJson(component);

    it('Should render the About component', () => {
        expect(tree).toMatchSnapshot();
    })

    it('Should not contain an h2 element', () => {
        expect( component.contains('h2') ).toBe(false);
    })
})

我需要做什么才能解决此警告?我已经将我所有的打包更新到了​​最新版本.

What do I need to do in order to solve this warnings? I already updated all my packaged to the latest versions.

推荐答案

我认为它来自使用酶的 shallow 渲染函数,该函数尚未针对 v15.5 更新(有一个拉取请求).

I think it's coming from using the shallow render function from enzyme, which has not been updated for v15.5 yet (there is a pull request for it though).

您可以尝试使用其他渲染功能之一(rendermount),但这会改变测试的语义(并且可能会或可能不会仍然产生警告).

You can try to use one of the other render function (render or mount), but this will change the semantics of your test (and may or may not still produce the warning).

您的另一个选择是不使用酶并使用 react-test-renderer/shallow 自己,但酶 API 非常适合测试组件.

Your other option is to not use enzyme and use react-test-renderer/shallow yourself, but the enzyme API is quite nice for testing components.

我的建议是等待酶的版本,暂时接受警告.

My advice is to wait for the version of enzyme and just live with the warning for now.

这篇关于ReactTestUtils 已被移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆