forked from koajs/koa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse.test.js
88 lines (70 loc) · 1.82 KB
/
use.test.js
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
'use strict'
const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
describe('app.use(fn)', () => {
it('should compose middleware', async () => {
const app = new Koa()
const calls = []
app.use((ctx, next) => {
calls.push(1)
return next().then(() => {
calls.push(6)
})
})
app.use((ctx, next) => {
calls.push(2)
return next().then(() => {
calls.push(5)
})
})
app.use((ctx, next) => {
calls.push(3)
return next().then(() => {
calls.push(4)
})
})
await request(app.callback())
.get('/')
.expect(404)
assert.deepStrictEqual(calls, [1, 2, 3, 4, 5, 6])
})
it('should compose mixed middleware', async () => {
const app = new Koa()
const calls = []
app.use((ctx, next) => {
calls.push(1)
return next().then(() => {
calls.push(6)
})
})
app.use(async (ctx, next) => {
calls.push(2)
await next()
calls.push(5)
})
app.use((ctx, next) => {
calls.push(3)
return next().then(() => {
calls.push(4)
})
})
await request(app.callback())
.get('/')
.expect(404)
assert.deepStrictEqual(calls, [1, 2, 3, 4, 5, 6])
})
// https://github.com/koajs/koa/pull/530#issuecomment-148138051
it('should catch thrown errors in non-async functions', () => {
const app = new Koa()
app.use(ctx => ctx.throw(404, 'Not Found'))
return request(app.callback()).get('/').expect(404)
})
it('should throw error for non-function', () => {
const app = new Koa();
[null, undefined, 0, false, 'not a function'].forEach(v => {
assert.throws(() => app.use(v), /middleware must be a function!/)
})
})
})