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

test-promise

v1.0.0

Published

Synchronous promises that are super helpful for testing.

Downloads

6

Readme

Test-Promise

A simple, synchronous Promise object that is useful for tests. Not completely Promises/A+ compatible, but really close. Also comes with useful inspection methods.

This is ONLY for testing code. It uses the global function spyOn that is supplied by Jasmine.

Including

First, add this to your project. This library works in ES5, node, browsers and practically everywhere JavaScript runs -- including IE8.

Next, just use this promise library instead of your built-in one. That may be a lot harder. To make the swap easier, you should design projects around dependency injection. I use Dizzy for node development. Perhaps you need to make a special build and replace your other Promise library. Maybe, just for testing, you allow the Promise object to be passed in? The structure of your application is entirely up to you.

After that, you start making Promises!

Usage

Let's suppose we are testing how your asynchronous code operates. We want to prepare a Promise object with a resolved value, run your code, then get the resulting Promise. In most cases and with external libraries mocked to return right away, we suddenly have synchronous code!

describe("your object", function () {
    var TestPromise, yourObject;

    beforeEach(function () {
        TestPromise = require("test-promise");

        // For demonstration purposes only
        yourObject = {
            reverseAsync: function (promise) {
                return promise.then(function (val) {
                    return val.toString().split('').reverse().join('');
                });
            }
        };
    });
    it("now appears synchonous", function () {
        var endPromise, startPromise;

        startPromise = new TestPromise();
        startPromise.resolve(1234);
        endPromise = yourObject.reverseAsync(startPromise);
        expect(endPromise.status()).toEqual({
            success: true,
            value: 4321
        });
    });
    it("allows you to reject easily", function () {
        var endPromise, err, startPromise;

        err = new Error("fake error");
        startPromise = new TestPromise();
        startPromise.reject(new Error("fake error"));
        endPromise = yourObject.reverseAsync(startPromise);
        expect(endPromise.status).toBe(false);
        expect(endPromise.value).toBe(err);
    });
    it("lets you trigger success/failure when you like", function () {
        var endPromise, startPromise;

        startPromise = new TestPromise();
        endPromise = yourObject.reverseAsync(startPromise);
        expect(endPromise.status).toBe(null);
        startPromise.resolve("testing");
        expect(endPromise.status).toBe(true);
        expect(endPromise.value).toBe("gnitset");
    });
    it("even supports you grabbing the success/error handlers", function () {
        var startPromise;

        startPromise = new TestPromise();
        yourObject.reverseAsync(startPromise);
        expect(startPromise.then.mostRecentCall.args[0]).toEqual(jasmine.any(Function));
        expect(startPromise.then.mostRecentCall.args.length).toBe(1);
    });
    it("helps out with async functions in Jasmine 1.x", function () {
        var endPromise;

        runs(function () {
            var startPromise;

            startPromise = new TestPromise();
            startPromise.resolve("async works");
            endPromise = yourObject.reverseAsync(startPromise);
        });
        waitsFor(function () {
            return endPromise.status();
        });
        runs(function () {
            expect(endPromise.value).toEqual("skrow cnysa");
        });
    });
    it("assists with debugging", function () {
        var callbackSet, endPromise, startPromise;

        startPromise = new TestPromise();
        endPromise = yourObject.reverseAsync(startPromise);
        expect(startPromise.then).toHaveBeenCalled();
        expect(startPromise.children.length).toBe(1);
        callbackSet = startPromise.children[0];
        expect(callbackSet.promise).toBe(endPromise);
    });
});

API

  • new TestPromise() - Creates a new TestPromise.
  • new TestPromise(otherPromise) - Attaches a new TestPromise to the result of otherPromise.
  • promise.then(success, failure) - Works exactly as expected.