Skip to content

assert.throws() does not accept an arrow function as the second argument #3275

Closed
@asukakenji

Description

Code:

var assert = require("assert");

console.log("Checkpoint 1");
assert.throws(function() { throw new TypeError(); }, function(err) { return err instanceof TypeError; });

console.log("Checkpoint 2");
assert.throws(() => { throw new TypeError(); }, function(err) { return err instanceof TypeError; });

console.log("Checkpoint 3");
assert.throws(() => { throw new TypeError(); }, (err) => { return err instanceof TypeError; });

console.log("Checkpoint 4");

Result:

Checkpoint 1
Checkpoint 2
Checkpoint 3
assert.js:271
  } else if (actual instanceof expected) {
                               ^

TypeError: Function has non-object prototype 'undefined' in instanceof check
    at expectedException (assert.js:271:32)
    at Function._throws (assert.js:310:8)
    at Function.assert.throws (assert.js:319:11)
    at Object.<anonymous> (/path/to/bug.js:10:8)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:475:10)
    at startup (node.js:118:18)

The results are the same no matter which of the following syntax is used:

(err) => { return err instanceof TypeError; }
err => { return err instanceof TypeError; }
err => err instanceof TypeError

[BridgeAR: fixed examples by adding missing return statements]

Metadata

Assignees

No one assigned

    Labels

    assertIssues and PRs related to the assert subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions