diff --git a/supertest-as-promised/supertest-as-promised-tests.ts b/supertest-as-promised/supertest-as-promised-tests.ts new file mode 100644 index 0000000000..2b79abd07f --- /dev/null +++ b/supertest-as-promised/supertest-as-promised-tests.ts @@ -0,0 +1,64 @@ +/// +/// +/// + +import * as request from 'supertest-as-promised'; +import * as express from 'express'; + +var app = express(); + +// chain your requests like you were promised: +request(app) + .get("/user") + .expect(200) + .then(function (res) { + return request(app) + .post("/kittens") + .send({ userId: res}) + .expect(201); + }) + .then(function (res) { + // ... + }); + + +// Usage +request(app) + .get("/kittens") + .expect(200) + .then(function (res) { + // ... + }); + +describe("GET /kittens", function () { + it("should work", function () { + return request(app).get("/kittens").expect(200); + }); +}); + + +// Agents +var agent = request.agent(app); +agent + .get("/ugly-kitteh") + .expect(404) + .then(function () { + // ... + }) + + +// Promisey goodness +request(app) + .get("/kittens") + .expect(201) + .then(function (res) { /* ... */ }) + // I'm a real promise now! + .catch(function (err) { /* ... */ }) + +request(app) + .get("/kittens") + .expect(201) + .toPromise() + // I'm a real promise now! + .delay(10) + .then(function (res) { /* ... */ }) diff --git a/supertest-as-promised/supertest-as-promised.d.ts b/supertest-as-promised/supertest-as-promised.d.ts new file mode 100644 index 0000000000..2c035848f4 --- /dev/null +++ b/supertest-as-promised/supertest-as-promised.d.ts @@ -0,0 +1,45 @@ +// Type definitions for SuperTest as Promised v2.0.2 +// Project: https://github.com/WhoopInc/supertest-as-promised +// Definitions by: Tanguy Krotoff +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// + +declare module "supertest-as-promised" { + // Mostly copy-pasted from supertest.d.ts + + import * as superagent from 'superagent'; + import * as PromiseBluebird from 'bluebird'; + + function supertest(app: any): supertest.SuperTest; + + module supertest { + function agent(app?: any): supertest.SuperTest; + + interface SuperTest extends superagent.SuperAgent { + } + + interface Promise extends PromiseBluebird { + toPromise(): PromiseBluebird; + } + + interface Test extends superagent.Request { + url: string; + serverAddress(app: any, path: string): string; + expect(status: number): Promise; + expect(status: number, body: string): Promise; + expect(body: string): Promise; + expect(body: RegExp): Promise; + expect(body: Object): Promise; + expect(field: string, val: string): Promise; + expect(field: string, val: RegExp): Promise; + expect(checker: (res: Response) => any): Promise; + } + + interface Response extends superagent.Response { + } + } + + export = supertest; +}