From f6e5112fa0f1ebbd97fb95bcbda1c47a0cc750f9 Mon Sep 17 00:00:00 2001 From: Matthew Costabile Date: Wed, 1 Nov 2023 07:25:17 -0400 Subject: [PATCH] fix: resolve the "eventTargets" error in JSDOM by dropping "instanceof" (#1813) --- src/node/SetupServerApi.ts | 7 +++++-- src/node/utils/isNodeException.ts | 10 ---------- src/node/utils/isNodeExceptionLike.ts | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 12 deletions(-) delete mode 100644 src/node/utils/isNodeException.ts create mode 100644 src/node/utils/isNodeExceptionLike.ts diff --git a/src/node/SetupServerApi.ts b/src/node/SetupServerApi.ts index a451f521b..ab69b4175 100644 --- a/src/node/SetupServerApi.ts +++ b/src/node/SetupServerApi.ts @@ -14,7 +14,7 @@ import { mergeRight } from '~/core/utils/internal/mergeRight' import { handleRequest } from '~/core/utils/handleRequest' import { devUtils } from '~/core/utils/internal/devUtils' import { SetupServer } from './glossary' -import { isNodeException } from './utils/isNodeException' +import { isNodeExceptionLike } from './utils/isNodeExceptionLike' const DEFAULT_LISTEN_OPTIONS: RequiredDeep = { onUnhandledRequest: 'warn', @@ -80,7 +80,10 @@ export class SetupServerApi * which won't be printed anyway if `setMaxListeners` fails. */ if ( - !(isNodeException(error) && error.code === 'ERR_INVALID_ARG_TYPE') + !( + isNodeExceptionLike(error) && + error.code === 'ERR_INVALID_ARG_TYPE' + ) ) { throw error } diff --git a/src/node/utils/isNodeException.ts b/src/node/utils/isNodeException.ts deleted file mode 100644 index 268e5b8a5..000000000 --- a/src/node/utils/isNodeException.ts +++ /dev/null @@ -1,10 +0,0 @@ -/** - * Determines if the given value is a Node.js exception. - * Node.js exceptions have additional information, like - * the `code` and `errno` properties. - */ -export function isNodeException( - error: unknown, -): error is NodeJS.ErrnoException { - return error instanceof Error && 'code' in error -} diff --git a/src/node/utils/isNodeExceptionLike.ts b/src/node/utils/isNodeExceptionLike.ts new file mode 100644 index 000000000..65b6af6a3 --- /dev/null +++ b/src/node/utils/isNodeExceptionLike.ts @@ -0,0 +1,14 @@ +/** + * Determines if the given value is shaped like a Node.js exception. + * Node.js exceptions have additional information, like + * the `code` and `errno` properties. + * + * In some environments, particularly jsdom/jest these may not + * instances of `Error` or its subclasses, despite being similar + * to them. + */ +export function isNodeExceptionLike( + error: unknown, +): error is NodeJS.ErrnoException { + return !!error && typeof error === 'object' && 'code' in error +}