├── .gitignore ├── .jshintrc ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── index.js ├── package.json └── test ├── index.js ├── mocha.opts ├── setup.js └── supertest.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | // Enforcing options 3 | "curly": true, 4 | "camelcase": true, 5 | "eqeqeq": true, 6 | "freeze": true, 7 | "latedef": true, 8 | "indent": 2, 9 | "newcap": true, 10 | "noarg": true, 11 | "noempty": true, 12 | "nonbsp": true, 13 | "nonew": true, 14 | "quotmark": "double", 15 | "trailing": true, 16 | "undef": true, 17 | "unused": "vars", 18 | 19 | // Relaxing options 20 | "laxcomma": true, 21 | 22 | // Environments 23 | "node": true, 24 | "white": true, 25 | 26 | "globals": { 27 | "testRequire": false, 28 | "describe": false, 29 | "it": false, 30 | "before": false, 31 | "beforeEach": false, 32 | "after": false, 33 | "afterEach": false 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "6" 4 | - "5" 5 | - "4" 6 | - "0.12" 7 | - "0.10" 8 | sudo: false 9 | deploy: 10 | provider: npm 11 | email: nikhil.benesch@gmail.com 12 | api_key: 13 | secure: OBKShdM6H036C452HerUpJ1PidHQyMWe5z4tMZsgIN0gpsFwujfYG2KOjx6c3UrWkfE4OM0pNxupEM+jaUA/YT/ms8kFvKXaoGMMZan5yCrUeZd6v5mDLSZMbkRBmD2hMXODPVVokE0ox/ManaFWuGDQOWVgjdbFrzYRuKUuvRI= 14 | on: 15 | tags: true 16 | repo: WhoopInc/supertest-as-promised 17 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | We follow [semver]: the major version number will be upgraded with any 4 | breaking change. This changelog lists all meaningful changes in each 5 | release; consult the main [README] to determine which changes are 6 | breaking. 7 | 8 | ## --- / 2016-12-29 9 | 10 | * SuperTest as Promised is deprecated. Thanks, [@jasisk]! 11 | 12 | See the README for instructions on upgrading to SuperTest 2.0. 13 | 14 | ## 4.0.2 / 2016-11-02 15 | 16 | * [[#39]] Bring list of breaking changes in the [README] up-to-date. Thanks, [@karlbecker]! 17 | 18 | ## 4.0.1 / 2016-10-24 19 | 20 | * expose additional `agent` properties through inheritance chain ([#38]) 21 | 22 | Thanks, [@jsdevwork]! 23 | 24 | ## 4.0.0 / 2016-08-11 25 | 26 | * mark `err.response` as non-enumerable to avoid cluttering console output ([#34]) 27 | 28 | Thanks, [@sylvaingi]! 29 | 30 | ## 3.2.0 / 2016-07-02 31 | 32 | * attach `response` to error object to ease debugging of failed requests ([#30]) 33 | 34 | Thanks, [@mkasberg]! 35 | 36 | ## 3.1.0 / 2016-03-30 37 | 38 | * add `catch()` convenience method directly to test instance ([#23]) 39 | 40 | Thanks, [@bbatha]! 41 | 42 | ## 3.0.0 / 2016-02-21 43 | 44 | * update Bluebird to v3.x series 45 | 46 | ## 2.0.2 / 2015-06-05 47 | 48 | * fix double-resolution of promise upon error ([#11]) 49 | 50 | Thanks, [@srussellextensis]! 51 | 52 | ## 2.0.1 / 2015-05-12 53 | 54 | * bump dev dependencies for compatibility with SuperTest's v1.0.0 test 55 | suite. 56 | 57 | ## 2.0.0 / 2015-04-06 58 | 59 | * add `toPromise()` method to explicitly convert request to promise 60 | * support custom promise libraries 61 | * update Bluebird to v2.x series 62 | 63 | ## 1.0.0 / 2014-07-01 64 | 65 | * release: v1.0.0 66 | * support `del` by passing through to upstream's alias 67 | Closes [#3](https://github.com/WhoopInc/supertest-as-promised/issues/3). Closes [#4](https://github.com/WhoopInc/supertest-as-promised/issues/4). 68 | * Alias del and delete to supertest.del 69 | * test: run upstream tests against our wrapped module 70 | * test: switch to expect interface 71 | * README: add build badge 72 | * .travis.yml: add continuous integration configuration 73 | * refactor `then` method 74 | `Promise.promisify` is awkward when invoked immediately. Save to 75 | temporary variable for clarity. 76 | * CHANGELOG: update for v0.1.1 77 | 78 | ## 0.1.1 / 2014-05-19 79 | 80 | * release: v0.1.1 81 | * refactor factory wrapping 82 | We don't need to inherit from the factory as long as we set 83 | the receiver correctly when calling its functions. D'oh! 84 | * CHANGELOG: update for v0.1.0 85 | v0.0.1 was cut from a rebased tree that was never pushed to GitHub, so 86 | changelog generation is a bit out-of-sync. 87 | * release: v0.1.0 88 | 89 | ## 0.1.0 / 2014-05-19 90 | 91 | * README: fix package name in code example 92 | * promisify and expose SuperTest agents 93 | Agents persist cookies across requests. Promisify and expose this 94 | interface at `exports.agent` to be compatible with SuperTest. 95 | Fixes [#1](https://github.com/WhoopInc/supertest-as-promised/issues/1). 96 | * add changelog 97 | * initial commit 98 | 99 | [#39]: https://github.com/WhoopInc/supertest-as-promised/issues/39 100 | [#38]: https://github.com/WhoopInc/supertest-as-promised/issues/38 101 | [#34]: https://github.com/WhoopInc/supertest-as-promised/pull/34 102 | [#30]: https://github.com/WhoopInc/supertest-as-promised/issues/30 103 | [#23]: https://github.com/WhoopInc/supertest-as-promised/pull/23 104 | [#11]: https://github.com/WhoopInc/supertest-as-promised/pull/11 105 | 106 | [@bbatha]: https://github.com/bbatha 107 | [@karlbecker]: https://github.com/karlbecker 108 | [@mkasberg]: https://github.com/mkasberg 109 | [@jasisk]: https://github.com/jasisk 110 | [@jsdevwork]: https://github.com/jsdevwork 111 | [@srussellextensis]: https://github.com/srussellextensis 112 | [@sylvaingi]: https://github.com/sylvaingi 113 | 114 | [README]: README.md 115 | [semver]: http://semver.org 116 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 WHOOP, Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Notice: This repository is no longer maintained as of 3/26/2024 2 | 3 | 4 | Promises/A+ logo 6 | 7 | 8 | # supertest-as-promised 9 | 10 | 11 | Build Status 13 | 14 | 15 | **This project is deprecated!** As of v2.0.0, [SuperTest] has native 16 | support for promises. **Don't use SuperTest as Promised in new projects**; 17 | use SuperTest directly! 18 | 19 | If you're currently using SuperTest as Promised, here's how to switch to 20 | native SuperTest: 21 | 22 | * SuperTest returns only ES6 promises. SuperTest as Promised, by 23 | contrast, returns [Bluebird] promises by default, or promise 24 | objects from a library of your choosing using [BYOP 25 | support](#byop-bring-your-own-promise). Every promise library worth 26 | its salt supports ES6 promise interop, though, so it should be a 27 | small matter to cast ES6 promises to your custom promise library's 28 | promises. Here's a simple SuperTest as Promised snippet that relies 29 | on the `.delay` method of Bluebird promises 30 | 31 | ```js 32 | var request = require("supertest-as-promised"); 33 | 34 | request(app) 35 | .get("/kittens") 36 | .expect(201) 37 | .toPromise() 38 | .delay(10) 39 | .then(function (res) { /* ... */ }) 40 | ``` 41 | 42 | that can be trivially converted to use SuperTest 2.0 like so: 43 | 44 | ```js 45 | var request = require("supertest"); 46 | var BluebirdPromise = require("bluebird"); 47 | 48 | BluebirdPromise.resolve( 49 | request(app) 50 | .get("/kittens") 51 | .expect(201)) 52 | .delay(10) 53 | .then(function (res) { /* ... */ }) 54 | ``` 55 | 56 | * SuperTest does not ship a `.toPromise` method. You'll need to 57 | remove calls to this method. You were probably using `.toPromise` 58 | because you wanted a proper Promise object. 59 | `YourPromiseLibrary.resolve(...)` will probably do the trick; see 60 | the example above. 61 | 62 | SuperTest as Promised will continue to receive bugfixes, but no new 63 | features. Cheers, folks! It's been a good two years. 64 | 65 | ## Overview 66 | 67 | SuperTest as Promised supercharges [SuperTest] with a `then` method. 68 | 69 | Instead of layering callbacks on callbacks in your tests: 70 | 71 | ```js 72 | request(app) 73 | .get("/user") 74 | .expect(200, function (err, res) { 75 | if (err) return done(err); 76 | 77 | var userId = res.body.id; 78 | request(app) 79 | .post("/kittens") 80 | .send({ userId: userId, ... }) 81 | .expect(201, function (err, res) { 82 | if (err) return done(err); 83 | 84 | // ... 85 | }); 86 | }); 87 | ``` 88 | 89 | chain your requests like you were promised: 90 | 91 | ```js 92 | return request(app) 93 | .get("/user") 94 | .expect(200) 95 | .then(function (res) { 96 | return request(app) 97 | .post("/kittens") 98 | .send({ userId: res}) 99 | .expect(201); 100 | }) 101 | .then(function (res) { 102 | // ... 103 | }); 104 | ``` 105 | 106 | ## Usage 107 | 108 | SuperTest as Promised operates just like normal [SuperTest], except that the 109 | object returned by `.get`, `.post`, etc. is a proper 110 | thenable: 111 | 112 | ```js 113 | var express = require("express") 114 | , request = require("supertest-as-promised"); 115 | 116 | var app = express(); 117 | 118 | request(app) 119 | .get("/kittens") 120 | .expect(200) 121 | .then(function (res) { 122 | // ... 123 | }); 124 | ``` 125 | 126 | If you use a promise-friendly test runner, you can just 127 | return your `request` chain from the test case rather than messing with a 128 | callback: 129 | 130 | ```js 131 | describe("GET /kittens", function () { 132 | it("should work", function () { 133 | return request(app).get("/kittens").expect(200); 134 | }); 135 | }); 136 | ``` 137 | 138 | ### Upstream documentation 139 | 140 | SuperTest as Promised is built on top of [SuperTest], which is in turn 141 | built on top of [SuperAgent]. You'll want to be familiar with both of 142 | those packages' APIs. 143 | 144 | Here's the quick rundown if you're unfamiliar. SuperAgent is a generic 145 | HTTP client that provides methods for fluently constructing HTTP requests 146 | (e.g., `.get`, `.send`, `.query`, `.auth`). See the 147 | [SuperAgent docs][SuperAgent] for a full list of methods. SuperTest allows 148 | you to tack on assertions about e.g. response status (`.expect(200)`) or 149 | content (`.expect(/^kitties are \w+$/)`) to a SuperAgent request. See the 150 | [`.expect` function API][expect-api] in the SuperTest documentation for details. 151 | 152 | ### Agents 153 | 154 | If you use a SuperTest agent to persist cookies, those are thenable too: 155 | 156 | ```js 157 | var agent = require("supertest-as-promised").agent(app); 158 | 159 | agent 160 | .get("/ugly-kitteh") 161 | .expect(404) 162 | .then(function () { 163 | // ... 164 | }) 165 | ``` 166 | 167 | 168 | ### Promisey goodness 169 | 170 | To start, only the `then` and `catch` methods are exposed. But once you've 171 | called `.then` or `.catch` once, you've got a proper [Bluebird] promise that 172 | supports the whole gamut of promisey goodness: 173 | 174 | ```js 175 | request(app) 176 | .get("/kittens") 177 | .expect(201) 178 | .then(function (res) { /* ... */ }) 179 | // I'm a real promise now! 180 | .catch(function (err) { /* ... */ }) 181 | ``` 182 | 183 | See the [Bluebird API][bluebird-api] for everything that's available. 184 | 185 | You may find it cleaner to cast directly to a promise using the `toPromise` 186 | method: 187 | 188 | ```js 189 | request(app) 190 | .get("/kittens") 191 | .expect(201) 192 | .toPromise() 193 | // I'm a real promise now! 194 | .delay(10) 195 | .then(function (res) { /* ... */ }) 196 | ``` 197 | 198 | ### BYOP: Bring your own `Promise` 199 | 200 | You can supply own promise library so that the promises returned have your 201 | convenience methods of choice. 202 | 203 | Simply call the SuperTest as Promised module with a ES6-compliant `Promise` 204 | constructor, and you'll get back a new module configured to return your custom 205 | promises. To swap in [when.js], for example: 206 | 207 | ```js 208 | var when = require("when") 209 | , request; 210 | 211 | request = require("supertest-as-promised")(when.Promise); 212 | request(app) 213 | .get("/when.js") 214 | .then(function (res) { /* ... */ }) 215 | // I'm a when.js promise! (instanceof when.Promise == true) 216 | .frobulate() 217 | 218 | request = require("supertest-as-promised"); 219 | request(app) 220 | .get("/bluebird.js") 221 | .then(function (res) { /* .. */ }) 222 | // I'm back to the default Bluebird promise! 223 | ``` 224 | 225 | ### Debugging 226 | 227 | Suppose your snazzy test 228 | 229 | ```js 230 | it("should get kittens", function () { 231 | return request(app).get("/kittens").expect(200) 232 | .then(function (res) { 233 | return expect(res.body).to.equal("kittens r cute!"); 234 | }); 235 | }); 236 | ``` 237 | 238 | suddenly starts failing with a "400 Bad Request" error. It'd sure be 239 | handy if you could print out the response body to see if the server 240 | mentioned what, in particular, was bad about your test. Chain on a 241 | `.catch` and inspect `err.response`: 242 | 243 | ```js 244 | it("should get kittens", function () { 245 | return request(app).get("/kittens").expect(200) 246 | .then(function (res) { 247 | return expect(res.text).to.equal("kittens r cute!"); 248 | }) 249 | .catch(function (err) { 250 | console.log(err.response.text); // "You have viewed too many kittens today." 251 | }); 252 | }); 253 | ``` 254 | 255 | 256 | ## Installation 257 | 258 | ### Node 259 | 260 | ```bash 261 | $ npm install supertest supertest-as-promised 262 | ``` 263 | 264 | SuperTest as Promised lists [`supertest`][SuperTest] as a 265 | [peer dependency][peer-dependency], so it'll wrap whatever version of SuperTest 266 | you've asked for in your own `package.json`. 267 | 268 | In earlier versions of NPM, failing to list SuperTest as a dependency would get 269 | you the latest version. In NPM 3, failing to list SuperTest as a dependency will 270 | generate a warning and SuperTest will not be installed. 271 | 272 | Do note that SuperTest as Promised is a well-behaved citizen and doesn't 273 | monkey-patch SuperTest directly: 274 | 275 | ```js 276 | // I return thenables! 277 | var request = require("supertest-as-promised"); 278 | 279 | // I'm lame and force you to use callbacks 280 | var request = require("supertest"); 281 | ``` 282 | 283 | 284 | ## Versions 285 | 286 | We follow [semver]: the major version number will be upgraded with any breaking 287 | change. Breaking changes in each major version are listed below. Consult the 288 | [changelog] for a list of meaningful new features in each version; consult the 289 | commit log for a complete list. 290 | 291 | ### Breaking changes in 4.0 292 | 293 | * In `.catch` handlers, `err.response` is now marked as non-enumerable. 294 | 295 | ### Breaking changes in 3.0 296 | 297 | * [Bluebird][bluebird] has been upgraded to version 3.3.1. 298 | 299 | ### Breaking changes in 2.0 300 | 301 | * [Bluebird][bluebird] has been upgraded to version 2.9.24. 302 | 303 | [bluebird]: https://github.com/petkaantonov/bluebird 304 | [bluebird-api]: https://github.com/petkaantonov/bluebird/blob/master/API.md#promiseisdynamic-value---boolean 305 | [changelog]: CHANGELOG.md 306 | [expect-api]: https://github.com/visionmedia/supertest#api 307 | [peer-dependency]: http://blog.nodejs.org/2013/02/07/peer-dependencies/ 308 | [semver]: http://semver.org 309 | [SuperAgent]: http://visionmedia.github.io/superagent/ 310 | [SuperTest]: https://github.com/visionmedia/supertest 311 | [when.js]: https://github.com/cujojs/when 312 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var methods = require("methods") 2 | , PromiseBluebird = require("bluebird") 3 | , supertest = require("supertest") 4 | , util = require("util"); 5 | 6 | // Support SuperTest's historical `del` alias for `delete` 7 | methods = methods.concat("del"); 8 | 9 | // Generate a SuperTest as Promised module that returns promise 10 | // instances using the provided `Promise` constructor. 11 | function makeModule(Promise) { 12 | var out; 13 | 14 | function toPromise() { 15 | var self = this; 16 | return new Promise(function (resolve, reject) { 17 | self.end(function (err, res) { 18 | if (err) { 19 | // Attach response to error object so that details about the failure 20 | // (e.g., the response body) can be accessed by a `.catch` handler 21 | // (#30). Use `Object.defineProperty` so that `.response` is 22 | // non-enumerable; otherwise, the error message is lost in the sea of 23 | // the response object (#34). 24 | Object.defineProperty(err, 'response', { value: res }); 25 | reject(err); 26 | return; 27 | } 28 | resolve(res); 29 | }); 30 | }); 31 | } 32 | 33 | function then(onFulfilled, onRejected) { 34 | return this.toPromise().then(onFulfilled, onRejected); 35 | } 36 | 37 | function _catch(onRejected) { 38 | var promise = this.toPromise(); 39 | return promise.catch.apply(promise, arguments); 40 | } 41 | 42 | // Creates a new object that wraps `factory`, where each HTTP method 43 | // (`get`, `post`, etc.) is overriden to inject a `then` method into 44 | // the returned `Test` instance. 45 | function wrap(factory, out) { 46 | methods.forEach(function (method) { 47 | out[method] = function () { 48 | var test = factory[method].apply(this, arguments); 49 | test.toPromise = toPromise; 50 | test.then = then; 51 | test.catch = _catch; 52 | return test; 53 | }; 54 | }); 55 | 56 | return out; 57 | } 58 | 59 | out = function () { 60 | var request = supertest.apply(null, arguments); 61 | return wrap(request, {}); 62 | } 63 | 64 | out.agent = function () { 65 | var self = this; 66 | if (!(this instanceof out.agent)) { 67 | self = Object.create(out.agent.prototype); 68 | } 69 | supertest.agent.apply(self, arguments); 70 | return self; 71 | } 72 | util.inherits(out.agent, supertest.agent); 73 | wrap(supertest.agent.prototype, out.agent.prototype); 74 | 75 | return out; 76 | } 77 | 78 | // For backwards compatibility, we allow SuperTest as Promised to be 79 | // used without an explicit `Promise` constructor. Pass these requests 80 | // through to a default module that uses Bluebird promises. 81 | 82 | var defaultModule = makeModule(PromiseBluebird); 83 | 84 | module.exports = function (maybePromise) { 85 | if (typeof maybePromise.resolve === 'function' && 86 | typeof maybePromise.reject === 'function') { 87 | return makeModule(maybePromise); 88 | } 89 | 90 | return defaultModule.apply(null, arguments); 91 | } 92 | 93 | module.exports.agent = defaultModule.agent; 94 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "supertest-as-promised", 3 | "version": "4.0.2", 4 | "description": "Supercharge supertest with a promise interface", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/WhoopInc/supertest-as-promised.git" 12 | }, 13 | "keywords": [ 14 | "supertest", 15 | "superagent", 16 | "request", 17 | "tdd", 18 | "bdd", 19 | "http", 20 | "test", 21 | "testing", 22 | "promise", 23 | "promised" 24 | ], 25 | "author": "Nikhil Benesch ", 26 | "license": "MIT", 27 | "bugs": { 28 | "url": "https://github.com/WhoopInc/supertest-as-promised/issues" 29 | }, 30 | "homepage": "https://github.com/WhoopInc/supertest-as-promised", 31 | "peerDependencies": { 32 | "supertest": "*" 33 | }, 34 | "devDependencies": { 35 | "chai": "^2.2.0", 36 | "chai-as-promised": "^4.3.0", 37 | "rewire": "^2.3.1", 38 | "supertest": "*", 39 | "when": "3.7.2", 40 | 41 | "body-parser": "~1.12.3", 42 | "cookie-parser": "~1.3.5", 43 | "express": "~4.12.4", 44 | "mocha": "~2.2.5", 45 | "should": "~7.0.2" 46 | }, 47 | "dependencies": { 48 | "bluebird": "^3.3.1", 49 | "methods": "^1.1.1" 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var expect = require("chai").expect 2 | , http = require("http") 3 | , Promise = require("bluebird") 4 | , PromiseWhen = require("when").Promise 5 | , supertest = require("supertest") 6 | , supertestAsPromised = require(".."); 7 | 8 | function PromiseMock() { 9 | var self = this; 10 | ["then", "catch"].forEach(function (fn) { 11 | self[fn] = function () { 12 | this[fn].args.push([].slice.call(arguments)); 13 | return this; 14 | } 15 | self[fn].args = []; 16 | }) 17 | } 18 | PromiseMock.resolve = PromiseMock.reject = function () {}; 19 | 20 | var server = http.createServer(function (req, res) { 21 | res.end("helo"); 22 | }); 23 | 24 | describe("supertestAsPromised", function () { 25 | var request = supertestAsPromised(server); 26 | 27 | describe("Test instances", function () { 28 | describe("#toPromise", function () { 29 | it("should return a bluebird promise by default", function () { 30 | expect(request.get("/home").toPromise()).to.be.an.instanceOf(Promise); 31 | }); 32 | 33 | it("should return a when promise if configured with when", function () { 34 | var request = supertestAsPromised(PromiseWhen)(server); 35 | expect(request.get("/home").toPromise()).to.be.an.instanceOf(PromiseWhen); 36 | }); 37 | 38 | it("should still return a bluebird promise by default", function () { 39 | expect(request.get("/home").toPromise()).to.be.an.instanceOf(Promise); 40 | }); 41 | }); 42 | 43 | it("should fulfill if all assertions pass", function () { 44 | return expect(request.get("/home").expect(200)).to.eventually.be.fulfilled; 45 | }); 46 | 47 | it("should fulfill with the response", function () { 48 | return request.get("/home").then(function (res) { 49 | expect(res.text).to.equal("helo"); 50 | }); 51 | }); 52 | 53 | it("should reject if an assertion fails", function () { 54 | return expect(request.get("/home").expect(500)).to.eventually.be.rejected; 55 | }); 56 | 57 | it("should provide response on error object", function () { 58 | return request.get("/home").expect(500) 59 | .catch(function (err) { 60 | return expect(err.response.text).to.equal("helo"); 61 | }); 62 | }); 63 | 64 | it("should call then with two arguments", function () { 65 | var mock = supertestAsPromised(PromiseMock)(server).get("/home").then(1, 2, 3); 66 | expect(mock.then.args).to.deep.equal([[1, 2]]); 67 | }); 68 | 69 | it("should call catch with arbitrary arguments", function () { 70 | var mock = supertestAsPromised(PromiseMock)(server).get("/home").catch(1, 2, 3); 71 | expect(mock.catch.args).to.deep.equal([[1, 2, 3]]); 72 | }); 73 | }); 74 | 75 | describe("TestAgent instances", function () { 76 | var agent = supertestAsPromised.agent(server); 77 | 78 | describe("#toPromise", function () { 79 | it("should return a promise", function () { 80 | expect(agent.get("/home").toPromise()).to.be.an.instanceOf(Promise); 81 | }); 82 | }); 83 | 84 | it("should fulfill if all assertions pass", function () { 85 | return expect(agent.get("/home").expect(200)).to.eventually.be.fulfilled; 86 | }); 87 | 88 | it("should fulfill with the response", function () { 89 | return agent.get("/home").then(function (res) { 90 | expect(res.text).to.equal("helo"); 91 | }); 92 | }); 93 | 94 | it("should reject if an assertion fails", function () { 95 | return expect(agent.get("/home").expect(500)).to.eventually.be.rejected; 96 | }); 97 | 98 | it("should be an instance of TestAgent", function () { 99 | expect(agent).to.be.an.instanceof(supertest.agent); 100 | }); 101 | }); 102 | }); 103 | 104 | describe("supertest", function () { 105 | describe("Test instances", function () { 106 | var request = supertest(server); 107 | 108 | it("should not have toPromise", function () { 109 | request.get("/home").should.not.have.property("toPromise"); 110 | }); 111 | }); 112 | 113 | describe("TestAgent instances", function () { 114 | var agent = supertest.agent(server); 115 | 116 | it("should not have toPromise", function () { 117 | agent.get("/home").should.not.have.property("toPromise"); 118 | }); 119 | }); 120 | }); 121 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --require test/setup.js 2 | --reporter spec 3 | -------------------------------------------------------------------------------- /test/setup.js: -------------------------------------------------------------------------------- 1 | var chai = require("chai") 2 | , chaiAsPromised = require("chai-as-promised"); 3 | 4 | chai.use(chaiAsPromised); 5 | -------------------------------------------------------------------------------- /test/supertest.js: -------------------------------------------------------------------------------- 1 | // Run upstream tests against our promisified version 2 | 3 | var rewire = require("rewire"); 4 | 5 | // Support upstream's hand-rolled test cert 6 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; 7 | 8 | var test; 9 | test = rewire("supertest/test/supertest"); 10 | test.__set__("request", require("..")); 11 | --------------------------------------------------------------------------------