-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathlogError.spec.ts
More file actions
93 lines (84 loc) · 2.8 KB
/
logError.spec.ts
File metadata and controls
93 lines (84 loc) · 2.8 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
84
85
86
87
88
89
90
91
92
93
import { expect } from "chai";
import * as sinon from "sinon";
import { logError } from "./logError";
import { logger } from "./logger";
describe("logError", () => {
let sandbox: sinon.SinonSandbox;
let errorSpy: sinon.SinonSpy;
let debugSpy: sinon.SinonSpy;
beforeEach(() => {
sandbox = sinon.createSandbox();
errorSpy = sandbox.spy(logger, "error");
debugSpy = sandbox.spy(logger, "debug");
});
afterEach(() => {
sandbox.restore();
});
it("should log a simple error message", () => {
const error = { message: "A simple error has occurred." };
logError(error);
expect(errorSpy).to.have.been.calledWith(sinon.match.any, "A simple error has occurred.");
});
it("should log an error with children", () => {
const error = {
message: "An error with children has occurred.",
children: [{ name: "Child1", message: "Child error 1" }, { message: "Child error 2" }],
};
logError(error);
expect(errorSpy).to.have.been.calledWith(
sinon.match.any,
sinon.match(/An error with children has occurred./),
);
expect(errorSpy).to.have.been.calledWith(sinon.match(/- .*Child1.* Child error 1/));
expect(errorSpy).to.have.been.calledWith(sinon.match(/- Child error 2/));
});
it("should log an error with an original stack", () => {
const error = {
message: "An error with an original stack.",
original: { stack: "the stack" },
};
logError(error);
expect(debugSpy).to.have.been.calledWith("the stack");
});
it("should log an error with a context", () => {
const error = {
message: "An error with a context.",
context: { key: "value" },
};
logError(error);
expect(debugSpy).to.have.been.calledWith(
"Error Context:",
JSON.stringify({ key: "value" }, undefined, 2),
);
});
it("should log an error with both original stack and context", () => {
const error = {
message: "An error with both.",
original: { stack: "the stack" },
context: { key: "value" },
};
logError(error);
expect(debugSpy).to.have.been.calledWith("the stack");
expect(debugSpy).to.have.been.calledWith(
"Error Context:",
JSON.stringify({ key: "value" }, undefined, 2),
);
});
it("should log an error with children and context", () => {
const error = {
message: "An error with children and context.",
children: [{ message: "Child error" }],
context: { key: "value" },
};
logError(error);
expect(errorSpy).to.have.been.calledWith(
sinon.match.any,
sinon.match(/An error with children and context./),
);
expect(errorSpy).to.have.been.calledWith(sinon.match(/- Child error/));
expect(debugSpy).to.have.been.calledWith(
"Error Context:",
JSON.stringify({ key: "value" }, undefined, 2),
);
});
});