-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
75 lines (52 loc) · 1.57 KB
/
test.js
File metadata and controls
75 lines (52 loc) · 1.57 KB
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
require('./');
const t = require('tap');
const express = require('express');
const request = require('supertest');
t.test('express-async-patch', async (t) => {
await t.test('still working in old way', async (t) => {
t.plan(3);
const app = express();
app.get('/', () => {
throw new Error('thrown error');
});
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
t.is(err.message, 'thrown error');
res.json({error: err.message});
});
const {body, statusCode} = await request(app).get('/');
t.strictSame(body, {error: 'thrown error'});
t.is(statusCode, 200);
});
await t.test('catch error in async handler', async (t) => {
t.plan(3);
const app = express();
app.get('/', async () => {
await Promise.reject(new Error('async error'));
});
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
t.is(err.message, 'async error');
res.json({error: err.message});
});
const {body, statusCode} = await request(app).get('/');
t.strictSame(body, {error: 'async error'});
t.is(statusCode, 200);
});
await t.test('async error middleware', async (t) => {
t.plan(3);
const app = express();
app.get('/', () => {
throw new Error('thrown error');
});
// eslint-disable-next-line no-unused-vars
app.use(async (err, req, res, next) => {
await Promise.resolve(42);
t.is(err.message, 'thrown error');
res.json({error: err.message});
});
const {body, statusCode} = await request(app).get('/');
t.strictSame(body, {error: 'thrown error'});
t.is(statusCode, 200);
});
});