epicweb-dev / epicweb-dev/full-stack-testing
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`
- 主要語言
- TypeScript
- 星號
- 221
- 分支
- 61
- PR 合併指標
- 30 天內沒有已合併 PR
描述
`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](https://github.com/sindresorhus/execa/issues/1123), 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](https://github.com/sindresorhus/execa/issues/1123#issuecomment-2153463691). However, I added this hack in the `db-setup.ts` file and it does work for me:
```ts
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
}
}
}
```
貢獻指南
這個儲存庫沒有索引到貢獻指南
研究方向
首先執行 `npx vitest` 並閱讀 `db-setup.ts`,以重現隔離的 DB 失敗。查看連結的 execa issue,了解 `AbortSignal` 和 `setMaxListeners` 的上下文,然後驗證所選修正是否能讓測試套件在 jsdom 環境中執行而不出現此 TypeError。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- node.js, typescript
- 領域
- databases, testing-qa
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100