Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
promisify and expose SuperTest agents
Browse files Browse the repository at this point in the history
Agents persist cookies across requests. Promisify and expose this
interface at `exports.agent` to be compatible with SuperTest.

Fixes #1.
  • Loading branch information
benesch committed May 19, 2014
1 parent 4860f45 commit fe68e02
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ describe("GET /kittens", function () {
});
```

### Agents

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

```js
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`
Expand Down
30 changes: 21 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _ = require("lodash")
var methods = require("methods")
, Promise = require("bluebird")
, supertest = require("supertest");

Expand All @@ -7,17 +7,29 @@ function then(onFulfilled, onRejected) {
.then(onFulfilled, onRejected);
}

module.exports = function () {
// Let SuperTest work its magic on whatever arguments we receive
var request = supertest.apply(null, arguments);
// Creates a new object that inherits from `factory`, where each HTTP method
// (`get`, `post`, etc.) is overriden to inject a `then` method into the
// returned `Test` instance.
function extend(factory) {
var out = Object.create(factory);

// Wrap all SuperTest functions (`get`, `post`, etc.) so we can inject a
// `then` method into the returned `Test` instance
return _.mapValues(request, function wrap(fn) {
return function () {
var test = fn.apply(null, arguments);
methods.forEach(function (method) {
out[method] = function () {
var test = factory[method].apply(factory, arguments);
test.then = then;
return test;
};
});

return out;
}

module.exports = function () {
var request = supertest.apply(null, arguments);
return extend(request);
};

module.exports.agent = function () {
var agent = supertest.agent.apply(null, arguments);
return extend(agent);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
},
"dependencies": {
"bluebird": "^1.2.4",
"lodash": "^2.4.1"
"methods": "^1.0.0"
}
}
36 changes: 34 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,46 @@ describe("supertestAsPromised", function () {
return request.get("/home").expect(500).should.eventually.be.rejected;
});
});

describe("TestAgent instances", function () {
var agent = supertestAsPromised.agent(server);

describe("#then", function () {
it("should return a promise", function () {
agent.get("/home").then().should.be.an.instanceOf(Promise);
});
});

it("should fulfill if all assertions pass", function () {
return agent.get("/home").expect(200).should.eventually.be.fulfilled;
});

it("should fulfill with the response", function () {
return agent.get("/home").then(function (res) {
res.text.should.equal("helo");
});
});

it("should reject if an assertion fails", function () {
return agent.get("/home").expect(500).should.eventually.be.rejected;
});
});
});

describe("supertest", function () {
var request = supertest(server);

describe("Test instances", function () {
var request = supertest(server);

it("should not be a promise", function () {
request.get("/home").should.not.have.property("then");
});
});

describe("TestAgent instances", function () {
var agent = supertest.agent(server);

it("should not be a promise", function () {
agent.get("/home").should.not.have.property("then");
});
});
});

0 comments on commit fe68e02

Please sign in to comment.