-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
77 lines (63 loc) · 1.82 KB
/
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
const koaSome = require('.');
const assert = require('assert');
const { spy } = require('sinon');
describe('Koa Some', function () {
it('should select first success result', async function () {
const middleware = [
async (ctx, next) => {
throw new Error('e1');
},
async (ctx, next) => {
return next('good');
},
async (ctx, next) => {
throw new Error('e2');
},
]
const nextcb = spy();
await koaSome(middleware)({}, nextcb);
assert(nextcb.calledWith('good'), 'Next callback is not resolved');
});
it('should return first error', async function () {
const middleware = Array.from({ length: 5 }, (_, i) =>
async (ctx, next) => {
throw new Error(String(i));
}
);
const nextcb = spy();
try {
await koaSome(middleware)({}, nextcb);
} catch (e) {
assert(e.message === '0', 'Error is not the first one');
}
assert(!nextcb.called, 'Next is called on Error');
});
it('should restore ctx after error', async function () {
const ctx = { state: 1 };
const middleware = [
async (ctx, next) => {
ctx.state = 2;
throw new Error('e1');
},
async (ctx, next) => {
return next('good');
},
async (ctx, next) => {
ctx.state = 3;
throw new Error('e2');
},
]
await koaSome(middleware)({});
assert(ctx.state === 1, 'Context state is not restored');
});
it('should keep first error context', async function () {
const middleware = Array.from({ length: 5 }, (_, i) =>
async (ctx, next) => ctx.state = i
);
const ctx = {};
const nextcb = spy();
await koaSome(middleware)(ctx, nextcb);
assert(!nextcb.called, 'Next is called on Error');
assert(ctx.state === 0, 'Next is called on Error');
});
});