-
-
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.
fix: resolved an issue that caused errors to not be output properly
- Loading branch information
1 parent
e6695bf
commit 7c14f94
Showing
6 changed files
with
73 additions
and
49 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 |
---|---|---|
@@ -1,85 +1,78 @@ | ||
import { Chance } from 'chance'; | ||
import { LEVEL_CHALK, LogLevel, Logger } from '../index'; | ||
import { stringify } from '../utils/stringify'; | ||
import { LEVEL_CHALK, LogLevel, Logger, MAX_LENGTH } from '../index'; | ||
import { sanitizeMessages } from '../utils/messages'; | ||
|
||
const chance = new Chance(); | ||
|
||
describe('util(Logger)', () => { | ||
let log: jest.SpyInstance<void, [message?: unknown, ...optionalParams: unknown[]], unknown>; | ||
|
||
beforeEach(() => { | ||
log = jest.spyOn(console, 'log'); | ||
log = jest.spyOn(console, 'log').mockReturnValue(null); | ||
|
||
Logger.setLevel(LogLevel.SILLY); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
function validate(level: LogLevel, ...messages: any[]): void { | ||
const chalk = LEVEL_CHALK[level]; | ||
|
||
expect(log).toHaveBeenCalledWith( | ||
chalk(`[${LogLevel[level].toLowerCase()}]:`).padEnd(MAX_LENGTH, ' '), | ||
...sanitizeMessages(messages).map((message) => (message instanceof Error ? message : chalk(message))) | ||
); | ||
} | ||
|
||
describe('func(log)', () => { | ||
it('should output a console log given the log level', () => { | ||
const expectedMessage = chance.word(); | ||
|
||
Logger.log(LogLevel.INFO, expectedMessage); | ||
|
||
expect(log).toHaveBeenCalledWith(LEVEL_CHALK[LogLevel.INFO](`[info]: ${expectedMessage}`)); | ||
validate(LogLevel.INFO, expectedMessage); | ||
}); | ||
|
||
it('should support objects', () => { | ||
const expectedObject = { hello: 'world' }; | ||
|
||
Logger.log(LogLevel.INFO, expectedObject); | ||
|
||
expect(log).toHaveBeenCalledWith(LEVEL_CHALK[LogLevel.INFO](`[info]: ${stringify(expectedObject)}`)); | ||
}); | ||
|
||
it('should not output a console log if the current log level is below the required level', () => { | ||
Logger.setLevel(LogLevel.ERROR); | ||
|
||
Logger.log(LogLevel.INFO, chance.word()); | ||
|
||
expect(log).not.toHaveBeenCalled(); | ||
validate(LogLevel.INFO, expectedObject); | ||
}); | ||
}); | ||
|
||
describe('func(error)', () => { | ||
it('should output a console log with the expected level', () => { | ||
const expectedMessage = chance.word(); | ||
it('should support errors', () => { | ||
const expectedError = new Error('Oops!'); | ||
|
||
Logger.error(expectedMessage); | ||
Logger.log(LogLevel.INFO, expectedError); | ||
|
||
expect(log).toHaveBeenCalledWith(LEVEL_CHALK[LogLevel.ERROR](`[error]: ${expectedMessage}`)); | ||
validate(LogLevel.INFO, expectedError); | ||
}); | ||
}); | ||
|
||
describe('func(warn)', () => { | ||
it('should output a console log with the expected level', () => { | ||
const expectedMessage = chance.word(); | ||
it('should not output a console log if the current log level is below the required level', () => { | ||
Logger.setLevel(LogLevel.ERROR); | ||
|
||
Logger.warn(expectedMessage); | ||
Logger.log(LogLevel.INFO, chance.word()); | ||
|
||
expect(log).toHaveBeenCalledWith(LEVEL_CHALK[LogLevel.WARN](`[warn]: ${expectedMessage}`)); | ||
expect(log).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('func(info)', () => { | ||
it('should output a console log with the expected level', () => { | ||
const expectedMessage = chance.word(); | ||
|
||
Logger.info(expectedMessage); | ||
const levels = Object.values(LogLevel).filter((value) => !isNaN(Number(value))) as LogLevel[]; | ||
|
||
expect(log).toHaveBeenCalledWith(LEVEL_CHALK[LogLevel.INFO](`[info]: ${expectedMessage}`)); | ||
}); | ||
}); | ||
for (const level of levels) { | ||
const name = LogLevel[level].toLowerCase(); | ||
|
||
describe('func(silly)', () => { | ||
it('should output a console log with the expected level', () => { | ||
const expectedMessage = chance.word(); | ||
describe(`func(${name})`, () => { | ||
it('should output a console log with the expected level', () => { | ||
const expectedMessage = chance.word(); | ||
|
||
Logger.silly(expectedMessage); | ||
Logger[name](expectedMessage); | ||
|
||
expect(log).toHaveBeenCalledWith(LEVEL_CHALK[LogLevel.SILLY](`[silly]: ${expectedMessage}`)); | ||
validate(level, expectedMessage); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); |
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,22 @@ | ||
import { sanitizeMessages } from '../messages'; | ||
import { stringify } from '../stringify'; | ||
|
||
describe('util(Messages)', () => { | ||
describe('func(sanitizeMessages)', () => { | ||
it('should leave strings alone', () => { | ||
expect(sanitizeMessages(['hello world'])).toEqual(['hello world']); | ||
}); | ||
|
||
it('should errors alone', () => { | ||
const error = new Error('hello world'); | ||
|
||
expect(sanitizeMessages([error])).toEqual([error]); | ||
}); | ||
|
||
it('should stringify objects', () => { | ||
const expectedObject = { hello: 'world' }; | ||
|
||
expect(sanitizeMessages([expectedObject])).toEqual([stringify(expectedObject)]); | ||
}); | ||
}); | ||
}); |
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,11 @@ | ||
import { stringify } from './stringify'; | ||
|
||
export function sanitizeMessages(messages: any[]): any[] { | ||
return messages.map((message) => { | ||
if (message instanceof Error || typeof message !== 'object') { | ||
return message; | ||
} | ||
|
||
return stringify(message); | ||
}); | ||
} |
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