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

Isolated DB throws this error TypeError: The "eventTargets" argument must be an instance of EventEmitter or EventTarget. Received an instance of AbortSignal with npx vitest #39

Open
iammrsea opened this issue Nov 21, 2024 · 5 comments

Comments

@iammrsea
Copy link

iammrsea commented Nov 21, 2024

npx vitest throws the following error "TypeError: The "eventTargets" argument must be an instance of EventEmitter or EventTarget. Received an instance of AbortSignal" with Isolated DB for testing.

A little bit of google searching landed me here, which is the root cause of the problem. You can check out the link for more context. Apparently, execa version 9 which is the current version of the package in the project relies on setMaxListeners export from node:events which throws error in jsdom environment because the AbortSignal from jsdom's AbortController is not node.js EventTarget.

I'm not exactly sure the best way to fix this problem. Of course, one option would be to downgrade execa to version 8 but not sure if that's a good idea based on this comment. However, I added this hack in the db-setup.ts file and it does work for me:

const kEventTarget = Symbol.for('nodejs.event_target') // https://github.com/nodejs/node/blob/68c9f554ffa2fab1ef3b6466c51491371f0dd158/lib/internal/event_target.js#L50

if (globalThis.window !== undefined) {
	const JSDOMAbortController = window.AbortController
	window.AbortController = class extends JSDOMAbortController {
		get signal() {
			const signal = super.signal
			if ('constructor' in signal) {
				// @ts-expect-error
				signal.constructor[kEventTarget] = true //
			} else {
				// @ts-expect-error
				signal.constructor = { [kEventTarget]: true } //https://github.com/nodejs/node/blob/68c9f554ffa2fab1ef3b6466c51491371f0dd158/lib/internal/event_target.js#L1070
			}
			return signal
		}
	}
}
@iammrsea iammrsea changed the title Isolated DB throws this error TypeError: The "eventTargets" argument must be an instance of EventEmitter or EventTarget. Received an instance of AbortSignal Isolated DB throws this error TypeError: The "eventTargets" argument must be an instance of EventEmitter or EventTarget. Received an instance of AbortSignal with npx vitest Nov 21, 2024
@kentcdodds
Copy link
Member

Hello @iammrsea. Can you tell me which exercise has this issue? I was unable to reproduce this.

@iammrsea
Copy link
Author

@kentcdodds, I think the problem started with 01. DB Setup exercise but the reason because more obvious after 02. Isolated DB

@kentcdodds
Copy link
Member

I've successfully reproduced. Thank you.

I have no idea why this would suddenly stop working. I'll have to investigate further. Any tips from others are welcome!

kentcdodds added a commit that referenced this issue Nov 24, 2024
@iammrsea
Copy link
Author

I have no idea why this would suddenly stop working.

I think here is probably the reason. execa v9 now uses setMaxListeners export from node:events module which throws that error because EventTarget in dom environment isn't the same as EventTarget in node environment.

@kentcdodds
Copy link
Member

Ah, well that makes sense and is unfortunate... I wonder if we could get away with switching away from execa for this 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants