2.0.3 • Published 9 years ago

supertest-session-promise v2.0.3

Weekly downloads
15
License
MIT
Repository
github
Last release
9 years ago

supertest-session-promise

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 session
  .get("/user")
  .expect(200)
  .then(function (res) {
    return request(app)
      .post("/kittens")
      .send({ userId: res})
      .expect(201);
  })
  .then(function (res) {
    // ...
  });

Usage (mocha example)

var sessionFactory = require("supertest-session-promise"),
	app = /* get an express app or something */,
	session;
	
beforeEach(function(){
	session = sessionFactory.create({app:app});
});

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 session.get("/kittens").expect(200);
  });
});

Cookies

If you don't care about sessions, use supertest-as-promised module, which this is based off of.

Cookies are available through

var session = sessionFactory.create({app:app});
session.cookies

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:

session
  .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:

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

Installation

Node

$ npm install supertest-session-promise
  • Bluebird has been upgraded to version 2.9.24.