Skip to content

Case where timeouts are not cleared #45

@petewins

Description

@petewins

In specific testing Environments, it is possible for setTimeout to not clear after each test. Causing my test to fail with a window not defined error

Error I'm recieving:

ReferenceError: window is not defined
❯ Timeout.e [as onTimeout] node_modules/.pnpm/@react-input+core@1.0.12@types+react@18.2.79_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@react-input/core/dist/module/hooks/useInput.js:1:1751

I believe the fix is to always clearTimeout when component is unmounted / unregistered.

Currently, I believe timeouts are only cleared onBlur of the input.

window.clearTimeout(timeout.id);

Workaround

For those Running into a similar error, I was able to avoid this issue by invoking the blur() function after each test so the timeouts are cleared.

afterEach(() => {
  const activeElement = global.document?.activeElement
  if (activeElement && activeElement instanceof HTMLElement) {
    activeElement.blur()
  }
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions