简体   繁体   English

测试库自定义查询打字稿错误

[英]Testing-library custom query Typescript error

In a current project using react with jest and @testing-library/react , I'm implementing custom queries to query the text content of components.在当前使用react with jest@testing-library/react ,我正在实施自定义查询来查询组件的文本内容

The custom query (built with testing-library's buildQueries method) I want to use receives two parameters (one is optional).我想使用的自定义查询(使用 testing-library 的buildQueries方法buildQueries )接收两个参数(一个是可选的)。 When I want to add a second parameter, Typescript tells me that only one argument can be passed.当我想添加第二个参数时,Typescript 告诉我只能传递一个参数。

const queryAllByTextContent = (
  container: HTMLElement,
  text: string | RegExp,
  options?: QueryParameterOptions
): HTMLElement[] => {
  const { exact = false } = options ?? { };

  return queryAllByText(container, (content, node): boolean => {
    if (!node) {
      return false;
    }
    const nodeHasSearchText = nodeContainsSearchText({ searchText: text, exact, node });
    // eslint-disable-next-line unicorn/prefer-spread
    const childNodesDontHaveSearchText = Array.from(node.children).every(
      (childNode): boolean => !nodeContainsSearchText({ searchText: text, exact, node: childNode })
    );

    return nodeHasSearchText && childNodesDontHaveSearchText;
  });
};

const getMultipleError = (
  container: Element | null,
  text: string | RegExp
): string => `Found multiple elements with the text: ${text}`;
const getMissingError = (
  container: Element | null,
  text: string | RegExp
): string => `Unable to find an element with the text: ${text}`;

const [
  queryByTextContent,
  getAllByTextContent,
  getByTextContent,
  findAllByTextContent,
  findByTextContent
] = buildQueries(queryAllByTextContent, getMultipleError, getMissingError);

I have created a Github repository with a working example:我用一个工作示例创建了一个 Github 存储库:

https://github.com/desudo/example-jest-with-react-testing-library https://github.com/desudo/example-jest-with-react-testing-library

Most important files are:最重要的文件是:

/test/helpers/textContentQueries.ts -> queryAllByTextContent /test/component/Price.test.tsx /test/helpers/textContentQueries.ts -> queryAllByTextContent /test/component/Price.test.tsx

What I found out :我发现了什么

The first parameter of the custom query has to be of type string | RegExp自定义查询的第一个参数的类型必须string | RegExp string | RegExp . string | RegExp When I set the type to just a string, everything works as intended.当我将类型设置为只是一个字符串,一切都会按计划。

IMPORTANT重要的

The example tests are just dummy tests to show the Typescript error.示例测试只是用于显示 Typescript 错误的虚拟测试。


Thank you for any hint or solution!感谢您的任何提示或解决方案!

One way of fixing this issue is to add the parameter options?: QueryParameterOptions to both getMultipleError and getMissingError .解决此问题的一种方法是将参数options?: QueryParameterOptions添加到getMultipleErrorgetMissingError

This is due to the type that is being defined under the hood of buildQueries .这是由于在buildQueries引擎盖下定义的类型。

I certainly do not like this solution because we are not using the options parameter in either of these two methods.我当然不喜欢这个解决方案,因为我们没有在这两种方法中使用 options 参数。

暂无
暂无

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

相关问题 使用 Jest 和 @testing-library/react-hooks 在 TypeScript 中单元测试自定义 React Hook - Unit Testing Custom React Hooks in TypeScript using Jest and @testing-library/react-hooks 使用 fireEvent 传递自定义事件属性(testing-library 和 jest) - pass custom event properties with fireEvent (testing-library and jest) testing-library 错误:mockConstructor(…): 没有从渲染返回 - testing-library Error: mockConstructor(…): Nothing was returned from render 自定义 React 组件库 - 开玩笑“找不到模块反应”- 测试库,汇总 - Custom React Component Library - jest 'cannot find module react'- testing-library, rollup ts2339怎么解决? 使用带有测试库/反应的打字稿 - How to solve ts2339? Using typescript with testing-library/react 反应测试 | 从“@testing-library/user-event”导入 userEvent 会生成错误“toBeInTheDocument 不是函数” - React Testing | importing userEvent from '@testing-library/user-event' generates error "toBeInTheDocument is not a function" useFakeTimers 在玩笑/测试库中不起作用 - useFakeTimers not working in jest/testing-library @testing-library/dom window.getComputedStyle 即使设置了computedStyleSupportsPseudoElements,也会出现“未实现”错误:true - @testing-library/dom window.getComputedStyle “Not implemented” error even after setting computedStyleSupportsPseudoElements: true 测试库无法设置只有 getter 的 [object Object] 的属性错误 - Testing-library Cannot set property error of [object Object] which has only a getter testing-library/react 如何在动态生成错误消息时测试无效输入 - testing-library/react how to test invalid inputs when they dynamically generate error message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM