Skip to content

59naga/assert-exception

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Assert-exception

Npm version Build Status

Installation

$ yarn add -D assert-exception
# or
$ npm install assert-exception --save-dev

Usage

without assert-exception

import assert from 'power-assert';

assert.throws(
  () => {
    throw new Error('foo');
  },
  (error) => {
    assert(error.message === 'foo');
    return true; // requirement :(
  }
); // pass

with assert-exception

import assert from 'power-assert';
import { throws } from 'assert-exception';

assert(throws(
  () => {throw new Error('foo');},
).message === 'foo'); // pass

if doesn't become the exception, it returns an empty object.

import assert from 'power-assert';
import { throws } from 'assert-exception';

assert(throws(
  () => {},
).message === 'foo'); // fail
//
// AssertionError:   # foo.js:4
//
//   assert(throws(() => {}).message === 'foo')
//          |                |       |
//          |                |       false
//          Object{}         undefined
//

API

throws(exception) -> Error or {}

run the exception, and returns the error. if not returned error, it returns an empty object.

import assert from 'power-assert';
import { throws } from 'assert-exception';

assert(throws(
  () => {},
).message === undefined); // pass

assert(throws(
  () => {throw new Error('foo');}
).message === undefined); // fail

rejects(promise) -> Promise(reason)

return the rejected reason unless fulfill.

import 'babel-polyfill';
import assert from 'power-assert';
import { rejects } from 'assert-exception';

it('rejects', async () => {
  assert((await rejects(Promise.reject(new Error('foo')))).message === 'foo'); // pass
  assert((await rejects(Promise.resolve(new Error('foo')))).message === 'foo'); // fail
});

Development

Requirement global

  • NodeJS v10.6.0
  • Yarn v1.8.0
git clone https://github.com/59naga/assert-exception
cd assert-exception
yarn

yarn test

License

MIT

About

an assert exception functions (throws / rejects)

Resources

Stars

Watchers

Forks

Packages

No packages published