Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"lint": "biome lint .",
"format": "biome format --write .",
"typecheck": "tsc --noEmit",
"test": "vitest run --passWithNoTests",
"test:watch": "vitest",
"test": "vitest --passWithNoTests",
"test:ci": "vitest run --passWithNoTests"
},
"devDependencies": {
Expand Down
7 changes: 1 addition & 6 deletions packages/utils/src/__tests__/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ const ORIGINAL_ENV = process.env.NODE_ENV;

describe('storage utils', () => {
function ensureWindowWithLocalStorage() {
// Ensure a Window-like global for Node environment
if (typeof window === 'undefined') {
Object.defineProperty(globalThis, 'window', {
// unknown avoids explicit any; cast to Window shape for tests
value: {} as unknown as Window & typeof globalThis,
configurable: true
});
}
// Polyfill localStorage if missing
if (!('localStorage' in window)) {
const store = new Map<string, string>();
Object.defineProperty(window, 'localStorage', {
Expand All @@ -35,7 +32,7 @@ describe('storage utils', () => {
}

beforeEach(() => {
// Ensure clean slate
ensureWindowWithLocalStorage();
try {
window.localStorage.removeItem(KEY);
} catch {
Expand All @@ -53,14 +50,12 @@ describe('storage utils', () => {
});

it('SSR/test guard disables storage (returns fallback in test env)', () => {
// In vitest, NODE_ENV is "test" by default. Verify guard path returns fallback.
window.localStorage.setItem(KEY, JSON.stringify({ value: 123 }));
const result = loadFromStorage(KEY, { value: 999 });
expect(result).toEqual({ value: 999 });
});

it('Malformed JSON returns fallback', () => {
// Enable storage access by switching to a non-test env for this test
process.env.NODE_ENV = 'development';
ensureWindowWithLocalStorage();
window.localStorage.setItem(KEY, '{not json');
Expand Down
2 changes: 0 additions & 2 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ export { cn } from './cn';
export type { Todo, TodoFilter, TodoStore } from './types';
export { loadFromStorage, saveToStorage, removeFromStorage } from './storage';
export type { StorageLike } from './storage';
// Re-export type for validator usage in tests and apps
export type { } from './storage';
6 changes: 2 additions & 4 deletions packages/utils/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ function getStorage(): StorageLike | null {
}
}

export function loadFromStorage<T>(key: string, fallback: T): T;
export function loadFromStorage<T>(key: string, fallback: T, validate: (value: unknown) => value is T | boolean): T;
export function loadFromStorage<T>(key: string, fallback: T, validate?: (value: unknown) => value is T | boolean): T {
export function loadFromStorage<T>(key: string, fallback: T, validate?: (value: unknown) => value is T): T {
const storage = getStorage();
if (!storage) return fallback;
try {
const raw = storage.getItem(key);
if (!raw) return fallback;
const parsed = JSON.parse(raw) as unknown;
if (validate && !validate(parsed)) return fallback; // Add optional validation guard
if (validate && !validate(parsed)) return fallback;
return parsed as T;
} catch {
return fallback;
Expand Down
15 changes: 3 additions & 12 deletions packages/utils/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
// Default to jsdom for React + DOM/localStorage tests
environment: 'jsdom',

// Optional: run Node env for server-only utils tests
environmentMatchGlobs: [
['packages/utils/**', 'node'],
],

// Optional (we already import from 'vitest')
// globals: true,
},
});
environment: 'jsdom'
}
});