-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ngMocks): allows to suppress console logs #578
- Loading branch information
Showing
8 changed files
with
128 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,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'); | ||
``` |
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
27 changes: 27 additions & 0 deletions
27
libs/ng-mocks/src/lib/mock-helper/mock-helper.ignore-on-console.ts
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,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); | ||
}); | ||
}; |
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
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,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); | ||
}); | ||
}); | ||
}); |