Skip to content

Commit

Permalink
feat(ngMocks): allows to suppress console logs #578
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Jun 30, 2021
1 parent d3680e6 commit ee1c6bb
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/articles/api/ngMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ access desired elements and instances in fixtures.

- [`guts()`](ngMocks/guts.md)
- [`faster()`](ngMocks/faster.md)
- [`ignoreOnConsole()`](ngMocks/ignoreOnConsole.md)
- [`throwOnConsole()`](ngMocks/throwOnConsole.md)
- [`formatHtml()`](ngMocks/formatHtml.md)
- [`formatText()`](ngMocks/formatText.md)
Expand Down
14 changes: 14 additions & 0 deletions docs/articles/api/ngMocks/ignoreOnConsole.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: ngMocks.ignoreOnConsole
description: A way to suppress console logs in Angular unit tests
---

`ngMocks.ignoreOnConsole` suppresses `console.log` with a spy (if [auto spy](../../extra/auto-spy.md) is being used).

`ngMocks.ignoreOnConsole` suppresses the functions for the current test suite in `beforeAll` and restores in `afterAll`.

Also, any other methods can be stubbed:

```ts
ngMocks.ignoreOnConsole('log', 'err', 'warn');
```
12 changes: 10 additions & 2 deletions docs/articles/api/ngMocks/throwOnConsole.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@ title: ngMocks.throwOnConsole
description: Documentation about ngMocks.throwOnConsole from ng-mocks library
---

`ngMocks.throwOnConsole` stubs `console.warn` and `console.error` to throw an error instead of printing into the console.
It is useful in `Ivy` enabled mode, because then some errors are printed instead of being thrown.
`ngMocks.throwOnConsole` stubs `console.warn` and `console.error` to throw an error instead of printing it into the console.
It is useful in `Ivy` enabled mode, because some errors are printed via `console` instead of being thrown.

`ngMocks.throwOnConsole` stubs the functions for the current test suite in `beforeAll` and restores in `afterAll`.

Also, any other methods can be stubbed:

```ts
ngMocks.throwOnConsole('log');
```
1 change: 1 addition & 0 deletions docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ module.exports = {
'api/ngMocks/stubMember',
'api/ngMocks/guts',
'api/ngMocks/faster',
'api/ngMocks/ignoreOnConsole',
'api/ngMocks/throwOnConsole',
'api/ngMocks/formatHtml',
'api/ngMocks/formatText',
Expand Down
27 changes: 27 additions & 0 deletions libs/ng-mocks/src/lib/mock-helper/mock-helper.ignore-on-console.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// tslint:disable no-console

import helperMockService from '../mock-service/helper.mock-service';

const factory = (propName: string) => helperMockService.mockFunction(`console.${propName}`);

// Thanks Ivy, it does not throw an error and we have to use injector.
export default (...methods: Array<keyof typeof console>): void => {
const backup: Array<[keyof typeof console, any]> = [];

beforeAll(() => {
if (methods.indexOf('log') === -1) {
methods.push('log');
}
for (const method of methods) {
backup.push([method, console[method]]);
console[method] = factory(method);
}
});

afterAll(() => {
for (const [method, implementation] of backup) {
console[method] = implementation;
}
backup.splice(0, backup.length);
});
};
2 changes: 2 additions & 0 deletions libs/ng-mocks/src/lib/mock-helper/mock-helper.object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import mockHelperGlobalMock from './mock-helper.global-mock';
import mockHelperGlobalReplace from './mock-helper.global-replace';
import mockHelperGlobalWipe from './mock-helper.global-wipe';
import mockHelperGuts from './mock-helper.guts';
import mockHelperIgnoreOnConsole from './mock-helper.ignore-on-console';
import mockHelperInput from './mock-helper.input';
import mockHelperOutput from './mock-helper.output';
import mockHelperReset from './mock-helper.reset';
Expand Down Expand Up @@ -78,6 +79,7 @@ export default {
globalWipe: mockHelperGlobalWipe,
guts: mockHelperGuts,
hide: mockHelperHide,
ignoreOnConsole: mockHelperIgnoreOnConsole,
input: mockHelperInput,
output: mockHelperOutput,
render: mockHelperRender,
Expand Down
7 changes: 7 additions & 0 deletions libs/ng-mocks/src/lib/mock-helper/mock-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,13 @@ export const ngMocks: {
*/
hide(instance: object, directive: object): void;

/**
* it suppresses any log calls, other methods can be suppressed too.
*
* @see https://ng-mocks.sudo.eu/api/ngMocks/ignoreOnConsole
*/
ignoreOnConsole(...args: Array<keyof typeof console>): void;

/**
* @see https://ng-mocks.sudo.eu/api/ngMocks/input
*/
Expand Down
66 changes: 66 additions & 0 deletions tests/ng-mocks-ignore-on-console/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// tslint:disable no-console

import { ngMocks } from 'ng-mocks';

describe('ng-mocks-ignore-on-console', () => {
describe('default', () => {
let original: any;
beforeAll(() => (original = console.log));

describe('override', () => {
ngMocks.ignoreOnConsole();

it('replaces the log with a spy', () => {
expect(console.log).not.toBe(original);
console.log('test');
expect(console.log).toHaveBeenCalledWith('test');
});
});

it('restores the original console.log', () => {
expect(console.log).toBe(original);
});
});

describe('custom', () => {
let original: any;
beforeAll(() => (original = console.warn));

describe('override', () => {
ngMocks.ignoreOnConsole('warn');

it('replaces the log with a spy', () => {
expect(console.warn).not.toBe(original);
console.warn('test');
expect(console.log).not.toHaveBeenCalled();
expect(console.warn).toHaveBeenCalledWith('test');
});
});

it('restores the original console.log', () => {
expect(console.warn).toBe(original);
});
});

describe('restore', () => {
let originalLog: any;
beforeAll(() => (originalLog = console.log));
let originalWarn: any;
beforeAll(() => (originalWarn = console.warn));

describe('override', () => {
ngMocks.ignoreOnConsole('log', 'warn');
ngMocks.throwOnConsole('log', 'warn');

it('replaces the log with a spy', () => {
expect(console.log).not.toBe(originalLog);
expect(console.warn).not.toBe(originalWarn);
});
});

it('restores the original console.log', () => {
expect(console.log).toBe(originalLog);
expect(console.warn).toBe(originalWarn);
});
});
});

0 comments on commit ee1c6bb

Please sign in to comment.