gpt4 book ai didi

reactjs - ReactTestUtils 已被移动

转载 作者:行者123 更新时间:2023-12-03 13:07:18 25 4
gpt4 key购买 nike

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

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

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

它们不会阻止测试运行或正确验证,但总是存在此错误。

通过查看文档,我 found this page ,即使在我添加了他们推荐的那些行之后,警告消息仍然显示。

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

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

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

最佳答案

如果您使用的是 React 0.14 或 React <15.5,除了 enzyme 之外,您还必须确保还安装了以下 npm 模块(如果尚未安装):

npm i --save-dev react-addons-test-utils react-dom

如果您使用的是 React >=15.5,除了 enzyme 之外,您还必须确保还安装了以下 npm 模块(如果尚未安装):

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

关于reactjs - ReactTestUtils 已被移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43334942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com