Files
nuxt4/tests/logger.plugin.spec.ts
2026-05-03 07:26:12 +00:00

53 lines
1.4 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
async function loadPlugin() {
vi.resetModules()
vi.stubGlobal('defineNuxtPlugin', (setup: unknown) => setup)
return (await import('../app/plugins/logger')).default
}
describe('logger plugin', () => {
beforeEach(() => {
vi.restoreAllMocks()
vi.unstubAllGlobals()
})
it('logs messages with the expected prefix and payload', async () => {
vi.useFakeTimers()
vi.setSystemTime(new Date('2026-03-29T12:00:00.000Z'))
const infoSpy = vi.spyOn(console, 'info').mockImplementation(() => {})
const plugin = await loadPlugin()
const { provide } = plugin()
provide.logger.info('Action completed', { section: 'home' })
expect(infoSpy).toHaveBeenCalledWith(
'[template:info] 2026-03-29T12:00:00.000Z Action completed',
{ section: 'home' }
)
vi.useRealTimers()
})
it('logs messages without payload as a single console argument', async () => {
vi.useFakeTimers()
vi.setSystemTime(new Date('2026-03-29T12:00:00.000Z'))
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
const plugin = await loadPlugin()
const { provide } = plugin()
provide.logger.warn('Missing optional data')
expect(warnSpy).toHaveBeenCalledWith(
'[template:warn] 2026-03-29T12:00:00.000Z Missing optional data'
)
vi.useRealTimers()
})
})