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

tapromise

v1.1.2

Published

Turn any tap Test object into a promise-resolving thingie

Downloads

381

Readme

tapromise

Turn any tap Test object into a promise-resolving thingie

Build Status

If you're using tap for tests, and those test interact with a lot of Promises, sometimes it's nice to be able to do asserts against those objects by resolving them first.

This will work just fine with any version of tap or tape.

However, tap version 5.5 or greater is strongly recommended because:

  1. You don't have to .then(function() { t.end() }) if you return a promise from a tap test function, because it groks promises as return values.
  2. The at and stack fields will be set in a useful way in tap 5.5 and higher, so that failures will point at the proper line in your test script, rather than in some obscure place inside this module.

Use it like so:

var tapromise = require('tapromise')
var t = require('tap')

t.test('whoa lotta promises!', function (t) {
  t = tapromise(t)
  return Promise.all([
    t.equal(promiseToBeTen(), 10),
    t.ok(Promise.resolve(true)),
    t.match(Promise.resolve({ a: 1 }), { a: 1 })
  ])
})

This has the following effects:

  1. A tapromise object has all the same assert methods as the Test object passed to it.
  2. Every assert method on the tapromise object resolves all Promises passed to it, and returns a Promise.
  3. When all the promises resolve, it runs the assert on the data.

So, the above code would be equivalent to:

var t = require('tap')

t.test('whoa lotta promises!', function (t) {
  return promiseToBeTen().then(function (ten) {
    t.equal(ten, 10)
    return Promise.resolve(true)
  }).then(function (shouldBeTrue) {
    t.ok(shouldBeTrue)
    return Promise.resolve({ a: 1 })
  }).then(function (obj) {
    t.match(obj, { a: 1 })
  })
})

This reduces a lot of the Promise boilerplate. If you are testing an API that uses Promises extensively to return data (for example, Selenium), then this can be very convenient.

Note that this means you can't ever test that an object is a Promise, since the tapromise object will resolve everything it receives.

API

  • tapromise(test, [options]) Returns a tapromise object with methods corresponding to all methods on the tap.Test argument that accept Promises as args and returns a Promise that resolves when the assert has been made.

    • options.exclude A list of method names to expose, but not wrap in promise-resolving behavior. Note that this means those methods will probably execute synchronously and not return a promise.

      You can use this if there are test methods you want to be able to call right away. It's also especially useful if you want to be able to assert that a thing is a Promise, without automatically resolving it.