Skip to content

Bug: test throws an error when using a MouseEvent with jsdom environment #4685

@brandonroberts

Description

@brandonroberts

Describe the bug

Tests fail when simulating mouse events. The tests pass when using Vitest and happy-dom, and the tests pass when using Jest and jest-environment-dom.

Error message

 FAIL  test/mouse-event.test.ts > repro
TypeError: Failed to construct 'MouseEvent': member view is not of type Window.
 ❯ new UIEventImpl node_modules/jsdom/lib/jsdom/living/events/UIEvent-impl.js:30:15
 ❯ new MouseEventImpl node_modules/jsdom/lib/jsdom/living/events/MouseEvent-impl.js:9:1
 ❯ exports.setup node_modules/jsdom/lib/jsdom/living/generated/MouseEvent.js:59:12
 ❯ new MouseEvent node_modules/jsdom/lib/jsdom/living/generated/MouseEvent.js:117:22
 ❯ eval test/mouse-event.test.ts:9:17
      7| 
      8|   const onClick = vi.fn();
      9|   button.onclick = onClick;
       |                 ^
     10| 
     11|   document.body.append(button);

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-dzgdze?file=vite.config.ts&initialPath=__vitest__/

System Info

System:
    OS: macOS 12.6.8
    CPU: (10) arm64 Apple M1 Pro
    Memory: 115.47 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node
    Yarn: 1.22.15 - ~/.yarn/bin/yarn
    npm: 9.6.7 - ~/.nvm/versions/node/v18.17.1/bin/npm
    pnpm: 8.10.5 - ~/.nvm/versions/node/v18.17.1/bin/pnpm
    bun: 1.0.2 - ~/.bun/bin/bun
  Browsers:
    Chrome: 119.0.6045.199
    Safari: 15.6.1
  npmPackages:
    @vitest/coverage-v8: ^0.34.6 => 0.34.6 
    @vitest/ui: ^0.34.6 => 0.34.6 
    vite: ^4.4.8 => 4.5.0 
    vitest: ^1.0.0 => 1.0.1

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions