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

Error when using with @testing-library/user-event / @testing-library/dom #544

Closed
benbender opened this issue Jul 18, 2022 · 1 comment · Fixed by #611
Closed

Error when using with @testing-library/user-event / @testing-library/dom #544

benbender opened this issue Jul 18, 2022 · 1 comment · Fixed by #611
Assignees
Labels
bug Something isn't working

Comments

@benbender
Copy link

benbender commented Jul 18, 2022

I'm trying to use happy-dom together with @testing-library/user-event or @testing-library/dom. When I try to emulate a click (via fireEvent or userEvent.click, I'm seeing the following error:

TypeError: Cannot set property currentTarget of #<MouseEvent> which has only a getter
  GlobalWindow.dispatchEvent node_modules/.pnpm/happy-dom@6.0.4/node_modules/happy-dom/lib/event/EventTarget.js:44:29
  HTMLDocument.dispatchEvent node_modules/.pnpm/happy-dom@6.0.4/node_modules/happy-dom/lib/nodes/document/Document.js:698:37
  HTMLElement.dispatchEvent node_modules/.pnpm/happy-dom@6.0.4/node_modules/happy-dom/lib/nodes/node/Node.js:354:40
  HTMLElement.dispatchEvent node_modules/.pnpm/happy-dom@6.0.4/node_modules/happy-dom/lib/nodes/node/Node.js:354:40
  HTMLElement.dispatchEvent node_modules/.pnpm/happy-dom@6.0.4/node_modules/happy-dom/lib/nodes/node/Node.js:354:40
  HTMLElement.dispatchEvent node_modules/.pnpm/happy-dom@6.0.4/node_modules/happy-dom/lib/nodes/node/Node.js:354:40
  node_modules/.pnpm/@testing-library+user-event@14.2.5_gwcpuyfvwbszhlmedmugzivgzu/node_modules/@testing-library/user-event/dist/index.mjs:1733:33
    1731|     return !defaultPrevented;
    1732|   }
    1733|   return wrapEvent(() => target.dispatchEvent(event), target);
       |                                 ^
    1734| }
    1735|
  Object.eventWrapper node_modules/.pnpm/@testing-library+dom@8.16.0/node_modules/@testing-library/dom/dist/config.js:27:23
  wrapEvent node_modules/.pnpm/@testing-library+user-event@14.2.5_gwcpuyfvwbszhlmedmugzivgzu/node_modules/@testing-library/user-event/dist/index.mjs:1707:23
  dispatchEvent node_modules/.pnpm/@testing-library+user-event@14.2.5_gwcpuyfvwbszhlmedmugzivgzu/node_modules/@testing-library/user-event/dist/index.mjs:1733:10

The reason is that event.target and event.currentTarget are readonly. I've provided a pr to fix the problem.

@benbender benbender changed the title Typescript error when using happy-dom with @testing-library/user-event Error when using with @testing-library/user-event / @testing-library/dom Jul 18, 2022
capricorn86 added a commit that referenced this issue Oct 11, 2022
…ly to solve a problem with Vitest typechecking the internals of Happy DOM.
capricorn86 added a commit that referenced this issue Oct 11, 2022
…th-testing-libraryuser-event-testing-librarydom

#544@patch: Does not set Event.target and Event.currrentTarget direct…
@capricorn86 capricorn86 self-assigned this Oct 11, 2022
@capricorn86 capricorn86 added the bug Something isn't working label Oct 11, 2022
@capricorn86
Copy link
Owner

Thanks again for reporting @benbender! 🙂

I have created a fix that will not set Event.target and Event.currentTarget directly. However, you will probably run into more problems if you let Vitest transpile external libraries. You need to change the transform config to only transform your app source code to avoid this problem.

    transformMode: {
      web: [/src\/\.[tj]sx?$/]
    }

You can read more about the release here:
https://github.com/capricorn86/happy-dom/releases/tag/v7.5.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants