|
1 | | -import { describe, expect, it } from 'vitest' |
| 1 | +import { describe, expect, expectTypeOf, it, vi } from 'vitest' |
2 | 2 | import Base from '../src/base.js' |
3 | | -import { loggerDecorator } from '../src/logger.js' |
| 3 | +import { |
| 4 | + getLogLevel, |
| 5 | + loggerDecorator, |
| 6 | + LogLevel, |
| 7 | + LogLevelStrings |
| 8 | +} from '../src/logger.js' |
4 | 9 |
|
5 | 10 | describe('logger', () => { |
6 | 11 | it('has a logger property', () => { |
@@ -39,4 +44,56 @@ describe('logger', () => { |
39 | 44 | }) |
40 | 45 | }) |
41 | 46 | }) |
| 47 | + |
| 48 | + describe('logLevels', () => { |
| 49 | + it.each(['verbose', 'debug', 'info', 'warn', 'error'])( |
| 50 | + `has a %s method`, |
| 51 | + (level) => { |
| 52 | + class Test extends Base {} |
| 53 | + loggerDecorator()(Test) |
| 54 | + const instance = new Test() |
| 55 | + expect(instance.logger).toHaveProperty(level) |
| 56 | + expect(instance.logger[level]).toBeInstanceOf(Function) |
| 57 | + } |
| 58 | + ) |
| 59 | + |
| 60 | + it('should log a message at the specified level', () => { |
| 61 | + class Test extends Base { |
| 62 | + hello() { |
| 63 | + this.logger.debug('hello') |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + loggerDecorator('test', { level: 'debug' })(Test) |
| 68 | + const testInstance = new Test() |
| 69 | + const consoleSpy = vi.spyOn(console, 'debug') |
| 70 | + testInstance.hello() |
| 71 | + expect(consoleSpy).toHaveBeenCalledOnce() |
| 72 | + }) |
| 73 | + |
| 74 | + it('should not log a message at a lower level', () => { |
| 75 | + class Test extends Base { |
| 76 | + hello() { |
| 77 | + this.logger.debug('hello') |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + loggerDecorator('test', { level: 'error' })(Test) |
| 82 | + const testInstance = new Test() |
| 83 | + const consoleSpy = vi.spyOn(console, 'debug') |
| 84 | + testInstance.hello() |
| 85 | + expect(consoleSpy).not.toHaveBeenCalled() |
| 86 | + }) |
| 87 | + }) |
| 88 | +}) |
| 89 | + |
| 90 | +describe('getLogLevel', () => { |
| 91 | + it.each(['verbose', 'debug', 'info', 'warn', 'error'])( |
| 92 | + 'returns the correct log level for %s', |
| 93 | + (level) => { |
| 94 | + expect(getLogLevel(level as LogLevelStrings)).toBe( |
| 95 | + LogLevel[level.toUpperCase() as keyof typeof LogLevel] |
| 96 | + ) |
| 97 | + } |
| 98 | + ) |
42 | 99 | }) |
0 commit comments