Coder Social home page Coder Social logo

fakepromise's Introduction

FakePromise

A fake Promise implementation, for testing purposes.

The main difficulty with testing native promises is that their fulfillment and rejection handlers are called asynchronously, which means your test finishes before all your code has been run. Fake promises solve this by queueing up handlers, so you can decide exactly when they are executed. They also expose their current state, making them easier to assert on.

Usage

You can create a FakePromise directly, or replace native promises by calling FakePromise.replacePromise(). Native promises can be restored with FakePromise.restorePromise().

API

FakePromise supports the same API as native promises, with a few additions. If you've previously called FakePromise.replacePromise(), you can access the static functions through Promise as well as FakePromise.

FakePromise.flush()

Runs any pending fulfillment or rejection handlers. If they add more handlers to the queue, those will be run as well.

FakePromise.clear()

Clears the queue of pending handlers, so that they won't be run during the next flush().

FakePromise.defer()

Creates a new FakePromise that must be resolved or rejected manually.

FakePromise.replacePromise()

Replaces the Promise constructor with FakePromise, so any code creating Promises will create fakes instead.

FakePromise.restorePromise()

Sets Promise back to the native implementation.

#resolve(value)

Resolves the promise with the given (optional) value.

#reject(error)

Rejects the promise with the given (optional) error.

#value

Returns the value that the promise was resolved with, if it was resolved.

#error

Returns the error that the promise was rejected with, if it was rejected.

#isPending

True if the promise has been neither resolved nor rejected.

#isResolved

True if the promise has been resolved.

#isRejected

True if the promise has been rejected.

fakepromise's People

Contributors

soxtoby avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.