-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.test.js
59 lines (53 loc) · 1.36 KB
/
index.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
const logger = require('./index');
describe('Logger', function () {
beforeEach(() => {
this.ctx = {
status: 200,
state: {},
method: 'GET',
url: '/index-url',
request: {
query: '/index',
method: 'GET',
url: '/index-url',
path: '/index-path',
ip: '::1',
route: '/index-route',
host: 'localhost',
protocol: 'ipv4',
get: () => 'device-id',
},
response: {
set: () => { },
}
}
});
it('Should be able to redefine options', async () => {
let call = 0;
const opt = {
async fillInfo(ctx) {
call++;
ctx.__logInfo = ctx.state.__logInfo = { myInfo: 'tutu' };
},
};
await logger(opt)(this.ctx, () => { });
expect(call).toBe(1);
});
it('Should not call loggerError when success', async () => {
const myLogger = { info: jest.fn(), error: jest.fn() };
const opt = { logger: myLogger };
await logger(opt)(this.ctx, () => { });
expect(myLogger.info).toBeCalled();
expect(myLogger.error).not.toBeCalled();
});
it('Should call loggerError on error', async () => {
const msg = 'my error';
let errorMsg;
try {
await logger()(this.ctx, () => { throw new Error(msg) });
} catch (err) {
errorMsg = err.message;
}
expect(errorMsg).toBe('my error');
});
})