-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39f0d88
commit 9f9959c
Showing
1 changed file
with
143 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/** | ||
* © 2014 Liferay, Inc. <https://liferay.com> | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
*/ | ||
|
||
import Logger from '../logger'; | ||
|
||
/* eslint-disable no-console */ | ||
|
||
const savedError = console.error; | ||
const savedWarn = console.warn; | ||
const savedInfo = console.info; | ||
const savedDebug = console.debug; | ||
const savedLog = console.log; | ||
|
||
describe('Logger', function() { | ||
beforeEach(() => { | ||
console.error = jest.fn(); | ||
console.warn = jest.fn(); | ||
console.info = jest.fn(); | ||
console.debug = jest.fn(); | ||
console.log = jest.fn(); | ||
}); | ||
|
||
afterEach(() => { | ||
console.error = savedError; | ||
console.warn = savedWarn; | ||
console.info = savedInfo; | ||
console.debug = savedDebug; | ||
console.log = savedLog; | ||
}); | ||
|
||
it('off does not write any message', () => { | ||
const log = new Logger({logLevel: 'off'}); | ||
|
||
log.error('an error'); | ||
log.warn('a warn'); | ||
log.info('an info'); | ||
log.debug('a debug'); | ||
|
||
expect(console.error.mock.calls).toHaveLength(0); | ||
expect(console.warn.mock.calls).toHaveLength(0); | ||
expect(console.info.mock.calls).toHaveLength(0); | ||
expect(console.debug.mock.calls).toHaveLength(0); | ||
}); | ||
|
||
it('error writes error messages only', () => { | ||
const log = new Logger({logLevel: 'error'}); | ||
|
||
log.error('an error'); | ||
log.warn('a warn'); | ||
log.info('an info'); | ||
log.debug('a debug'); | ||
|
||
expect(console.error.mock.calls).toHaveLength(1); | ||
expect(console.error.mock.calls[0]).toEqual([ | ||
'liferay-amd-loader |', | ||
'an error', | ||
]); | ||
expect(console.warn.mock.calls).toHaveLength(0); | ||
expect(console.info.mock.calls).toHaveLength(0); | ||
expect(console.debug.mock.calls).toHaveLength(0); | ||
}); | ||
|
||
it('warn writes error and warn messages only', () => { | ||
const log = new Logger({logLevel: 'warn'}); | ||
|
||
log.error('an error'); | ||
log.warn('a warn'); | ||
log.info('an info'); | ||
log.debug('a debug'); | ||
|
||
expect(console.error.mock.calls).toHaveLength(1); | ||
expect(console.error.mock.calls[0]).toEqual([ | ||
'liferay-amd-loader |', | ||
'an error', | ||
]); | ||
expect(console.warn.mock.calls).toHaveLength(1); | ||
expect(console.warn.mock.calls[0]).toEqual([ | ||
'liferay-amd-loader |', | ||
'a warn', | ||
]); | ||
expect(console.info.mock.calls).toHaveLength(0); | ||
expect(console.debug.mock.calls).toHaveLength(0); | ||
}); | ||
|
||
it('info writes error, warn and info messages only', () => { | ||
const log = new Logger({logLevel: 'info'}); | ||
|
||
log.error('an error'); | ||
log.warn('a warn'); | ||
log.info('an info'); | ||
log.debug('a debug'); | ||
|
||
expect(console.error.mock.calls).toHaveLength(1); | ||
expect(console.error.mock.calls[0]).toEqual([ | ||
'liferay-amd-loader |', | ||
'an error', | ||
]); | ||
expect(console.warn.mock.calls).toHaveLength(1); | ||
expect(console.warn.mock.calls[0]).toEqual([ | ||
'liferay-amd-loader |', | ||
'a warn', | ||
]); | ||
expect(console.info.mock.calls).toHaveLength(1); | ||
expect(console.info.mock.calls[0]).toEqual([ | ||
'liferay-amd-loader |', | ||
'an info', | ||
]); | ||
expect(console.debug.mock.calls).toHaveLength(0); | ||
}); | ||
|
||
it('debug writes all messages', () => { | ||
const log = new Logger({logLevel: 'debug'}); | ||
|
||
log.error('an error'); | ||
log.warn('a warn'); | ||
log.info('an info'); | ||
log.debug('a debug'); | ||
|
||
expect(console.error.mock.calls).toHaveLength(1); | ||
expect(console.error.mock.calls[0]).toEqual([ | ||
'liferay-amd-loader |', | ||
'an error', | ||
]); | ||
expect(console.warn.mock.calls).toHaveLength(1); | ||
expect(console.warn.mock.calls[0]).toEqual([ | ||
'liferay-amd-loader |', | ||
'a warn', | ||
]); | ||
expect(console.info.mock.calls).toHaveLength(1); | ||
expect(console.info.mock.calls[0]).toEqual([ | ||
'liferay-amd-loader |', | ||
'an info', | ||
]); | ||
expect(console.debug.mock.calls).toHaveLength(1); | ||
expect(console.debug.mock.calls[0]).toEqual([ | ||
'liferay-amd-loader |', | ||
'a debug', | ||
]); | ||
}); | ||
}); |