1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

fix(node/assert): throws not checking error instance (#24466)

The implementation for `assert.throws()` from `node:assert` didn't work
when the expected value was an `Error` constructor. In this case the
thrown error should checked if it's an instance of said constructor.

Fixes https://github.com/denoland/deno/issues/24464
This commit is contained in:
Marvin Hagemeister 2024-07-08 21:28:39 +02:00 committed by GitHub
parent 86010bec09
commit b338b541ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 2 deletions

View file

@ -17,6 +17,9 @@ import {
ERR_MISSING_ARGS,
} from "ext:deno_node/internal/errors.ts";
import { isDeepEqual } from "ext:deno_node/internal/util/comparisons.ts";
import { primordials } from "ext:core/mod.js";
const { ObjectPrototypeIsPrototypeOf } = primordials;
function innerFail(obj: {
actual?: unknown;
@ -744,8 +747,8 @@ function validateThrownError(
error = undefined;
}
if (
error instanceof Function && error.prototype !== undefined &&
error.prototype instanceof Error
typeof error === "function" &&
(error === Error || ObjectPrototypeIsPrototypeOf(Error, error))
) {
// error is a constructor
if (e instanceof error) {

View file

@ -53,6 +53,7 @@ util::unit_test_factory!(
_fs_writeFile_test = _fs / _fs_writeFile_test,
_fs_write_test = _fs / _fs_write_test,
async_hooks_test,
assert_test,
assertion_error_test,
buffer_test,
child_process_test,

View file

@ -0,0 +1,18 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import * as assert from "node:assert";
Deno.test("[node/assert] .throws() compares Error instance", () => {
assert.throws(
() => {
throw new Error("FAIL");
},
Error,
);
assert.throws(
() => {
throw new TypeError("FAIL");
},
TypeError,
);
});