简体   繁体   English

使用反应测试库和storyshots?

[英]Using react-testing-library with storyshots?

Is it possible to use react-testing-library with the storybook storyshots addon? 是否有可能将反应测试库与故事书storyshots插件一起使用? I'd like to generate some tests for react components which do not use enzyme. 我想为不使用酶的反应组分进行一些测试。

Probably a bit late for you, but just been looking at this exact problem. 对你来说可能有点晚了,但只是看着这个确切的问题。 This is the config I came up with which seems to work for me. 这是我想出的配置,似乎对我有用。

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).)*$/
});

The secret is to pass in a custom Jest serializer for the snapshotSerializers option that gets the container from the result of the render function and passes its firstChild to be serialized. 秘诀是为snapshotSerializers选项传递一个自定义的Jest 序列化程序 ,该选项从render函数的结果中获取容器并传递其firstChild进行序列化。

If you need to, you can modify the content of the container before serializing, to remove unstable attributes or entire elements using the DOM api. 如果需要,可以在序列化之前修改容器的内容,使用DOM api删除不稳定的属性或整个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM