Skip to content

assert.rejects passes if the given function throws synchronously #19646

Closed
@not-an-aardvark

Description

@not-an-aardvark
  • Version: Built from source off of latest master, df62e69. (This API has not appeared in a release yet.)
  • Platform: macOS
  • Subsystem: assert

The following code prints assertion passed:

const assert = require('assert');

function functionThatThrows() {
  throw new Error();
}

assert.rejects(functionThatThrows).then(
  () => console.log('assertion passed'),
  () => console.log('assertion failed')
);

This is unexpected because I would expect assert.rejects to check that the given function returns a Promise which eventually rejects. In this case, no Promise is returned at all, so there is nothing that "rejects".

From an ergonomics perspective, if I were writing a function which could return a rejected Promise, I would want to always return a Promise to indicate errors, rather than sometimes throwing synchronously. If my function did throw synchronously rather than rejecting, this would be incorrect behavior and I would expect assert.rejects to report the error.

Metadata

Metadata

Labels

assertIssues and PRs related to the assert subsystem.confirmed-bugIssues with confirmed bugs.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions