【问题标题】:Using react-testing-library with storyshots?将 react-testing-library 与 storyshots 一起使用?
【发布时间】:2019-02-01 09:35:00
【问题描述】:

是否可以将 react-testing-library 与 storybook storyshots 插件一起使用?我想为不使用酶的反应成分生成一些测试。

【问题讨论】:

标签: reactjs storybook react-testing-library


【解决方案1】:

对您来说可能有点晚了,但只是在研究这个确切的问题。这是我想出的配置,似乎对我有用。

import initStoryshots from "@storybook/addon-storyshots";
import path from "path";
import { render } from "react-testing-library";


const reactTestingLibrarySerializer = {
  print: (val, serialize, indent) => serialize(val.container.firstChild),
  test: (val) => val && val.hasOwnProperty("container")
};

initStoryshots({
  configPath: path.join(__dirname, "..", "config", "storybook"),
  framework: "react",
  integrityOptions: { cwd: path.join(__dirname, "stories") },
  renderer: render,
  snapshotSerializers: [reactTestingLibrarySerializer],
  storyKindRegex: /^((?!.*?DontTest).)*$/
});

秘诀是为 snapshotSerializers 选项传入一个自定义的 Jest serializer,该选项从渲染函数的结果中获取容器并传递其 firstChild 以进行序列化。

如果需要,您可以在序列化之前修改容器的内容,以使用 DOM api 移除不稳定的属性或整个元素。

【讨论】:

  • 必须补充一点,在我有限的经验中,在这种情况下,它比 Enzyme 替代品更快、更强大。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-27
  • 2019-07-07
  • 2019-05-21
  • 2019-11-11
  • 1970-01-01
  • 2021-06-02
  • 2023-04-04
相关资源
最近更新 更多