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

tape-promise

v4.0.0

Published

Promise/async support for tape.

Downloads

12,575

Readme

tape-promise

build status

Promise and ES2016 (ES7) async/await support for Tape.

This module assumes that you're familiar with at least the concepts of Promises and if you want to use async/await, then you must be familiar with Babel.

Install

npm i --save-dev tape-promise

Usage

Make sure that you have tape installed:

npm i --save-dev tape

Unlike blue-tape, tape-promise is just a decorator (not an ES7 decorator). That is, it does NOT depend upon tape and requires that you pass it in.

ES5

var tape = require('tape')
var _test = require('tape-promise').default // <---- notice 'default'
var test = _test(tape) // decorate tape

ES6 (ES2015)

import tape from 'tape'
import _test from 'tape-promise'
const test = _test(tape) // decorate tape

or, for convenience...

import test from 'tape-promise/tape'

but you must explicitly have tape as a dependency.

Example (promises)

Just return the promise.

// example function that returns a Promise
function delay (time) {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      resolve()
    }, time)
  })
}

test('ensure promises works', function (t) {
  return delay(100).then(function () {
    t.true(true)
  })
})

Example (async/await)

Async/await functions just transform to Promises. Don't forget to put the async keyword on your function.

// example function that returns a Promise
// it could also be an async function
function delay (time) {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      resolve()
    }, time)
  })
}

// NOTICE 'async'?
test('ensure async works', async function (t) {
  await delay(100)
  t.true(true)
  t.end() // not really necessary
})

Example (normal tape tests)

Of course you can write normal tape tests as you would.

test('ensure that regular test functions still work', function (t) {
  t.true(true)
  t.end()
})

New Assertions

t.rejects(promise, expected, msg)

Assert that the promise will reject.

If promise is a function, then it is called, and the returned promise is used.

expected is the error that should be rejected with, if present, must be a RegExp or Function. The RegExp matches the string representation of the exception, as generated by err.toString(). The Function is the exception thrown (e.g. Error). If the promise rejects, but its error doesn't match expected, then the assertion fails.

msg is an optional description of the assertion.

This function returns a promise that resolves when the assertion is complete (after the promise is settled). The test will not wait for the assertion automatically, so it needs to be awaited.

t.doesNotReject(promise, expected, msg)

Assert that the promise will resolve.

If promise is a function, then it is called, and the returned promise is used.

expected is the error that the promise shouldn't reject with, if present, it must be a RegExp or Function. The RegExp matches the string representation of the exception, as generated by err.toString(). The Function is the exception thrown (e.g. Error). If the promise rejects, but its error doesn't match expected, then the assertion passes.

msg is an optional description of the assertion.

This function returns a promise that resolves when the assertion is complete (after the promise is settled). The test will not wait for the assertion automatically, so it needs to be awaited.

example

test('reject and doesNotReject example', async (t) => {
  await t.rejects(asyncFunction)
  await t.rejects(asyncFunction())
  await t.doesNotReject(Promise.resolve())
})

License

Licensed under MIT

Copyright (c) JP Richardson