javascript - 浅浅和开玩笑渲染有什么区别?
问题描述
开玩笑地说,使用酶的浅层和渲染有什么区别?
以下是两者的示例:
用浅层测试渲染:
import "jest";
import * as React from "react";
import { shallow } from "enzyme";
import Item from "../item.component";
describe("Item", () => {
it("renders correct", () => {
const item = {
name: "A"
};
const component = shallow(
<Item item={item}/>
);
expect(component).toMatchSnapshot();
});
});
使用渲染测试渲染
import "jest";
import * as React from "react";
import { render } from "enzyme";
import Item from "../item.component";
describe("Item", () => {
it("renders correct", () => {
const item = {
name: "A"
};
const component = render(
<Item item={item}/>
);
expect(component).toMatchSnapshot();
});
});
这些 2 的典型用法是什么。我用浅层编写了所有测试,我应该回去更改它以在某些情况下渲染吗?
解决方案
shallow
并且render
是 Enzyme 特有的,可以独立于 Jest 使用。
shallow
仅呈现顶级组件,不需要 DOM。它适用于独立的单元测试。
render
渲染整个组件,用 Cheerio 包装,这是 Node.js 的 jQuery 实现。它旨在借助类 jQuery 选择器进行黑盒集成/e2e 测试。它可能有一些用途,但shallow
很mount
常用。
它们都不应该与 一起使用toMatchSnapshot
,至少没有额外的工具(enzyme-to-json
forshallow
和mount
)。
推荐阅读
- mysql - 按类别分类的产品价格总和作为 MySQL 中的列
- javascript - 从服务器 react-native 动态获取本地化字符串
- ehcache - 如果 LoaderWriter 失败,使 Ehcache 返回过期数据
- c# - 使用 UCMA 创建电话会议
- batch-file - 批处理脚本调用自己的名称而不是某种类型的文件?
- azure - 如何使用 Azure 流分析将本地 SQL Server 用作输出?
- javascript - 如何点击下载图片
- vue.js - 在不同的 url 上有重复的命名路由定义警告
- javascript - 如何阻止任何脚本影响元素?
- google-cloud-platform - 如何使用 gsutil 命令查找对象和谷歌云存储桶的剩余到期时间和日期?