|
1
|
import winston from "winston" |
|
2
|
import Transport from "winston-transport" |
|
3
|
import { Reporter } from "../" |
|
4
|
|
|
5
|
describe(`Reporter`, () => { |
|
6
|
const winstonMock = { |
|
7
|
log: jest.fn(), |
|
8
|
} |
|
9
|
|
|
10
|
const reporter = new Reporter({ |
|
11
|
logger: winstonMock, |
|
12
|
activityLogger: {}, |
|
13
|
}) |
|
14
|
|
|
15
|
const getErrorMessages = (fn) => |
|
16
|
fn.mock.calls |
|
17
|
.map(([firstArg]) => firstArg) |
|
18
|
.filter((structuredMessage) => structuredMessage.level === `error`) |
|
19
|
|
|
20
|
beforeEach(() => { |
|
21
|
winstonMock.log.mockClear() |
|
22
|
}) |
|
23
|
|
|
24
|
it(`handles "String" signature correctly`, () => { |
|
25
|
reporter.error("Test log") |
|
26
|
expect(getErrorMessages(winstonMock.log)).toMatchSnapshot() |
|
27
|
}) |
|
28
|
|
|
29
|
it(`handles "String, Error" signature correctly`, () => { |
|
30
|
reporter.error("Test log", new Error("String Error")) |
|
31
|
|
|
32
|
const generated = getErrorMessages(winstonMock.log) |
|
33
|
|
|
34
|
expect(generated).toMatchSnapshot([ |
|
35
|
{ |
|
36
|
stack: expect.any(Array), |
|
37
|
}, |
|
38
|
]) |
|
39
|
}) |
|
40
|
|
|
41
|
it(`handles "Error" signature correctly`, () => { |
|
42
|
reporter.error(new Error("Error")) |
|
43
|
|
|
44
|
const generated = getErrorMessages(winstonMock.log) |
|
45
|
|
|
46
|
expect(generated).toMatchSnapshot([ |
|
47
|
{ |
|
48
|
stack: expect.any(Array), |
|
49
|
}, |
|
50
|
]) |
|
51
|
}) |
|
52
|
|
|
53
|
it(`should display error cause when using json formatter`, () => { |
|
54
|
class MemoryTransport extends Transport { |
|
55
|
logs = [] |
|
56
|
log(info, callback) { |
|
57
|
this.logs.push(info) |
|
58
|
callback() |
|
59
|
} |
|
60
|
} |
|
61
|
const transport = new MemoryTransport() |
|
62
|
|
|
63
|
const jsonReporter = new Reporter({ |
|
64
|
logger: winston.createLogger({ |
|
65
|
level: "info", |
|
66
|
levels: winston.config.npm.levels, |
|
67
|
format: winston.format.combine( |
|
68
|
winston.format.timestamp({ |
|
69
|
format: "YYYY-MM-DD HH:mm:ss", |
|
70
|
}), |
|
71
|
winston.format.errors({ stack: true }), |
|
72
|
winston.format.splat(), |
|
73
|
winston.format.json() |
|
74
|
), |
|
75
|
transports: [transport], |
|
76
|
}), |
|
77
|
}) |
|
78
|
jsonReporter.error(new Error("Error", { cause: new Error("Nested error") })) |
|
79
|
|
|
80
|
expect(transport.logs).toHaveLength(1) |
|
81
|
expect(transport.logs[0]).toHaveProperty("cause") |
|
82
|
expect(transport.logs[0].cause).toMatch("Nested error") |
|
83
|
}) |
|
84
|
|
|
85
|
it(`should display custom message and error cause when using json formatter`, () => { |
|
86
|
class MemoryTransport extends Transport { |
|
87
|
logs = [] |
|
88
|
log(info, callback) { |
|
89
|
this.logs.push(info) |
|
90
|
callback() |
|
91
|
} |
|
92
|
} |
|
93
|
const transport = new MemoryTransport() |
|
94
|
|
|
95
|
const jsonReporter = new Reporter({ |
|
96
|
logger: winston.createLogger({ |
|
97
|
level: "info", |
|
98
|
levels: winston.config.npm.levels, |
|
99
|
format: winston.format.combine( |
|
100
|
winston.format.timestamp({ |
|
101
|
format: "YYYY-MM-DD HH:mm:ss", |
|
102
|
}), |
|
103
|
winston.format.errors({ stack: true }), |
|
104
|
winston.format.splat(), |
|
105
|
winston.format.json() |
|
106
|
), |
|
107
|
transports: [transport], |
|
108
|
}), |
|
109
|
}) |
|
110
|
jsonReporter.error( |
|
111
|
"Something went wrong", |
|
112
|
new Error("Error", { cause: new Error("Nested error") }) |
|
113
|
) |
|
114
|
|
|
115
|
expect(transport.logs).toHaveLength(1) |
|
116
|
expect(transport.logs[0].message).toEqual("Something went wrong: Error") |
|
117
|
expect(transport.logs[0]).toHaveProperty("stack") |
|
118
|
expect(transport.logs[0].stack).toEqual(expect.any(Array)) |
|
119
|
expect(transport.logs[0]).toHaveProperty("cause") |
|
120
|
expect(transport.logs[0].cause).toMatch("Nested error") |
|
121
|
}) |
|
122
|
}) |