log.spec.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. describe('log', () => {
  3. afterAll(() => {
  4. vi.restoreAllMocks()
  5. })
  6. beforeEach(() => {
  7. vi.spyOn(console, 'error').mockReturnValue()
  8. vi.spyOn(console, 'warn').mockReturnValue()
  9. vi.spyOn(console, 'info').mockReturnValue()
  10. vi.spyOn(console, 'log').mockReturnValue()
  11. vi.spyOn(console, 'trace').mockReturnValue()
  12. })
  13. it('logs with default log level', async (context) => {
  14. const { default: log } = await import('../log.ts')
  15. context.skipConsole = true
  16. log.error('error')
  17. log.warn('warn')
  18. log.info('info')
  19. log.debug('debug')
  20. log.trace('trace')
  21. expect(console.error).toHaveBeenCalledTimes(1)
  22. expect(console.warn).toHaveBeenCalledTimes(1)
  23. // This verifies our custom default log level INFO
  24. expect(console.info).toHaveBeenCalledTimes(1)
  25. expect(console.log).toHaveBeenCalledTimes(0)
  26. expect(console.trace).toHaveBeenCalledTimes(0)
  27. log.setLevel('trace')
  28. log.error('error')
  29. log.warn('warn')
  30. log.info('info')
  31. log.debug('debug')
  32. log.trace('trace')
  33. // It seems trace also calls error().
  34. expect(console.error).toHaveBeenCalledTimes(2)
  35. expect(console.warn).toHaveBeenCalledTimes(2)
  36. expect(console.info).toHaveBeenCalledTimes(2)
  37. expect(console.log).toHaveBeenCalledTimes(1)
  38. expect(console.trace).toHaveBeenCalledTimes(1)
  39. })
  40. })