GithubHelp home page GithubHelp logo

ufo22940268 / supertest-as-promised Goto Github PK

View Code? Open in Web Editor NEW

This project forked from whoopinc/supertest-as-promised

0.0 1.0 0.0 103 KB

Supercharge supertest with a promise interface

License: MIT License

JavaScript 100.00%

supertest-as-promised's Introduction

Promises/A+ logo

supertest-as-promised

Build Status

SuperTest as Promised supercharges SuperTest with a then method.

Instead of layering callbacks on callbacks in your tests:

request(app)
  .get("/user")
  .expect(200, function (err, res) {
    if (err) return done(err);

    var userId = res.body.id;
    request(app)
      .post("/kittens")
      .send({ userId: userId, ... })
      .expect(201, function (err, res) {
        if (err) return done(err);

        // ...
      });
  });

chain your requests like you were promised:

return request(app)
  .get("/user")
  .expect(200)
  .then(function (res) {
    return request(app)
      .post("/kittens")
      .send({ userId: res})
      .expect(201);
  })
  .then(function (res) {
    // ...
  });

Usage

SuperTest as Promised operates just like normal SuperTest, except that the object returned by .get, .post, etc. is a proper thenable:

var express = require("express")
  , request = require("supertest-as-promised");

var app = express();

request(app)
  .get("/kittens")
  .expect(200)
  .then(function (res) {
    // ...
  });

If you use a promise-friendly test runner, you can just return your request chain from the test case rather than messing with a callback:

describe("GET /kittens", function () {
  it("should work", function () {
    return request(app).get("/kittens").expect(200);
  });
});

Agents

If you use a SuperTest agent to persist cookies, those are thenable too:

var agent = require("supertest-as-promised").agent(app);

agent
  .get("/ugly-kitteh")
  .expect(404)
  .then(function () {
    // ...
  })

Promisey goodness

To start, only the then method is exposed. But once you've called .then once, you've got a proper Bluebird promise that supports the whole gamut of promisey goodness:

request(app)
  .get("/kittens")
  .expect(201)
  .then(function (res) { /* ... */ })
  // I'm a real promise now!
  .catch(function (err) { /* ... */ })

See the Bluebird API for everything that's available.

You may find it cleaner to cast directly to a promise using the toPromise method:

request(app)
  .get("/kittens")
  .expect(201)
  .toPromise()
  // I'm a real promise now!
  .delay(function (res) { /* ... */ })
  .then(function (res) { /* ... */ })

BYOP: Bring your own Promise

You can supply own promise library so that the promises returned have your convenience methods of choice.

Simply call the SuperTest as Promised module with a ES6-compliant Promise constructor, and you'll get back a new module configured to return your custom promises. To swap in when.js, for example:

var when = require("when")
  , request;

request = require("supertest-as-promised")(when.Promise);
request(app)
  .get("/when.js")
  .then(function (res) { /* ... */ })
  // I'm a when.js promise! (instanceof when.Promise == true)
  .frobulate()

request = require("supertest-as-promised");
request(app)
  .get("/bluebird.js")
  .then(function (res) { /* .. */ })
  // I'm back to the default Bluebird promise!

Installation

Node

$ npm install supertest-as-promised

SuperTest as Promised lists supertest as a peer dependency, so it'll wrap whatever version of SuperTest you've asked for in your own package.json. If you don't specify a version of SuperTest, npm will use the latest.

Do note that SuperTest as Promised is a well-behaved citizen and doesn't monkey-patch SuperTest directly:

// I return thenables!
var request = require("supertest-as-promised");

// I'm lame and force you to use callbacks
var request = require("supertest");

Versions

We follow semver: the major version number will be upgraded with any breaking change. Breaking changes in each major version are listed below. Consult the changelog for a list of meaningful new features in each version; consult the commit log for a complete list.

Breaking changes in 2.0

  • Bluebird has been upgraded to version 2.9.24.

supertest-as-promised's People

Contributors

benesch avatar ufo22940268 avatar alavers avatar

Watchers

James Cloos 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.