简体   繁体   English

使用react-test-renderer进行快照测试

[英]snapshot testing with react-test-renderer

I'm trying to create a test case for App component which has the following providers. 我正在尝试为具有以下提供程序的App组件创建测试用例。

  • Store Provider 商店供应商
  • Translation provider 翻译提供者
  • redux-persist 持久化
  • Router 路由器

I'm trying to render the app which certain state, to ensure the same app is rendered. 我正在尝试呈现某个特定状态的应用程序,以确保呈现相同的应用程序。 The test case passes, but I get the following as snapshot. 测试用例通过了,但是我得到了以下快照。

// Jest Snapshot v1, [link to jest docs]

exports[`This should Render App 1`] = `null`;

exports[`This should Render App 2`] = `null`;

My test case is like this, 我的测试用例是这样的,

const component = renderer.create(
         <App store={store} persistor={persistor} />,
  );
  let tree = component.toJSON();
  expect(tree).toMatchSnapshot();

tree is not null and has the rendered component. tree不为null,具有呈现的组件。 But I do not see this being written in snapshot. 但是我看不到它写在快照中。

npm package versions npm软件包版本

"jest": "^22.4.4",
"react-test-renderer": "^16.4.0",

Jest doc 开玩笑的文件

I was able to fix the issue by using .toTree() method available in react-test-renderer instead of toJSON() . 我能够通过使用react-test-renderer可用的.toTree()方法而不是toJSON()来解决此问题。

toMatchSnapshot() of jest seems to accept only ReactTestRendererTree as apposed to ReactTestRendererJSON . toMatchSnapshot()开玩笑的,似乎只接受ReactTestRendererTree作为并列于ReactTestRendererJSON

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

相关问题 使用 react-test-renderer 测试异步 componentDidMount() - Testing async componentDidMount() with react-test-renderer 在使用 react-test-renderer 的 Jest 快照测试中,Refs 为空 - Refs are null in Jest snapshot tests with react-test-renderer 使用 React-Test-Renderer 测试嵌套的 React 组件 - Testing of Nested React components using React-Test-Renderer 测试 Formik 使用 react-test-renderer 验证 function - Testing Formik validate function with react-test-renderer "在 react-test-renderer 测试中渲染 jest 快照之前运行 useEffect 钩子" - Run useEffect hook before rendering jest snapshot in react-test-renderer test 如何使用@testing-library/react 和 react-test-renderer 测试由 Redux 状态控制的输入值? - how to test input value controlled by Redux state using @testing-library/react and react-test-renderer? react-test-renderer不正确的对等依赖 - react-test-renderer incorrect peer dependency react-test-renderer 的 create() 与 @testing-library/react 的 render() - react-test-renderer's create() vs. @testing-library/react's render() react-test-renderer浅渲染器在生产模式下不可用 - react-test-renderer shallow renderer is not available in production mode 使用Jest和React-test-renderer测试伪类 - Test for pseudo class using Jest and React-test-renderer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM