微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

React 测试渲染器 JSON 的打字稿错误没有孩子

如何解决React 测试渲染器 JSON 的打字稿错误没有孩子

我已经使用文档中的基本打字稿模板创建了一个博览会应用程序。安装了测试及其类型所需的所有要求。

我可以运行测试,它确实通过了测试。

但是,我在 example test on the doc 的 IDE(VS 代码)上遇到错误

enter image description here

解决方法

您可以使用 Type Assertions 指定特定类型。

如果 boost::iostreams::mapped_file_params params; params.path = m_input_file_name; params.length = 1024; params.flags = boost::iostreams::mapped_file::mapmode::readonly; boost::iostreams::mapped_file file; file.open(params); if (!file.is_open()) { std::cout << "file is not open" << std::endl; return; } if (file.begin() == file.end()) { std::cout << "file is weird" << std::endl; return; } 组件有一个子组件列表:

App

您可以为 import React,{ Component } from 'react'; export default class App extends Component { render() { return ( <> <div>app</div> <p>123</p> </> ); } } 指定 ReactTestRendererJSON[] 并编写如下测试:

tree

测试结果:

import React from 'react';
import renderer,{ ReactTestRendererJSON } from 'react-test-renderer';
import App from './index';

describe('67900373',() => {
  it('should pass',() => {
    const tree = renderer.create(<App />).toJSON() as ReactTestRendererJSON[];
    console.log(tree);
    tree.forEach((node) => {
      expect(node.children?.length).toBe(1);
    });
  });
});

如果 PASS examples/67900373/index.test.tsx (9.11 s) 67900373 ✓ should pass (28 ms) console.log [ { type: 'div',props: {},children: [ 'app' ] },{ type: 'p',children: [ '123' ] } ] at Object.<anonymous> (examples/67900373/index.test.tsx:8:13) Test Suites: 1 passed,1 total Tests: 1 passed,1 total Snapshots: 0 total Time: 10.006 s 组件只有一个子组件

App

您可以为 import React,{ Component } from 'react'; export default class App extends Component { render() { return <div>app</div>; } } 指定 ReactTestRendererJSON 并编写如下测试:

tree

测试结果:

import React from 'react';
import renderer,() => {
    const tree = renderer.create(<App />).toJSON() as ReactTestRendererJSON;
    console.log(tree);
    expect(tree.children?.length).toBe(1);
  });
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。