-
Notifications
You must be signed in to change notification settings - Fork 1
/
basic-test.ts
91 lines (79 loc) · 2.65 KB
/
basic-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import express = require('express');
import { run, wait } from 'f-promise';
import request = require('supertest');
import { Application, Express, IRouterHandler, NextFunction, Request, Response, Router } from '../src';
import fexpress = require('../src');
function delay(x: string) {
wait<void>(cb => setTimeout(cb, 0));
return x;
}
let silent = false;
// do not show unhandled rejection error
process.on('unhandledRejection', (error: Error) => {
if (!silent) console.error(error.stack);
});
function test(name: string, fn: (app: Express) => (body: express.RequestHandler) => any) {
describe(name, function() {
it('should work with f-express', function(done) {
silent = false;
const app = fexpress();
fn(app)((req, res) => {
res.send(delay('hello'));
});
request(app)
.get('/')
.expect(200, 'hello', done);
});
it('should fail with express', function(done) {
silent = true;
const app = express();
fn(app)((req, res) => {
try {
res.send(delay('hello'));
} catch (ex) {
res.status(500).send(ex.message);
}
});
request(app)
.get('/')
.expect(500, /cannot wait: no fiber/, done);
});
});
}
test('app.use', app => app.use.bind(app));
test('app.get', app => (handler: express.RequestHandler) => app.get('/', handler));
test('router.get', app => {
const router: Router = express.Router();
app.use(router);
return (handler: express.RequestHandler) => router.get('/', handler);
});
test('simple middleware chain', app => {
return (handler: express.RequestHandler) => {
app.get('/', (req, res, next) => next(), handler);
};
});
test('middleware chain with one calling next inside a promise resolve', app => {
return (handler: express.RequestHandler) => {
app.get(
'/',
function(req, res, next) {
Promise.resolve().then(next);
},
handler,
);
};
});
describe('error handler middleware', () => {
it('should work with f-express', function(done) {
const app: Application = fexpress();
app.get('/', function(req, res, next) {
next(new Error('testing'));
});
app.use(function(err: Error, req: Request, res: Response, next: NextFunction) {
res.status(500).send(`ERR: ${err.message}`);
});
request(app)
.get('/')
.expect(500, 'ERR: testing', done);
});
});