|
| 1 | +import { describe, expect, test } from "vitest"; |
| 2 | +import { NicifyOptions, deindent, nicify } from "./strings"; |
| 3 | + |
| 4 | +describe("nicify", () => { |
| 5 | + describe("primitive values", () => { |
| 6 | + test("numbers", () => { |
| 7 | + expect(nicify(123)).toBe("123"); |
| 8 | + expect(nicify(123n)).toBe("123n"); |
| 9 | + }); |
| 10 | + |
| 11 | + test("strings", () => { |
| 12 | + expect(nicify("hello")).toBe('"hello"'); |
| 13 | + }); |
| 14 | + |
| 15 | + test("booleans", () => { |
| 16 | + expect(nicify(true)).toBe("true"); |
| 17 | + expect(nicify(false)).toBe("false"); |
| 18 | + }); |
| 19 | + |
| 20 | + test("null and undefined", () => { |
| 21 | + expect(nicify(null)).toBe("null"); |
| 22 | + expect(nicify(undefined)).toBe("undefined"); |
| 23 | + }); |
| 24 | + |
| 25 | + test("symbols", () => { |
| 26 | + expect(nicify(Symbol("test"))).toBe("Symbol(test)"); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe("arrays", () => { |
| 31 | + test("empty array", () => { |
| 32 | + expect(nicify([])).toBe("[]"); |
| 33 | + }); |
| 34 | + |
| 35 | + test("single-element array", () => { |
| 36 | + expect(nicify([1])).toBe("[1]"); |
| 37 | + }); |
| 38 | + |
| 39 | + test("single-element array with long content", () => { |
| 40 | + expect(nicify(["123123123123123"])).toBe('["123123123123123"]'); |
| 41 | + }); |
| 42 | + |
| 43 | + test("flat array", () => { |
| 44 | + expect(nicify([1, 2, 3])).toBe("[1, 2, 3]"); |
| 45 | + }); |
| 46 | + |
| 47 | + test("longer array", () => { |
| 48 | + expect(nicify([10000, 2, 3])).toBe(deindent` |
| 49 | + [ |
| 50 | + 10000, |
| 51 | + 2, |
| 52 | + 3, |
| 53 | + ] |
| 54 | + `); |
| 55 | + }); |
| 56 | + |
| 57 | + test("nested array", () => { |
| 58 | + expect(nicify([1, [2, 3]])).toBe(deindent` |
| 59 | + [ |
| 60 | + 1, |
| 61 | + [2, 3], |
| 62 | + ] |
| 63 | + `); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe("objects", () => { |
| 68 | + test("empty object", () => { |
| 69 | + expect(nicify({})).toBe("{}"); |
| 70 | + }); |
| 71 | + |
| 72 | + test("simple object", () => { |
| 73 | + expect(nicify({ a: 1 })).toBe('{ "a": 1 }'); |
| 74 | + }); |
| 75 | + |
| 76 | + test("multiline object", () => { |
| 77 | + expect(nicify({ a: 1, b: 2 })).toBe(deindent` |
| 78 | + { |
| 79 | + "a": 1, |
| 80 | + "b": 2, |
| 81 | + } |
| 82 | + `); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe("custom classes", () => { |
| 87 | + test("class instance", () => { |
| 88 | + class TestClass { |
| 89 | + constructor(public value: number) {} |
| 90 | + } |
| 91 | + expect(nicify(new TestClass(42))).toBe('TestClass { "value": 42 }'); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe("built-in objects", () => { |
| 96 | + test("URL", () => { |
| 97 | + expect(nicify(new URL("https://example.com"))).toBe('URL("https://example.com/")'); |
| 98 | + }); |
| 99 | + |
| 100 | + test("TypedArrays", () => { |
| 101 | + expect(nicify(new Uint8Array([1, 2, 3]))).toBe("Uint8Array([1,2,3])"); |
| 102 | + expect(nicify(new Int32Array([1, 2, 3]))).toBe("Int32Array([1,2,3])"); |
| 103 | + }); |
| 104 | + |
| 105 | + test("Error objects", () => { |
| 106 | + const error = new Error("test error"); |
| 107 | + const nicifiedError = nicify({ error }); |
| 108 | + expect(nicifiedError).toMatch(new RegExp(deindent` |
| 109 | + ^\{ |
| 110 | + "error": Error: test error |
| 111 | + Stack: |
| 112 | + at (.|\\n)* |
| 113 | + \}$ |
| 114 | + `)); |
| 115 | + }); |
| 116 | + |
| 117 | + test("Error objects with cause and an extra property", () => { |
| 118 | + const error = new Error("test error", { cause: new Error("cause") }); |
| 119 | + (error as any).extra = "something"; |
| 120 | + const nicifiedError = nicify(error, { lineIndent: "--" }); |
| 121 | + expect(nicifiedError).toMatch(new RegExp(deindent` |
| 122 | + ^Error: test error |
| 123 | + --Stack: |
| 124 | + ----at (.|\\n)+ |
| 125 | + --Extra properties: \{ "extra": "something" \} |
| 126 | + --Cause: |
| 127 | + ----Error: cause |
| 128 | + ------Stack: |
| 129 | + --------at (.|\\n)+$ |
| 130 | + `)); |
| 131 | + }); |
| 132 | + |
| 133 | + test("Headers", () => { |
| 134 | + const headers = new Headers(); |
| 135 | + headers.append("Content-Type", "application/json"); |
| 136 | + headers.append("Accept", "text/plain"); |
| 137 | + expect(nicify(headers)).toBe(deindent` |
| 138 | + Headers { |
| 139 | + "accept": "text/plain", |
| 140 | + "content-type": "application/json", |
| 141 | + }` |
| 142 | + ); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe("multiline strings", () => { |
| 147 | + test("basic multiline", () => { |
| 148 | + expect(nicify("line1\nline2")).toBe('deindent`\n line1\n line2\n`'); |
| 149 | + }); |
| 150 | + |
| 151 | + test("multiline with trailing newline", () => { |
| 152 | + expect(nicify("line1\nline2\n")).toBe('deindent`\n line1\n line2\n` + "\\n"'); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + describe("circular references", () => { |
| 157 | + test("object with self reference", () => { |
| 158 | + const circular: any = { a: 1 }; |
| 159 | + circular.self = circular; |
| 160 | + expect(nicify(circular)).toBe(deindent` |
| 161 | + { |
| 162 | + "a": 1, |
| 163 | + "self": Ref<value>, |
| 164 | + }` |
| 165 | + ); |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + describe("configuration options", () => { |
| 170 | + test("maxDepth", () => { |
| 171 | + const deep = { a: { b: { c: { d: { e: 1 } } } } }; |
| 172 | + expect(nicify(deep, { maxDepth: 2 })).toBe('{ "a": { "b": { ... } } }'); |
| 173 | + }); |
| 174 | + |
| 175 | + test("lineIndent", () => { |
| 176 | + expect(nicify({ a: 1, b: 2 }, { lineIndent: " " })).toBe(deindent` |
| 177 | + { |
| 178 | + "a": 1, |
| 179 | + "b": 2, |
| 180 | + } |
| 181 | + `); |
| 182 | + }); |
| 183 | + |
| 184 | + test("hideFields", () => { |
| 185 | + expect(nicify({ a: 1, b: 2, secret: "hidden" }, { hideFields: ["secret"] })).toBe(deindent` |
| 186 | + { |
| 187 | + "a": 1, |
| 188 | + "b": 2, |
| 189 | + <some fields may have been hidden>, |
| 190 | + } |
| 191 | + `); |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | + describe("custom overrides", () => { |
| 196 | + test("override with custom type", () => { |
| 197 | + expect(nicify({ type: "special" }, { |
| 198 | + overrides: ((value: unknown) => { |
| 199 | + if (typeof value === "object" && value && "type" in value && (value as any).type === "special") { |
| 200 | + return "SPECIAL"; |
| 201 | + } |
| 202 | + return null; |
| 203 | + }) as NicifyOptions["overrides"] |
| 204 | + })).toBe("SPECIAL"); |
| 205 | + }); |
| 206 | + }); |
| 207 | + |
| 208 | + describe("functions", () => { |
| 209 | + test("named function", () => { |
| 210 | + expect(nicify(function namedFunction() {})).toBe("function namedFunction(...) { ... }"); |
| 211 | + }); |
| 212 | + |
| 213 | + test("arrow function", () => { |
| 214 | + expect(nicify(() => {})).toBe("(...) => { ... }"); |
| 215 | + }); |
| 216 | + }); |
| 217 | + |
| 218 | + describe("Nicifiable interface", () => { |
| 219 | + test("object implementing Nicifiable", () => { |
| 220 | + const nicifiable = { |
| 221 | + value: 42, |
| 222 | + getNicifiableKeys() { |
| 223 | + return ["value"]; |
| 224 | + }, |
| 225 | + getNicifiedObjectExtraLines() { |
| 226 | + return ["// custom comment"]; |
| 227 | + } |
| 228 | + }; |
| 229 | + expect(nicify(nicifiable)).toBe(deindent` |
| 230 | + { |
| 231 | + "value": 42, |
| 232 | + // custom comment, |
| 233 | + } |
| 234 | + `); |
| 235 | + }); |
| 236 | + }); |
| 237 | + |
| 238 | + describe("unknown types", () => { |
| 239 | + test("object without prototype", () => { |
| 240 | + const unknownType = Object.create(null); |
| 241 | + unknownType.value = "test"; |
| 242 | + expect(nicify(unknownType)).toBe('{ "value": "test" }'); |
| 243 | + }); |
| 244 | + }); |
| 245 | +}); |
0 commit comments