diff --git a/src/shared/bigint-util.test.ts b/src/shared/bigint-util.test.ts new file mode 100644 index 0000000..24f5ae8 --- /dev/null +++ b/src/shared/bigint-util.test.ts @@ -0,0 +1,66 @@ +import { bigIntReplacer, convertBigIntToString } from "./bigint-util" + +const BIG_INT_VALUE = BigInt(10 ** 20) +const BIG_INT_STRING_VALUE = "100000000000000000000" + +describe("bigIntReplacer", () => { + it("should convert bigint to string", () => { + const sut = { + key: BIG_INT_VALUE, + nestedKey: { + key: BIG_INT_VALUE, + }, + } + const result = JSON.stringify(sut, bigIntReplacer) + expect(result).toBe(`{"key":"${BIG_INT_STRING_VALUE}","nestedKey":{"key":"${BIG_INT_STRING_VALUE}"}}`) + }) + + it("should return value as is if not a bigint", () => { + const sut = { + key: 100, + } + const result = JSON.stringify(sut, bigIntReplacer) + expect(result).toBe('{"key":100}') + }) +}) + +describe("convertBigIntToString", () => { + it("should convert bigint to string", () => { + const result = convertBigIntToString(BIG_INT_VALUE) + expect(result).toBe(BIG_INT_STRING_VALUE) + }) + + it("should convert bigint in array to string", () => { + const result = convertBigIntToString([BIG_INT_VALUE, 123]) + expect(result).toEqual([BIG_INT_STRING_VALUE, 123]) + }) + + it("should convert bigint in object to string", () => { + const result = convertBigIntToString({ key: BIG_INT_VALUE, anotherKey: 123 }) + expect(result).toEqual({ key: BIG_INT_STRING_VALUE, anotherKey: 123 }) + }) + + it("should handle nested structures", () => { + const data = { + key: BIG_INT_VALUE, + nested: { + anotherKey: BIG_INT_VALUE, + array: [BIG_INT_VALUE, 123], + }, + } + const expected = { + key: BIG_INT_STRING_VALUE, + nested: { + anotherKey: BIG_INT_STRING_VALUE, + array: [BIG_INT_STRING_VALUE, 123], + }, + } + const result = convertBigIntToString(data) + expect(result).toEqual(expected) + }) + + it("should return non-bigint values as is", () => { + const result = convertBigIntToString(123) + expect(result).toBe(123) + }) +}) diff --git a/vitest.workspace.ts b/vitest.workspace.ts index d6ed7cf..5f4ee37 100644 --- a/vitest.workspace.ts +++ b/vitest.workspace.ts @@ -77,4 +77,27 @@ export default defineWorkspace([ }, }, }, + { + test: { + globals: true, + exclude: ["**/node_modules/**", "**/dist/**", "**/docs/**", "**/public/**", "**/test-apps/**"], + environment: "node", + root: "./src/shared", + name: "react-router-devtools/shared", + // @ts-expect-error + coverage: { + provider: "v8", + include: ["src/**/*"], + reporter: ["text", "json-summary", "json", "html"], + reportOnFailure: true, + all: false, + thresholds: { + statements: 70, + branches: 75, + functions: 70, + lines: 70, + }, + }, + }, + }, ])