forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracing.util.spec.ts
More file actions
83 lines (77 loc) · 2.07 KB
/
Copy pathtracing.util.spec.ts
File metadata and controls
83 lines (77 loc) · 2.07 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
76
77
78
79
80
81
82
83
import { StructuredLogger } from "./structured-logger.service";
import { RequestContextService } from "./request-context.service";
import { traceAsync } from "./tracing.util";
describe("traceAsync", () => {
const requestContext = new RequestContextService();
const logger = new StructuredLogger(requestContext);
const infoSpy = jest
.spyOn(logger, "info")
.mockImplementation(() => undefined);
const warnSpy = jest
.spyOn(logger, "warn")
.mockImplementation(() => undefined);
const debugSpy = jest
.spyOn(logger, "debug")
.mockImplementation(() => undefined);
const errorSpy = jest
.spyOn(logger, "error")
.mockImplementation(() => undefined);
beforeEach(() => {
jest.clearAllMocks();
});
it("flags slow network spans", async () => {
await requestContext.runWithContext(
{ correlationId: "trace-1", traceId: "trace-1" },
async () => {
await traceAsync(
logger,
{
operation: "test.slow",
category: "network",
slowThresholdMs: 0,
},
async () => {
await new Promise((resolve) => setTimeout(resolve, 5));
return "ok";
},
);
},
);
expect(warnSpy).toHaveBeenCalledWith(
"span.slow",
expect.objectContaining({
operation: "test.slow",
category: "network",
slow: true,
}),
);
expect(debugSpy).not.toHaveBeenCalledWith(
"span.complete",
expect.anything(),
);
});
it("logs span errors", async () => {
await expect(
traceAsync(
logger,
{
operation: "test.error",
category: "database",
slowThresholdMs: 200,
},
async () => {
throw new Error("db failed");
},
),
).rejects.toThrow("db failed");
expect(errorSpy).toHaveBeenCalledWith(
"span.error",
expect.objectContaining({
operation: "test.error",
category: "database",
error: "db failed",
}),
);
expect(infoSpy).not.toHaveBeenCalled();
});
});