assert-throws-async
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

assert-throws-async

Node's builtin assert.throws does not work very well with rejecting promises or (equivalently) async functions.

This wrapper fixes this.

It also allows you to pass both validation functions and constructors to compare with as I got sick of typing e instanceof MyError in my validation functions.

Examples:

const assertThrows = require('assert-throws-async'); 
 
class MyError extends Error { }
const naughtyFunction = () => {
    return Promise.reject(new MyError('waaat'));
}
 
//...
 
assertThrows(naughtyFunction, MyError, 'waaat');

Usage:

assertThrows(fFunction, errorCheckstring | RegExp | ((Error) => boolean));

Runs f().

If it throws or returns a promise that rejects with e:

  • if errorCheck is a string or RegExp, check e.message against errorCheck
  • If errorCheck is a function, run errorCheck(e).

If either of these checks are unsuccesful, re-throw e.

If f does not throw e, throw an AssertionError('Expected to throw.').

assertThrows(fFunction, ErrorTypeclass<Error>);

Runs f().

If it throws or returns a promise that rejects with e:

  • if e is not an instance of ErrorType, re-throw e.

If f does not throw e, throw an AssertionError('Expected to throw.').

assertThrows(fFunction, ErrorTypeclass<Error>, errorCheckstring | RegExp | ((Error) => boolean) );

Combines both the above checks. The ErrorType check is executed first.

Readme

Keywords

none

Package Sidebar

Install

npm i assert-throws-async

Weekly Downloads

1,667

Version

3.0.0

License

ISC

Unpacked Size

28.6 kB

Total Files

12

Last publish

Collaborators

  • akdor1154