-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
logger.test.ts
47 lines (35 loc) · 1.2 KB
/
logger.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { describe, expect, it, vi } from 'vitest'
import { consola } from 'consola'
import { logger, useLogger } from './logger'
vi.mock('consola', () => {
const logger = {} as any
logger.create = vi.fn(() => ({ ...logger }))
logger.withTag = vi.fn(() => ({ ...logger }))
return { consola: logger }
})
describe('logger', () => {
it('should expose consola', () => {
expect(logger).toBe(consola)
})
})
describe('useLogger', () => {
it('should expose consola when not passing a tag', () => {
expect(useLogger()).toBe(consola)
})
it('should create a new instance when passing a tag', () => {
const logger = vi.mocked(consola)
const instance = useLogger('tag')
expect(instance).toEqual(logger)
expect(instance).not.toBe(logger)
expect(logger.create).toBeCalledWith({})
expect(logger.withTag).toBeCalledWith('tag')
})
it('should create a new instance when passing a tag and options', () => {
const logger = vi.mocked(consola)
const instance = useLogger('tag', { level: 0 })
expect(instance).toEqual(logger)
expect(instance).not.toBe(logger)
expect(logger.create).toBeCalledWith({ level: 0 })
expect(logger.withTag).toBeCalledWith('tag')
})
})