Skip to content

findBy* & findAllBy* have wrong argument types for TS #534

Closed
@denysdovhan

Description

@denysdovhan
  • DOM Testing Library version: 7.2.0
  • node version: 12.13.0
  • yarn version: 1.22

Relevant code or config:

export const queryAllByAttribute = (
    container: HTMLElement,
    attribute: string,
    value: Matcher,
    options?: MatcherOptions
) => queryHelpers.queryAllByAttribute(attribute, container, value, options);

const includesAutomationId = (content: string, automationId: string) =>
    content.split(/\s+/).some(id => id === automationId);

export const queryAllByAutomationId = (
    container: HTMLElement,
    automationId: string | string[],
    options?: MatcherOptions
) =>
    queryAllByAttribute(
        container,
        TEST_ID_ATTRIBUTE,
        content => Array.isArray(automationId) ?
            automationId.every(id => includesAutomationId(content, id)) :
            includesAutomationId(content, automationId)
        ,
        options
    );

export const [
    queryByAutomationId,
    getAllByAutomationId,
    getByAutomationId,
    findAllByAutomationId,
    findByAutomationId
] = buildQueries(queryAllByAutomationId, getMultipleError, getMissingError);

const waitForOptions = { timeout: 2000 }

findByAutomationId(root, testId, options, waitForOptions), // <-  Expected 2-3 arguments, but got 4.ts(2554)
findAllByAutomationId(root, testId, options, waitForOptions), // <-  Expected 2-3 arguments, but got 4.ts(2554)

What you did:

I generated custom findBy and findAllBy queries by buildQueries.

What happened:

Generated queries have lost TypeScript types for 4th argument waitForOptions.

Problem description:

I've noticed all findBy*/findAllBy* methods have 4th param, for example:

export type FindAllByText = (
    container: HTMLElement,
    id: Matcher,
    options?: SelectorMatcherOptions,
    waitForElementOptions?: WaitForElementOptions,
) => Promise<HTMLElement[]>;

However, buildQueries returns FindBy and FindAllBy queries without that param:

export type QueryMethod<Arguments extends any[], Return> = (container: HTMLElement, ...args: Arguments) => Return;

export type FindAllBy<Arguments extends any[]> = QueryMethod<Arguments, Promise<HTMLElement[]>>;
export type FindBy<Arguments extends any[]> = QueryMethod<Arguments, Promise<HTMLElement>>;

Suggested solution:

Add 4th param type to the QueryMethod type or make special FindQueryMethod type with that param.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions