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

TypeError: activeElement.detachEvent is not a function #534

Closed
hornta opened this issue Jul 8, 2022 · 15 comments · Fixed by #598
Closed

TypeError: activeElement.detachEvent is not a function #534

hornta opened this issue Jul 8, 2022 · 15 comments · Fixed by #598
Assignees
Labels
bug Something isn't working

Comments

@hornta
Copy link

hornta commented Jul 8, 2022

Works in jsdom
https://stackblitz.com/edit/vitest-dev-vitest-ktnq7h?file=test%2Fhappy-dom.test.tsx&initialPath=__vitest__

@frankandrobot
Copy link

Seeing this as well... somehow related to ant design components. Do you mind placing some more details here @hornta ?

@soker90
Copy link

soker90 commented Jul 12, 2022

Same error with happy-dom + testing-library:

import { it } from 'vitest'
import { render } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import MyComponent from './MyComponent'

const user = userEvent.setup()

it('my test', async () => {
  const { getByPlaceholderText } = render(<MyComponent />)
  const input = getByPlaceholderText('any')

  await user.type(input, 'hello') // Crashs here
})

// vite.config.ts

/// <reference types="vitest" />
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

export default defineConfig({
  plugins: [react()],
  test: {
    globals: true,
    environment: 'happy-dom',
    coverage: {
      reporter: ['json', 'html'],
      exclude: [
        'node_modules/'
      ]
    }
  }
})

@hornta
Copy link
Author

hornta commented Jul 12, 2022

Seeing this as well... somehow related to ant design components. Do you mind placing some more details here @hornta ?

I don't know anything about ant design components. I think the bug triggers on different types of code in each code base so it's not trivial figuring out why it occurs but I posted a failing test in the link.

@doytch
Copy link

doytch commented Jul 14, 2022

Yeah I'm running into this as well in a Remix/Vitest/RTL setup. blur events on a textfield seems to be what triggers it for me - whether that's via a simulated tab, clicking off, or manually firing a blur.

@cloud-walker
Copy link

Hi guys, any workaround on this?

@doytch
Copy link

doytch commented Jul 28, 2022

Hi guys, any workaround on this?

Depends what you consider a workaround… We swapped our Vitest unit tests over to ole jsdom with zero friction.

@frankandrobot
Copy link

frankandrobot commented Jul 28, 2022 via email

@cloud-walker
Copy link

I'm currently trying using jsdom selectively on affected test files

image

@aaronmccall
Copy link

Further info:

It looks like react-dom is considering happy-dom to be an IE9 environment due to feature detection, which is why it is trying to call detachEvent.

The detection appears to be due to both document and div element not supporting oninput attribute to set an input handler.

@capricorn86, does happy-dom not support on${eventName} attributes?

@aaronmccall
Copy link

Follow-up to my previous:

I can confirm that adding the following to EventTarget eliminates the error (though there is still the implementation issue that is causing react-dom to consider happy-dom to be IE9):

        /**
	 * Removes an event listener.
	 *
	 * This is only supported by IE8- and Opera, but for some reason React uses it and calls it, so therefore we will keep support for it until they stop using it.
	 *
	 * @param type Event type.
	 * @param listener Listener.
	 */
	public detachEvent(type: string, listener: ((event: Event) => void) | IEventListener): void {
		this.removeEventListener(type.replace('on', ''), listener);
	}

@frankandrobot
Copy link

@aaronmccall we are hitting this issue again. Is there a way to apply this workaround to the test config? (Note: your CR may or may not land since it doesn't address the underlying issue but in the mean time, we need to be unblocked :-) )

@aaronmccall
Copy link

@aaronmccall we are hitting this issue again. Is there a way to apply this workaround to the test config? (Note: your CR may or may not land since it doesn't address the underlying issue but in the mean time, we need to be unblocked :-) )

@frankandrobot I'm not sure. I'll let you know when I take a look next. (I'm not actually depending on happy-dom in my project yet.)

@nukeop
Copy link

nukeop commented Sep 3, 2022

I'm seeing this in vitest now, in a test that types text into an input.

@ilteoood
Copy link

ilteoood commented Sep 5, 2022

@frankandrobot
To make it work, in the setupTests file, I've added the following line:

global.HTMLElement.prototype.detachEvent = function(type, listener) {
  this.removeEventListener(type.replace('on', ''), listener)
}

capricorn86 added a commit that referenced this issue Oct 7, 2022
capricorn86 added a commit that referenced this issue Oct 7, 2022
…types. This will solve the problem of React believing that Happy DOM is a legacy browser which caused an error where it could not find the method detachEvent().
@capricorn86 capricorn86 self-assigned this Oct 7, 2022
@capricorn86
Copy link
Owner

Hello everybody! 👋

Sorry that it took such a long time for me to look into this issue. It seems like a problem that probably affects many users. It has been a lot going on in my private and work life.

I have created a fix that fixes the root cause of the problem, which is that React believes that Happy DOM is a legacy browser. React could not find all the "on{event}" properties in the Document, which is something @aaronmccall discovered and mentioned in his pull request. Without him indicating where the problem was, I would not have been able to fix it this quickly.

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

@capricorn86 capricorn86 added the bug Something isn't working label Oct 7, 2022
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
Development

Successfully merging a pull request may close this issue.

9 participants