Skip to content

Commit d1e0d69

Browse files
committed
Simplify tests
1 parent bc37828 commit d1e0d69

File tree

4 files changed

+62
-93
lines changed

4 files changed

+62
-93
lines changed

test/unit/logging/formatters.test.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe("Logging formatters", () => {
1717
expect(formatTime(60000)).toBe("1.00m");
1818
expect(formatTime(150000)).toBe("2.50m");
1919
expect(formatTime(3600000)).toBe("1.00h");
20-
expect(formatTime(7200000)).toBe("2.00h");
20+
expect(formatTime(7255000)).toBe("2.02h");
2121
});
2222

2323
describe("formatMethod", () => {
@@ -28,7 +28,7 @@ describe("Logging formatters", () => {
2828
expect(formatMethod("delete")).toBe("DELETE");
2929
});
3030

31-
it("defaults to GET for undefined", () => {
31+
it("defaults to GET for falsy values", () => {
3232
expect(formatMethod(undefined)).toBe("GET");
3333
expect(formatMethod("")).toBe("GET");
3434
});
@@ -45,14 +45,17 @@ describe("Logging formatters", () => {
4545
expect(result).toContain("?");
4646
});
4747

48-
it("calculates size for strings", () => {
49-
const result = formatContentLength({}, "hello");
50-
expect(result).toContain("5 B");
51-
});
52-
53-
it("calculates size for buffers", () => {
54-
const result = formatContentLength({}, Buffer.from("test"));
55-
expect(result).toContain("4 B");
48+
it.each([
49+
["hello", 5],
50+
[Buffer.from("test"), 4],
51+
[123, 8],
52+
[false, 8],
53+
[BigInt(1234), 4],
54+
[null, 0],
55+
[undefined, 0],
56+
])("calculates size for %s", (data: unknown, bytes: number) => {
57+
const result = formatContentLength({}, data);
58+
expect(result).toContain(`${bytes} B`);
5659
});
5760

5861
it("estimates size for objects", () => {
@@ -66,11 +69,6 @@ describe("Logging formatters", () => {
6669
const result = formatContentLength({}, circular);
6770
expect(result).toMatch(/~\d+/);
6871
});
69-
70-
it("handles null and undefined", () => {
71-
expect(formatContentLength({}, null)).toContain("0");
72-
expect(formatContentLength({}, undefined)).toContain("0");
73-
});
7472
});
7573

7674
describe("formatUri", () => {

test/unit/logging/httpLogger.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ describe("REST HTTP Logger", () => {
7676
it("distinguishes between network errors and response errors", () => {
7777
const logger = createMockLogger();
7878

79-
const networkError = new AxiosError("Network Error", "ECONNREFUSED");
79+
const networkError = new AxiosError("Some Network Error", "ECONNREFUSED");
8080
networkError.config = {
8181
metadata: createRequestMeta(),
8282
} as RequestConfigWithMeta;
8383

8484
logError(logger, networkError, HttpClientLogLevel.BASIC);
8585
expect(logger.error).toHaveBeenCalledWith(
86-
expect.stringContaining("Network Error"),
86+
expect.stringContaining("Some Network Error"),
8787
);
8888

8989
// Response error (4xx/5xx)
@@ -96,6 +96,9 @@ describe("REST HTTP Logger", () => {
9696

9797
logError(logger, responseError, HttpClientLogLevel.BASIC);
9898
expect(logger.error).toHaveBeenCalledWith(expect.stringContaining("400"));
99+
expect(logger.error).toHaveBeenCalledWith(
100+
expect.stringContaining("Bad Request"),
101+
);
99102
});
100103

101104
it("handles non-Axios errors", () => {

test/unit/logging/utils.test.ts

Lines changed: 41 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -18,82 +18,50 @@ describe("Logging utils", () => {
1818
});
1919

2020
describe("sizeOf", () => {
21-
describe("primitive types", () => {
22-
it("returns 0 for null and undefined", () => {
23-
expect(sizeOf(null)).toBe(0);
24-
expect(sizeOf(undefined)).toBe(0);
25-
});
26-
27-
it("returns fixed size for numbers and booleans", () => {
28-
// Numbers and booleans typically use 8 bytes
29-
expect(sizeOf(42)).toBeGreaterThan(0);
30-
expect(sizeOf(3.14)).toBeGreaterThan(0);
31-
expect(sizeOf(true)).toBeGreaterThan(0);
32-
expect(sizeOf(false)).toBeGreaterThan(0);
33-
34-
// They should all be the same size
35-
expect(sizeOf(42)).toBe(sizeOf(3.14));
36-
expect(sizeOf(true)).toBe(sizeOf(false));
37-
});
38-
39-
it("calculates byte length for strings", () => {
40-
expect(sizeOf("hello")).toBe(5);
41-
expect(sizeOf("")).toBe(0);
42-
// Unicode characters use multiple bytes
43-
expect(sizeOf("✓")).toBeGreaterThan(1);
44-
expect(sizeOf("unicode: ✓")).toBeGreaterThan(10);
45-
});
46-
47-
it("calculates byte length for bigints", () => {
48-
expect(sizeOf(BigInt(12345))).toBe(5);
49-
expect(sizeOf(BigInt(0))).toBe(1);
50-
});
51-
});
52-
53-
describe("buffer types", () => {
54-
it("returns byte length for Buffer", () => {
55-
expect(sizeOf(Buffer.from("test"))).toBe(4);
56-
expect(sizeOf(Buffer.alloc(100))).toBe(100);
57-
expect(sizeOf(Buffer.from([]))).toBe(0);
58-
});
59-
60-
it("returns byte length for typed arrays", () => {
61-
expect(sizeOf(new ArrayBuffer(50))).toBe(50);
62-
expect(sizeOf(new Uint8Array([1, 2, 3, 4]))).toBe(4);
63-
expect(sizeOf(new Int32Array([1, 2, 3]))).toBe(12); // 3 * 4 bytes
64-
expect(sizeOf(new Float64Array([1.0, 2.0]))).toBe(16); // 2 * 8 bytes
65-
});
21+
it.each([
22+
// Primitives return a fixed value
23+
[null, 0],
24+
[undefined, 0],
25+
[42, 8],
26+
[3.14, 8],
27+
[false, 8],
28+
// Strings
29+
["hello", 5],
30+
["✓", 3],
31+
["unicode: ✓", 12],
32+
// Buffers
33+
[Buffer.from("test"), 4],
34+
[BigInt(12345), 5],
35+
[BigInt(0), 1],
36+
[Buffer.alloc(100), 100],
37+
[Buffer.from([]), 0],
38+
// Typed-arrays
39+
[new ArrayBuffer(50), 50],
40+
[new Uint8Array([1, 2, 3, 4]), 4],
41+
[new Int32Array([1, 2, 3]), 12],
42+
[new Float64Array([1.0, 2.0]), 16],
43+
// Objects/untyped-arrays return undefined
44+
[{ size: 1024 }, 1024],
45+
[{ size: 0 }, 0],
46+
[{ size: "not a number" }, undefined],
47+
[[], undefined],
48+
[[1, 2, 3], undefined],
49+
[["a", "b", "c"], undefined],
50+
[{}, undefined],
51+
[{ foo: "bar" }, undefined],
52+
[{ nested: { value: 123 } }, undefined],
53+
])("returns size for %s", (data: unknown, bytes: number | undefined) => {
54+
expect(sizeOf(data)).toBe(bytes);
6655
});
6756

68-
describe("objects and arrays", () => {
69-
it("returns size property if available", () => {
70-
expect(sizeOf({ size: 1024 })).toBe(1024);
71-
expect(sizeOf({ size: 0 })).toBe(0);
72-
// Only use size if it's a number
73-
expect(sizeOf({ size: "not a number" })).toBeUndefined();
74-
});
75-
76-
it("returns undefined for arrays without size", () => {
77-
expect(sizeOf([])).toBeUndefined();
78-
expect(sizeOf([1, 2, 3])).toBeUndefined();
79-
expect(sizeOf(["a", "b", "c"])).toBeUndefined();
80-
});
81-
82-
it("returns undefined for plain objects without size", () => {
83-
expect(sizeOf({})).toBeUndefined();
84-
expect(sizeOf({ foo: "bar" })).toBeUndefined();
85-
expect(sizeOf({ nested: { value: 123 } })).toBeUndefined();
86-
});
87-
88-
it("handles circular references safely", () => {
89-
const circular: Record<string, unknown> = { a: 1 };
90-
circular.self = circular;
91-
expect(sizeOf(circular)).toBeUndefined();
57+
it("handles circular references safely", () => {
58+
const circular: Record<string, unknown> = { a: 1 };
59+
circular.self = circular;
60+
expect(sizeOf(circular)).toBeUndefined();
9261

93-
const arr: unknown[] = [1, 2, 3];
94-
arr.push(arr);
95-
expect(sizeOf(arr)).toBeUndefined();
96-
});
62+
const arr: unknown[] = [1, 2, 3];
63+
arr.push(arr);
64+
expect(sizeOf(arr)).toBeUndefined();
9765
});
9866
});
9967

test/unit/logging/wsLogger.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe("WS Logger", () => {
2626
const wsLogger = new WsLogger(logger, "wss://example.com");
2727

2828
wsLogger.logOpen();
29-
wsLogger.logMessage({ complex: "object" }); // Unknown size
29+
wsLogger.logMessage({ complex: "object" }); // Unknown size - no estimation
3030
wsLogger.logMessage("known");
3131
wsLogger.logClose();
3232

@@ -49,13 +49,13 @@ describe("WS Logger", () => {
4949
const wsLogger = new WsLogger(logger, "wss://example.com");
5050

5151
wsLogger.logOpen();
52-
for (let i = 0; i < 1500; i++) {
52+
for (let i = 0; i < 1100; i++) {
5353
wsLogger.logMessage("x");
5454
}
5555
wsLogger.logClose();
5656

5757
expect(logger.trace).toHaveBeenLastCalledWith(
58-
expect.stringMatching(/1[.,]5K\s*msgs/),
58+
expect.stringMatching(/1[.,]1K\s*msgs/),
5959
);
6060
});
6161

0 commit comments

Comments
 (0)