-
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.
Adds new helpers
jestLogMute
and jestLogUnmute
- Loading branch information
1 parent
3256f52
commit c51c760
Showing
5 changed files
with
119 additions
and
2 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,48 @@ | ||
import { jestLogMute, jestLogUnmute } from '../index'; | ||
|
||
describe('Check specs', () => { | ||
const testMsg = 'Lorem ipsum!'; | ||
const testFn = (): void => console.log(testMsg); | ||
let defaultLog: (...args: any[]) => void; | ||
const setSpyAndGetList = (): any[][] => { | ||
const spyList: any[][] = []; | ||
const spyLog = (...args: any[]): void => { | ||
spyList.push(args); | ||
}; | ||
defaultLog = console.log; | ||
console.log = spyLog; | ||
return spyList; | ||
}; | ||
|
||
const removeSpy = (): void => { | ||
if (defaultLog !== undefined) { | ||
console.log = defaultLog; | ||
} | ||
}; | ||
|
||
test('Should mute and unmute `console.log`', () => { | ||
// try spy | ||
const spyList1 = setSpyAndGetList(); | ||
testFn(); | ||
expect(spyList1).toEqual([[testMsg]]); | ||
removeSpy(); | ||
|
||
// try mute and unmute | ||
const spyList2 = setSpyAndGetList(); | ||
jestLogMute(); | ||
testFn(); | ||
expect(spyList2).toEqual([]); | ||
jestLogUnmute(); | ||
testFn(); | ||
expect(spyList2).toEqual([[testMsg]]); | ||
removeSpy(); | ||
}); | ||
|
||
test('Should do nothing if call only unmute', () => { | ||
const spyList1 = setSpyAndGetList(); | ||
jestLogUnmute(); | ||
testFn(); | ||
expect(spyList1).toEqual([[testMsg]]); | ||
removeSpy(); | ||
}); | ||
}); |
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 +1,2 @@ | ||
export { default as jestFunctionSignatureTest } from './function-signature-test'; | ||
export { jestLogMute, jestLogUnmute } from './log-mute'; |
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,36 @@ | ||
let log: undefined | ((message?: any, ...optionalParams: any[]) => void); | ||
|
||
/** | ||
* Mute default `console.log` logging | ||
* @see {jestLogUnmute} | ||
* @example | ||
* // some-function.ts | ||
* export const someFunction = (x: number, y: number):number => { | ||
* console.log('SOME LOG MESSAGE'); | ||
* return x + y; | ||
* }; | ||
* | ||
* // some-function.spec.ts | ||
* import { someFunction } from 'some-function' | ||
* import { jestLogMute, jestLogUnmute } from '@wezom/toolkit-jest' | ||
* | ||
* describe('Should be silent test', () => { | ||
* jestLogMute(); | ||
* test('silent testing of the `someFunction`', () => { | ||
* expect(someFunction(1, 2)).toBe(3); | ||
* }); | ||
* jestLogUnmute(); | ||
* }); | ||
*/ | ||
export function jestLogMute(): void { | ||
log = console.log; | ||
console.log = (): void => undefined; | ||
} | ||
|
||
/** @see {jestLogMute} */ | ||
export function jestLogUnmute(): void { | ||
if (log !== undefined) { | ||
console.log = log; | ||
log = undefined; | ||
} | ||
} |