Skip to content

userEvent.setup() exists but I'm unable to call it #1279

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

Open
nene opened this issue Mar 14, 2025 · 1 comment
Open

userEvent.setup() exists but I'm unable to call it #1279

nene opened this issue Mar 14, 2025 · 1 comment
Labels
docs enhancement New feature or request

Comments

@nene
Copy link

nene commented Mar 14, 2025

Reproduction example

import { describe, expect, it } from "vitest";
import userEvent from "@testing-library/user-event";

describe("my test", () => {
  it("should pass", () => {
    console.log(userEvent.setup);
    userEvent.setup();     // <-- fails here
    expect(true).toBe(true);
  });
});

https://github.com/nene/test-userevent

Prerequisites

Sorry, I don't know how to work with tests in codesandbox. Instead I provide a repository:

  1. Check out the repo git clone [email protected]:nene/test-userevent.git
  2. Install dependencies with npm install
  3. Run tests npm test

Expected behavior

Tests execute successfully.

Actual behavior

An error occurs:

 FAIL  src/my.test.ts > my test > should pass
TypeError: Cannot read properties of undefined (reading 'Symbol(Node prepared with document state workarounds)')
 ❯ prepareDocument node_modules/@testing-library/user-event/dist/esm/document/prepareDocument.js:10:17
 ❯ Object.setupMain [as setup] node_modules/@testing-library/user-event/dist/esm/setup/setup.js:52:5
 ❯ src/my.test.ts:7:15
      5|   it("should pass", () => {
      6|     console.log(userEvent.setup);
      7|     userEvent.setup();
       |               ^
      8|     expect(true).toBe(true);
      9|   });

User-event version

14.6.1

Environment

Testing Library framework: @testing-library/dom 10.4.0

JS framework: react 18.3.1 (though not used in test)

Test environment: vitest 3.0.8

DOM implementation: Not sure, Vitest docs say that the default environment is node. I haven't installed any of the optional environments like js-dom or happy-dom.

Additional context

This problem looks similar to #839, but that issue has been fixed, and the workarounds suggested there didn't seem to do anything.

Most strangely, when I console.log(userEvent.setup) it prints:

[Function: setupMain]

Similarly typeof userEvent.setup also tells me that it's a function.

But when I call the function userEvent.setup() I get the error.

I managed to locate this strange Symbol(Node prepared with document state workarounds) inside user-event library. But I have no clue what's going on with that.

@nene nene added bug Something isn't working needs assessment This needs to be looked at by a team member labels Mar 14, 2025
@nene
Copy link
Author

nene commented Mar 14, 2025

I figured out the cause. I had not installed any of the vitest dom environments. After installing happy-dom the test started to work.

So feel free to close this issue. Though it would be nicer if the error message one gets when no DOM environment is present would not be quite as mysterious as this.

@ph-fritsche ph-fritsche added enhancement New feature or request docs and removed bug Something isn't working needs assessment This needs to be looked at by a team member labels Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants