supertest-as-promised

See https://github.com/WhoopInc/supertest-as-promised
This commit is contained in:
Tanguy Krotoff 2015-11-20 18:52:33 +01:00
parent 16134c168d
commit 5a1c6dc6ac
2 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,64 @@
/// <reference path="supertest-as-promised.d.ts" />
/// <reference path="../express/express.d.ts" />
/// <reference path="../jasmine/jasmine.d.ts" />
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) { /* ... */ })

View File

@ -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 <https://github.com/tkrotoff>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path='../superagent/superagent.d.ts' />
/// <reference path="../bluebird/bluebird.d.ts" />
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<Test> {
}
interface Promise<T> extends PromiseBluebird<T> {
toPromise(): PromiseBluebird<T>;
}
interface Test extends superagent.Request<Test> {
url: string;
serverAddress(app: any, path: string): string;
expect(status: number): Promise<supertest.Response>;
expect(status: number, body: string): Promise<supertest.Response>;
expect(body: string): Promise<supertest.Response>;
expect(body: RegExp): Promise<supertest.Response>;
expect(body: Object): Promise<supertest.Response>;
expect(field: string, val: string): Promise<supertest.Response>;
expect(field: string, val: RegExp): Promise<supertest.Response>;
expect(checker: (res: Response) => any): Promise<supertest.Response>;
}
interface Response extends superagent.Response {
}
}
export = supertest;
}