assert-throws-async
Node's builtin `assert.throws` does not work very well with rejecting promises or (equivalently) async functions.
Last updated 2 years ago by akdor1154 .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install assert-throws-async 
SYNC missed versions from official npm registry.

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.

Current Tags

  • 3.0.0                                ...           latest (2 years ago)

4 Versions

  • 3.0.0                                ...           2 years ago
  • 2.0.5                                ...           2 years ago
  • 2.0.1                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (0)
None
Dev Dependencies (6)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |