diff --git a/src/node/SetupServerApi.ts b/src/node/SetupServerApi.ts index a451f521b..4eb026a6a 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/isNodeException' 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 d66686ede..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 && typeof error === 'object' && '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 +}