reactjs - Jest 快照测试在 CI 快照中的 React HOC 中添加了一个“_class”字,但在我的机器上没有
问题描述
我有一个导入另一个 React HOC 的 React 组件。
我的反应 HOC:
import { withRouter } from "react-router";
...
const InnerComponent: FC<any> = (props): ReactElement => {
...
}
const withData = compose(
graphql(SOME_QUERY, {
props: mapProps,
options,
}),
injectIntl
);
export default withRouter(withX(withData(InnerComponent)));
我的反应组件:
export const OuterComponent: FC<any> = (props): ReactElement => {
...
return (
...
<InnerComponent />
...
)}
这是我的测试规范文件的摘录(我使用酶和玩笑):
const component = shallow(<OuterComponent {...props} />)
expect(component).toMatchSnapshot()
在本地,一切正常。我的 InnerComponent 应该被渲染的部分在快照中是这样表示的:
<withRouter() />
当我签入我的代码并让它在 CI 上运行时,快照测试失败,因为快照 CI 生成的该行如下所示:
<withRouter(_class) />
我确信 CI 和我的机器上的酶和开玩笑版本是相同的。我什至尝试在我的机器上更新 jest 和酵素,但从来没有得到包含这个奇怪_class
词的快照。
这_class
是从哪里来的,我该如何避免它,或者如何确保我的本地设置也在创建快照期间生成它?
解决方案
这个问题可以通过displayName
为我们的自定义 HOC 设置适当的来避免。在这种情况下,withX
HOC 没有任何displayName
. 我将其更改为如下所示,以便在本地计算机和构建服务器中生成具有相同名称的快照中的 HOC。
export function withX(WrappedComponent) {
class WithX extends React.Component {
render() {
...
return <WrappedComponent {...this.props} />
}
}
WithX.displayName = `WithX(${getDisplayName(WrappedComponent)})`
return WithX
}
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component'
}
不过,这仍然不能解释为什么 HOC 在本地机器上没有名称,但_class
在构建服务器上有名称。
推荐阅读
- php - MySQL 基于 2 列顺序拆分结果
- python-3.x - Tkinter在Button1中捕获两个不同按钮的位置单击并释放
- java - 如何检查二维数组是否为拉丁文
- vba - 无法使用 VBA 在 Solver 中设置整数约束
- r - 将 xts 转换为 tibble 的最佳方法是什么
- java - HTML 内容损坏
- apache-kafka - 为什么 Apache Kafka 不支持临时主题?
- javascript - Javascript querySelector 获取值条件
- php - Laravel Route::current() 返回 null
- perl - Perl 语法(替换 'defined(@array')