Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promises-aplus-tests

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promises-aplus-tests

Compliance test suite for Promises/A+

  • 1.3.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.3K
decreased by-10.05%
Maintainers
1
Weekly downloads
 
Created
Source
Promises/A+ logo

Promises/A+ Compliance Test Suite

This suite tests compliance of a promise implementation with the Promises/A+ specification.

How To Run

The tests run in a Node.js environment; make sure you have that installed.

Adapters

In order to test your promise library, you must expose a very minimal adapter interface. These are written as Node.js modules with a few well-known exports:

  • fulfilled(value): creates a promise that is already fulfilled with value.
  • rejected(reason): creates a promise that is already rejected with reason.
  • pending(): creates an object consisting of { promise, fulfill, reject }:
    • promise is a promise that is currently in the pending state.
    • fulfill(value) moves the promise from the pending state to a fulfilled state, with fulfillment value value.
    • reject(reason) moves the promise from the pending state to the rejected state, with rejection reason reason.

The fulfilled and rejected exports are actually optional, and will be automatically created by the test runner using pending if they are not present. But, if your promise library has the capability to create already-fulfilled or already-rejected promises, then you should include these exports, so that the test runner can provide you with better code coverage and uncover any bugs in those methods.

Note that the tests will never pass a promise or a thenable as a fulfillment value. This allows promise implementations that only have "resolve" functionality, and don't allow direct fulfillment, to implement the pending().fulfill and fulfilled, since fulfill and resolve are equivalent when not given a thenable.

Finally, note that none of these functions, including pending().fulfill and pending().reject, should throw exceptions. The tests are not structured to deal with that, and if your implementation has the potential to throw exceptions—e.g., perhaps it throws when trying to resolve an already-resolved promise—you should wrap direct calls to your implementation in try/catch when writing the adapter.

From the CLI

This package comes with a command-line interface that can be used either by installing it globally with npm install promises-aplus-tests -g or by including it in your package.json's devDependencies and using npm's scripts feature. In the latter case, your setup might look something like

{
    "devDependencies": {
        "promises-aplus-tests": "*"
    },
    "scripts": {
        "test": "run-my-own-tests && promises-aplus-tests test/my-adapter"
    }
}

The CLI takes as its first argument the filename of your adapter file, relative to the current working directory. It tries to pass through any subsequent options to Mocha, so you can use e.g. --reporter dot or --grep 3.2.6.4.

Programmatically

The main export of this package is a function that allows you to run the tests against an adapter:

var promisesAplusTests = require("promises-aplus-tests");

promisesAplusTests(adapter, function (err) {
    // All done; output is in the console. Or check `err` for number of failures.
});

You can also pass any Mocha options as the second parameter, e.g.

promisesAplusTests(adapter, { reporter: "dot" }, function (err) {
  // As before.
});

Within an Existing Mocha Test Suite

If you already have a Mocha test suite and want to include these tests in it, you can do:

describe("Promises/A+ Tests", function () {
    require("promises-aplus-tests").mocha(adapter);
});

This only works in Node.js, however.

Keywords

FAQs

Package last updated on 22 Jun 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc