Skip to content

Typescript: buildQueries should pass Arguments through to GetErrorFunction #1037

Closed
@dturcotte

Description

@dturcotte
  • @testing-library/dom version: 8.6.0 (via @testing-library/[email protected]
  • Testing Framework and version: jest 26.6.3
  • DOM Environment: jsdom 16.4.0

Relevant code or config:

This is likely a small oversight from this PR:
https://github.com/testing-library/dom-testing-library/pull/1016/files#diff-bd112f388b709e19de79e65dceba95e350e0cd11b3aa198813c67a0121055792R70-L65

The resulting types:

export function buildQueries<Arguments extends any[]>(
  queryAllBy: GetAllBy<Arguments>,
  getMultipleError: GetErrorFunction,
  getMissingError: GetErrorFunction,
): BuiltQueryMethods<Arguments>

What you did:

Here's a contrived example of a custom query that takes two arguments. Let's try to use these arguments to show a better error message:

buildQueries(
  (container: HTMLElement, key: string | RegExp, value: string | RegExp) =>
    doMagic(container, key, value),
  (_container, ...args) => {
    const [key, value] = args;
    return `Found multiple with key ${key.toString()} and value ${value.toString()}`;
  },
  () => `Found none`,
);

What happened:

Screen Shot 2021-09-23 at 2 39 35 PM

The types for the arguments to GetErrorFunction are stuck at the default. At runtime, you can log ...args and see that the expected key and value are passed.

Reproduction:

I can try to put together a TS repo if you need it, but here's all the code you need to see it:

import { buildQueries } from '@testing-library/react';

const doMagic = (_args: {
  container: HTMLElement;
  key: string | RegExp;
  value: string | RegExp;
}) => [];

buildQueries(
  (container: HTMLElement, key: string | RegExp, value: string | RegExp) =>
    doMagic({ container, key, value }),
  (_container, ...args) => {
    const [key, value] = args;
    return `Found multiple with key ${key.toString()} and value ${value.toString()}`;
  },
  () => `Found none`,
);

Problem description:

The types should match the intent and the runtime result and the GetErrorFunctions for multiple/none in custom queries should accept the same Arguments

Suggested solution:

Forwarding Arguments to GetErrorFunction should do the trick

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions