Code Monkey home page Code Monkey logo

jasmine-es6-promise-matchers's Introduction

Jasmine Promise Matchers

NOTE: If you use Jasmine 3.2+ there are built-in async matchers available. Read more here

Custom matchers for testing ES6 Promises with Jasmine 2.x.

NOTE: This library expects ES6 Promise feature to be present.

If you're testing in an old environment, e.g. PhantomJS, you need to load a polyfill.

Installing

You can install with either NPM or Bower like so:

npm i -D jasmine-es6-promise-matchers
bower install jasmine-es6-promise-matchers

Overview

Using the Jasmine matchers provided by this extension allows a simpler and cleaner flow of unit tests.

Before:

This is not only harded to read, but also unstable. If the promise is never resolved, the tests hang for a long timeout.

describe('test', function() {
  it('should resolve to something', function(done) {
    function assert(value) {
      expect(value).toBe('something');
      done();
    }

    const promise = something.withPromise();
    promise.then(assert, done.fail);
  });
});

After:

Using the matchers provided by this library your tests could instead look like this:

describe('test', function() {
  it('should resolve to something', function(done) {
    const promise = something.withPromise();
    expect(promise).toBeResolvedWith('something', done);
  });
});

Using the matchers

This library includes a couple of matchers, as shown below.

toBeRejected(done:Function)

Verify that a Promise is (or will be) rejected.

expect(promise).toBeRejected(done);
toBeRejectedWith(data:*, done:Function)

Verify that a Promise is rejected with a specific value.

expect(promise).toBeRejectedWith('something', done);

// Asymmetric matching is also supported for objects:
var partialMatch = jasmine.objectContaining({partial: 'match'});
expect(promise).toBeRejectedWith(partialMatch, done);
toBeResolved(done:Function)

Verify that a Promise is resolved.

expect(promise).toBeResolved(done);
toBeResolvedWith(data:*, done:Function)

Verify that a Promise is resolved with a specific value.

expect(promise).toBeResolvedWith('something', done);

// Asymmetric matching is also supported for objects:
var partialMatch = jasmine.objectContaining({partial: 'match'});
expect(promise).toBeResolvedWith(partialMatch, done);

Questions? Suggestions?

Open an issue or toss up a pull request if you'd like to see anything added to this library!

jasmine-es6-promise-matchers's People

Contributors

a-lucas avatar bvaughn avatar darlanalves avatar maks3w avatar mtaran-google avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  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.