🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

q-supertest

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

q-supertest

Supercharge supertest with the Q promise interface

latest
Source
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source
Promises/A+ logo

q-supertest

Build Status

q-supertest supercharges SuperTest with a then method, that returns Q promise.

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

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

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

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("q-supertest").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 Q 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 Q API for everything that's available.

Installation

Node

$ npm install q-supertest

q-supertest 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 q-supertest is a well-behaved citizen and doesn't monkey-patch SuperTest directly:

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

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

Keywords

supertest

FAQs

Package last updated on 12 May 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts