Skip to content

Commit

Permalink
fix(node/assert): throws not checking error instance (#24466)
Browse files Browse the repository at this point in the history
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 #24464
  • Loading branch information
marvinhagemeister committed Jul 8, 2024
1 parent 86010be commit b338b54
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
7 changes: 5 additions & 2 deletions ext/node/polyfills/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions tests/integration/node_unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions tests/unit_node/assert_test.ts
Original file line number Diff line number Diff line change
@@ -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,
);
});

0 comments on commit b338b54

Please sign in to comment.