Skip to content

Commit

Permalink
test: add bigint util test
Browse files Browse the repository at this point in the history
  • Loading branch information
kesoji committed Feb 16, 2025
1 parent 49ff902 commit 01e7666
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/shared/bigint-util.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
23 changes: 23 additions & 0 deletions vitest.workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
},
])

0 comments on commit 01e7666

Please sign in to comment.