Skip to content

Commit

Permalink
Adds new helpers jestLogMute and jestLogUnmute
Browse files Browse the repository at this point in the history
  • Loading branch information
OlehDutchenko committed Jun 12, 2021
1 parent 3256f52 commit c51c760
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 2 deletions.
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const someFunction = (y: boolean, z: number, w: number): number | null =>
y ? z + w : null;

// some-function.spec.ts
import someFunction from 'some-function.ts';
import { someFunction } from 'some-function';
import { jestFunctionSignatureTest } from '@wezom/toolkit-jest';

describe('Function signature should match specification', () => {
Expand All @@ -79,6 +79,38 @@ describe('Function signature should match specification', () => {

[comment]: <> (AUTODOC-TOOL-END)

### jestLogMute() and jestLogUnmute()

[comment]: <> (AUTODOC-TOOL-START::log-mute#jestLogMute)

Mute default `console.log` logging

_Returns:_ `void`

_Examples:_

```ts
// 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();
});
```

[comment]: <> (AUTODOC-TOOL-END)

---

[▲ Go Top](#) | [▲ Table of Content](#table-of-content)
Expand Down
48 changes: 48 additions & 0 deletions src/__spec__/log-mute.spec.ts
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();
});
});
2 changes: 1 addition & 1 deletion src/function-signature-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* export const someFunction = (y: boolean, z: number, w: number):number|null => y ? z + w : null;
*
* // some-function.spec.ts
* import someFunction from 'some-function.ts'
* import { someFunction } from 'some-function'
* import { jestFunctionSignatureTest } from '@wezom/toolkit-jest'
*
* describe('Function signature should match specification', () => {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
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';
36 changes: 36 additions & 0 deletions src/log-mute.ts
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;
}
}

0 comments on commit c51c760

Please sign in to comment.