Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@typescript-eslint/unbound-method triggered when destructuring from render #7

Closed
Stadly opened this issue Jan 27, 2025 · 0 comments · Fixed by vitest-dev/vitest#7422

Comments

@Stadly
Copy link

Stadly commented Jan 27, 2025

Destructuring getBy functions from the render function's returned value triggers ESLint errors when using the @typescript-eslint/unbound-method rule (included by default in the recommended-type-checked config).

Image

This is because the functions are declared as class methods, and hence may rely on this:

interface LocatorSelectors {
  getByRole(role: ARIARole | ({} & string), options?: LocatorByRoleOptions): Locator
  getByLabelText(text: string | RegExp, options?: LocatorOptions): Locator
  getByAltText(text: string | RegExp, options?: LocatorOptions): Locator
  getByPlaceholder(text: string | RegExp, options?: LocatorOptions): Locator
  getByText(text: string | RegExp, options?: LocatorOptions): Locator
  getByTitle(text: string | RegExp, options?: LocatorOptions): Locator
  getByTestId(text: string | RegExp): Locator
}

The solution is to either annotate the functions with this: void or use arrow functions:

interface LocatorSelectors {
  getByRole: (role: ARIARole | ({} & string), options?: LocatorByRoleOptions) => Locator
  getByLabelText: (text: string | RegExp, options?: LocatorOptions) => Locator
  getByAltText: (text: string | RegExp, options?: LocatorOptions) => Locator
  getByPlaceholder: (text: string | RegExp, options?: LocatorOptions) => Locator
  getByText: (text: string | RegExp, options?: LocatorOptions) => Locator
  getByTitle: (text: string | RegExp, options?: LocatorOptions) => Locator
  getByTestId: (text: string | RegExp) => Locator
}

The render function in @testing-library/svelte does not have this issue.

A similar fix has just been merged in SvelteKit: sveltejs/kit#12955

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant