Open source personal finance application
test(logger): make isDev injectable and add unit tests
Ilya Komichev9c84179f9f8d
diff --git a/app/utils/logger.test.ts b/app/utils/logger.test.tsnew file mode 100644index 00000000..08ff7f9a--- /dev/null+++ b/app/utils/logger.test.ts@@ -0,0 +1,33 @@+import { afterEach, describe, expect, it, vi } from 'vitest'++import { createLogger } from '~/utils/logger'++describe('createLogger', () => {+ afterEach(() => {+ vi.restoreAllMocks()+ })++ it('error() calls console.error with prefix', () => {+ const spy = vi.spyOn(console, 'error').mockImplementation(() => {})++ createLogger('pfx').error('msg', 42)++ expect(spy).toHaveBeenCalledWith('[pfx]', 'msg', 42)+ })++ it('log() calls console.log with prefix in dev mode', () => {+ const spy = vi.spyOn(console, 'log').mockImplementation(() => {})++ createLogger('pfx', true).log('info')++ expect(spy).toHaveBeenCalledWith('[pfx]', 'info')+ })++ it('log() is no-op in production', () => {+ const spy = vi.spyOn(console, 'log').mockImplementation(() => {})++ createLogger('pfx', false).log('should not appear')++ expect(spy).not.toHaveBeenCalled()+ })+})diff --git a/app/utils/logger.ts b/app/utils/logger.tsindex f733a104..9a2a2109 100644--- a/app/utils/logger.ts+++ b/app/utils/logger.ts@@ -1,6 +1,4 @@-const isDev = import.meta.dev--export function createLogger(prefix: string) {+export function createLogger(prefix: string, isDev = import.meta.dev) {return {error: (...args: unknown[]) => console.error(`[${prefix}]`, ...args),log: isDev ? (...args: unknown[]) => console.log(`[${prefix}]`, ...args) : () => {},