Skip to content

Commit 9f81264

Browse files
committed
simplify some tests w arrays
1 parent 5698d01 commit 9f81264

1 file changed

Lines changed: 16 additions & 55 deletions

File tree

‎src/trace/patch-console.spec.ts‎

Lines changed: 16 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,14 @@ describe("patchConsole", () => {
4141
unpatchConsole(cnsole as any);
4242
});
4343

44-
it("injects trace context into log messages", () => {
45-
patchConsole(cnsole as any, contextService);
46-
cnsole.log("Hello");
47-
expect(log).toHaveBeenCalledWith("[dd.trace_id=123456 dd.span_id=78910] Hello");
48-
});
49-
it("injects trace context into info messages", () => {
50-
patchConsole(cnsole as any, contextService);
51-
cnsole.info("Hello");
52-
expect(info).toHaveBeenCalledWith("[dd.trace_id=123456 dd.span_id=78910] Hello");
53-
});
54-
it("injects trace context into debug messages", () => {
55-
patchConsole(cnsole as any, contextService);
56-
cnsole.debug("Hello");
57-
expect(debug).toHaveBeenCalledWith("[dd.trace_id=123456 dd.span_id=78910] Hello");
58-
});
59-
it("injects trace context into error messages", () => {
60-
patchConsole(cnsole as any, contextService);
61-
cnsole.error("Hello");
62-
expect(error).toHaveBeenCalledWith("[dd.trace_id=123456 dd.span_id=78910] Hello");
63-
});
64-
it("injects trace context into warn messages", () => {
65-
patchConsole(cnsole as any, contextService);
66-
cnsole.warn("Hello");
67-
expect(warn).toHaveBeenCalledWith("[dd.trace_id=123456 dd.span_id=78910] Hello");
68-
});
69-
it("injects trace context into trace messages", () => {
70-
patchConsole(cnsole as any, contextService);
71-
cnsole.trace("Hello");
72-
expect(trace).toHaveBeenCalledWith("[dd.trace_id=123456 dd.span_id=78910] Hello");
73-
});
44+
it.each(["log", "info", "debug", "error", "warn", "trace"] as const)(
45+
"injects trace context into %s messages",
46+
(method) => {
47+
patchConsole(cnsole as any, contextService);
48+
cnsole[method]("Hello");
49+
expect(cnsole[method]).toHaveBeenCalledWith("[dd.trace_id=123456 dd.span_id=78910] Hello");
50+
},
51+
);
7452

7553
it("doesn't inject trace context when none is present", () => {
7654
contextService["rootTraceContext"] = undefined as any;
@@ -97,11 +75,15 @@ describe("patchConsole", () => {
9775
});
9876
});
9977

100-
it("injects trace context into logged array as string prefix", () => {
78+
it.each([
79+
{ name: "array", value: [1, 2, 3], expected: "[dd.trace_id=123456 dd.span_id=78910] 1,2,3" },
80+
{ name: "null", value: null, expected: "[dd.trace_id=123456 dd.span_id=78910] null" },
81+
{ name: "number", value: 42, expected: "[dd.trace_id=123456 dd.span_id=78910] 42" },
82+
{ name: "undefined", value: undefined, expected: "[dd.trace_id=123456 dd.span_id=78910] undefined" },
83+
])("injects trace context as string prefix for $name", ({ value, expected }) => {
10184
patchConsole(cnsole as any, contextService);
102-
103-
cnsole.log([1, 2, 3]);
104-
expect(log).toHaveBeenCalledWith("[dd.trace_id=123456 dd.span_id=78910] 1,2,3");
85+
cnsole.log(value);
86+
expect(log).toHaveBeenCalledWith(expected);
10587
});
10688

10789
it("injects trace context as string prefix when multiple arguments provided", () => {
@@ -122,27 +104,6 @@ describe("patchConsole", () => {
122104
expect(log).toHaveBeenCalledWith("[dd.trace_id=123456 dd.span_id=78910] [object Object]");
123105
});
124106

125-
it("injects trace context as string prefix for null", () => {
126-
patchConsole(cnsole as any, contextService);
127-
128-
cnsole.log(null);
129-
expect(log).toHaveBeenCalledWith("[dd.trace_id=123456 dd.span_id=78910] null");
130-
});
131-
132-
it("injects trace context as string prefix for number", () => {
133-
patchConsole(cnsole as any, contextService);
134-
135-
cnsole.log(42);
136-
expect(log).toHaveBeenCalledWith("[dd.trace_id=123456 dd.span_id=78910] 42");
137-
});
138-
139-
it("injects trace context as string prefix for undefined", () => {
140-
patchConsole(cnsole as any, contextService);
141-
142-
cnsole.log(undefined);
143-
expect(log).toHaveBeenCalledWith("[dd.trace_id=123456 dd.span_id=78910] undefined");
144-
});
145-
146107
it("injects trace context into JSON-style log created with Object.create(null)", () => {
147108
patchConsole(cnsole as any, contextService);
148109

0 commit comments

Comments
 (0)