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

@jasonewall/promitto

v0.2.1

Published

Simple Promise mocking for unit testing.

Downloads

3

Readme

Promitto

Simple Promise mocking for unit testing.

const p = promitto.pending("Some API results");

render(<MyComponent data={p} />);

// assert loading state

await p.resolve().settled();

// assert final state

Getting Started

npm i --save-dev @jasonewall/promitto

import promitto from '@jasonewall/promitto'

API

promitto<T>()

Returns a promise mock that can be resolved or rejected by calling either:

  1. resolve(value?: T) - The promise will resolve to the value passed in, and value will be passed to any fulfillment handlers attached to the promise mock.
  2. reject(reason?: any) - The promise will reject with the reason passed in and reason will be passed to any rejection handlers attached to the promise mock.

No then, catch, finally callbacks will be executed until either of these are called.

const p = promitto<string>();

p.resolve("Testing is fun!");

p.reject(new Error("Error handling is fun!"));

promitto.pending<T>(value: T)

Returns a promise mock that will not run any callbacks (added through then, catch, finally) until resolve() is called on the promise mock. The wrapped value of the promise mock is locked into the value initially passed into pending.

const p = promitto.pending("Hello!");

p.resolve();

promitto.resolve(value?: T)

Returns a promise mock that is already resolved to the provided value. Any callbacks added by then, catch, or finally will be immediately executed.

const p = promitto.resolve(["Cats", "Dogs"]);

promitto.reject(reason?: any)

Return a promise mock that is already rejected to the provided value. Any callbacks added by then, catch, or finally will be imediately executed.

const p = promitto.reject(new Error("Missing pets!"));

See Use Cases for more info.

PromiseMock Instance Methods

#children: PromiseMock<any>

Returns all promises created by calling then, catch, finally of this PromiseMock.

const p = promitto.pending("Some value");

callMyApplication(p);

console.log(p.children);

[More Info]

#settled(): Promise<T>

Returns a promise that only settles once this promise and all of it's children are settled.

const p = promitto.resolve("Good job!");

callMyApplication(p);

await p.settled();

// assert application state

Settling Rejected PromiseMocks

[More Info]

#status: PromiseState

Returns the current status of this PromiseMock. Will be one of:

enum PromiseState {
  Pending = "pending",
  Fulfilled = "fulfilled",
  Rejected = "rejected",
}

Compatability with Core JS/TS Promise

This is a huge concern of Promitto. We DO NOT want our tests to be buggy because our mocks are creating promise chains that behave differently than real promises. To ensure Promitto PromiseMocks behave exactly like real promises we have a large test suite that compares the behaviour of PromiseMock to Promise.

In short - we run our test suite against Promise core to test the tests. See the Promise tests for more details.

If you feel there are some cases that are missing Pull Requests and/or Issues describing the missing test cases are welcome.

For Testing Only

As a mostly synchronous promise implemenation this library has no value for application code. Please only use it for testing.