-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for logger and some adjustments for logic
- Loading branch information
Showing
6 changed files
with
95 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { | ||
AbstractLogger, | ||
DefaultLogger, | ||
LOG_LEVEL, | ||
mainLogger, | ||
preloadLogger, | ||
initialiseMain, | ||
initialisePreload, | ||
} from '../utils/logger' | ||
|
||
import type { LogLevel } from '../utils/logger' | ||
|
||
describe.each([ | ||
['main', initialiseMain], | ||
['preload', initialisePreload], | ||
])('Logger (%s)', (procName: string, initialise) => { | ||
const getLogger = (procName) => { | ||
switch (procName) { | ||
case 'main': | ||
return mainLogger | ||
|
||
case 'preload': | ||
return preloadLogger | ||
} | ||
} | ||
describe('DefaultLogger', () => { | ||
const errorSpy = vi.spyOn(console, 'error') | ||
const debugSpy = vi.spyOn(console, 'debug') | ||
const logSpy = vi.spyOn(console, 'log') | ||
|
||
beforeEach(() => { | ||
errorSpy.mockReset() | ||
debugSpy.mockReset() | ||
logSpy.mockReset() | ||
}) | ||
|
||
it('disable', () => { | ||
initialise({ logger: {} }) | ||
getLogger(procName).info('TEST') | ||
expect(logSpy).not.toHaveBeenCalled() | ||
expect(debugSpy).not.toHaveBeenCalled() | ||
}) | ||
|
||
it.each([ | ||
['info', logSpy], | ||
['warn', logSpy], | ||
['error', errorSpy], | ||
['verbose', debugSpy], | ||
['debug', debugSpy], | ||
['silly', debugSpy], | ||
])('Assert output the log at each log lebel (%s)', (logLevel, spy) => { | ||
initialise({ logger: { [procName]: new DefaultLogger() } }) | ||
getLogger(procName)[logLevel]('TEST') | ||
expect(spy).toHaveBeenCalled() | ||
}) | ||
}) | ||
|
||
describe('Assert output the log at each log level', () => { | ||
const logMock = vi.fn() | ||
class Logger extends AbstractLogger { | ||
protected writeLog(level: LogLevel, message: string): void { | ||
logMock(level, message) | ||
} | ||
} | ||
beforeEach(() => { | ||
logMock.mockReset() | ||
initialise({ logger: { [procName]: new Logger() } }) | ||
}) | ||
|
||
it.each([...Object.keys(LOG_LEVEL)])('logger test (%s)', (logLevel) => { | ||
getLogger(procName)[logLevel]('TEST') | ||
expect(logMock).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters