Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.8 KB
Raw Blame
rules:
    - id: jest-no-byrole-name-queries
      message: |
          `*ByRole(..., { name: ... })` in a jest/jsdom test. Role queries walk
          every element in the DOM, running `getComputedStyle`-based
          inaccessibility checks and computing accessible names as they go. In
          jsdom that costs tens of milliseconds per candidate element, so on a
          component that renders a large DOM (calendars with time columns, the
          taxonomic filter, whole scenes/dashboards) a single query takes
          seconds — and inside `waitFor`/`findBy*` the cost multiplies with
          every retry. This pattern has produced individual tests with p95
          durations of 40s in CI (see PostHog/posthog#68733).

          Prefer, in order:
            • `getByText(...)` when the assertion is about visible text
            • `getByTestId(...)` / a `data-attr` querySelector for interactions
            • `getByLabelText(...)` when the element is identified by aria-label
            • scope with `within(<small container>)` if a role query is genuinely needed

          Role+name queries on small DOMs (a single button, a dialog) are
          sometimes chosen deliberately, e.g. to assert a component's
          accessibility contract or to survive a markup swap. If yours is one
          of those, add `// nosemgrep: jest-no-byrole-name-queries` with a
          short reason — but check the suite's runtime first.
      severity: ERROR
      languages: [typescript, javascript]
      paths:
          include:
              - '**/*.test.ts'
              - '**/*.test.tsx'
      pattern-regex: '(?:get|query|find)(?:All)?ByRole\(\s*[''"][a-z]+[''"]\s*,\s*\{[^}]*\bname\s*:'
      metadata:
          category: best-practice
          subcategory:
              - audit