refactor: add strict JSON parsing helpers

This commit is contained in:
2026-08-22 16:54:29 +02:00
parent 30d1d0aeae
commit 8548afcd21
2 changed files with 36 additions and 0 deletions
+16
View File
@@ -2,6 +2,9 @@
// a browser) plus the mock-mint helpers every suite uses.
import {vi} from 'vitest'
import {parseJsonArray, parseJsonObject, parseJsonObjectArray} from './jsonParsing'
export {parseJsonArray, parseJsonObject, parseJsonObjectArray}
export class LocalStorageStub {
private map = new Map<string, string>()
@@ -26,3 +29,16 @@ export const stubLocalStorage = (): LocalStorageStub => {
vi.stubGlobal('localStorage', stub)
return stub
}
export const requiredValue = <T>(
value: T | null | undefined,
message = 'Expected test value to be present',
): T => {
if (value === null || value === undefined) throw new TypeError(message)
return value
}
export const requiredString = (value: unknown, message = 'Expected a string value'): string => {
if (typeof value !== 'string') throw new TypeError(message)
return value
}