npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

assertive-as-promised

v1.0.4

Published

Extends assertive with promise support

Downloads

11

Readme

Assertive for Promises

Assertive as Promised extends Assertive for asserting things about standards-compliant promises. It is 100% backward-compatible, so all of the existing assertive documentation applies.

DEPRECATED

As of assertive-2.1.0, this functionality has been rolled into assertive itself. The only functional change is that you can no longer pass functions-which-return-promises as arguments to resolves() and rejects() - you should replace things like:

assert = require 'assertive-as-promised'
assert.rejects -> doSomething('blah')

with

assert = require 'assertive'

# this version will explode your test if there is a synchronous throw
# during the invocation of doSomething() (which may be a good thing -
# you can test for that behavior separately with assert.throws())
assert.rejects(doSomething('blah')).then (err) -> ...

# this version will turn any synchronous throw into a rejection which will
# be available as err
Promise = require 'bluebird'
assert.rejects(Promise.try -> doSomething('blah')).then (err) ->

How to Use

This is best used with something like Mocha (version >= 1.18.0) which handles returned promises correctly. All of assertive's assertions are extended to accept promises as their argument to be tested and return a promise which will be resolved or rejected.

For all existing assertive functions, you may simply replace the argument with a promise for an equivalent argument. assert.equal('foo', funcThatReturnsAString()) becomes assert.equal('foo', funcThatReturnsAPromiseForAString()). Note that you may get nicer and more consistent errors if you put any function calls that may have a risk of throwing an exception synchronously inside a bluebird Promise.try(), thusly: assert.equal('foo', Promise.try -> funcThatReturnsAPromiseForAString())

Note for throws() and notThrows() that they accept a function (which may throw a synchronous exception) or a promise for a function (which may throw a synchronous exception). The resolution status of the promise itself is not being tested. Since you're often more interested in the resolution status of the promise, there are two new functions: rejects and resolves:

assert.rejects(-> funcThatReturnsAPromise(someArg)) takes as its argument a promise OR a function that returns a promise. The equivalent counterpart to notThrows is called resolves. rejects returns a promise for the rejection error, and thus composes nicely with other assertions.

Examples (using Mocha)

{ runSync, runAsync }  = require './some-library'
assert  = require 'assertive-as-promised'
Promise = require 'bluebird'
# runAsync returns a promise

it 'runs synchronously', ->
  assert.deepEqual 'got proper hash', { a: 42 }, runSync('good')

it 'fails synchronously', ->
  assert.throws 'fails on bad', -> runSync('bad')

it 'runs asynchronously', ->
  assert.deepEqual 'got proper hash', { a: 42 }, runAsync('good')

it 'fails asynchronously', ->
  assert.rejects 'fails on bad', -> runAsync('bad')

fn = -> Promise.try -> throw 'kaboom'
it 'fails asynchronously with the proper error', ->
  assert.equal 'kaboom', assert.rejects fn

Note that if you want to be able to put more than one asynchronous test in a single it(), you'll need to combine them somehow to make mocha-as-promised happy, e.g.:

{ runAsync } = require './some-library'
assert       = require 'assertive-as-promised'
Promise      = require 'bluebird'

it 'runs and fails asynchronously', ->
  Promise.all [
    assert.deepEqual 'got proper hash', { a: 42 }, runAsync('good')
    assert.rejects 'fails on bad', -> runAsync('bad')
  ]

(this may be bad style, depending on who you ask, but I find it useful if you have it()s with a lot of setup overhead)

Development

  • src/aap.coffee is the main library; it compiles to lib/aap.js.
  • test/assertive_test.coffee is a copy of the assertive library tests, slightly modified to run correctly in our test environment (see comments at the top)