New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

assert-throws-async

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

assert-throws-async

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

latest
Source
npmnpm
Version
3.0.0
Version published
Weekly downloads
1K
-22.56%
Maintainers
1
Weekly downloads
 
Created
Source

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(f: Function, errorCheck: string | 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(f: Function, ErrorType: class<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(f: Function, ErrorType: class<Error>, errorCheck: string | RegExp | ((Error) => boolean) );

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

FAQs

Package last updated on 07 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts