Skip to content

Commit

Permalink
feat(ng-mocks): ngMocks.throwOnConsole
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Jan 10, 2021
1 parent 26c12f6 commit 7b0f2f8
Show file tree
Hide file tree
Showing 18 changed files with 70 additions and 138 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2516,6 +2516,7 @@ describe('MockInstance', () => {
- [`.faster()`](#ngmocksfaster)
- [`.flushTestBed()`](#ngmocksflushtestbed)
- [`.reset()`](#ngmocksreset)
- [`.throwOnConsole()`](#ngmocksthrowonconsole)

#### ngMocks.guts

Expand Down Expand Up @@ -2859,6 +2860,13 @@ ngMocks.stub(instance, {

[to the top](#table-of-contents)

#### ngMocks.throwOnConsole

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

[to the top](#table-of-contents)

### Helper functions

`ng-mocks` provides several functions which help with **detection of mock objects**.
Expand Down
18 changes: 2 additions & 16 deletions lib/mock-builder/mock-builder.promise.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Pipe,
PipeTransform,
} from '@angular/core';
import { ngMocks } from 'ng-mocks';

import { getTestBedInjection } from '../common/core.helpers';

Expand Down Expand Up @@ -47,22 +48,7 @@ class TargetDirective {}
class TargetModule {}

describe('MockBuilderPromise', () => {
// Thanks Ivy, it doesn't throw an error and we have to use injector.
let backupWarn: typeof console.warn;
let backupError: typeof console.error;

beforeAll(() => {
backupWarn = console.warn;
backupError = console.error;
console.error = console.warn = (...args: any[]) => {
throw new Error(args.join(' '));
};
});

afterAll(() => {
console.error = backupError;
console.warn = backupWarn;
});
ngMocks.throwOnConsole();

it('skips dependencies in kept providers', async () => {
await MockBuilder().keep(TargetService, { dependency: true });
Expand Down
20 changes: 20 additions & 0 deletions lib/mock-helper/mock-helper.throw-on-console.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// tslint:disable no-console

// Thanks Ivy, it doesn't throw an error and we have to use injector.
export default (): void => {
let backupWarn: typeof console.warn;
let backupError: typeof console.error;

beforeAll(() => {
backupWarn = console.warn;
backupError = console.error;
console.error = console.warn = (...args: any[]) => {
throw new Error(args.join(' '));
};
});

afterAll(() => {
console.error = backupError;
console.warn = backupWarn;
});
};
9 changes: 9 additions & 0 deletions lib/mock-helper/mock-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import mockHelperInput from './mock-helper.input';
import mockHelperOutput from './mock-helper.output';
import mockHelperReset from './mock-helper.reset';
import mockHelperStub from './mock-helper.stub';
import mockHelperThrowOnConsole from './mock-helper.throw-on-console';

/**
* @see https://github.com/ike18t/ng-mocks#ngmocks
Expand Down Expand Up @@ -266,6 +267,13 @@ export const ngMocks: {
* @see https://github.com/ike18t/ng-mocks#ngmocksstub
*/
stub<I extends object>(instance: I, overrides: Partial<I>): I;

/**
* Thanks Ivy, it doesn't throw an error and we have to use injector.
*
* @see https://github.com/ike18t/ng-mocks#ngmocksthrowonconsole
*/
throwOnConsole(): void;
} = {
autoSpy: mockHelperAutoSpy,
defaultExclude: mockHelperDefaultExclude,
Expand All @@ -285,4 +293,5 @@ export const ngMocks: {
output: mockHelperOutput,
reset: mockHelperReset,
stub: mockHelperStub,
throwOnConsole: mockHelperThrowOnConsole,
};
17 changes: 1 addition & 16 deletions tests/correct-module-exports-11/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,7 @@ class ExternalComponent {}
class TargetModule {}

describe('correct-module-exports-11:proper', () => {
// Thanks Ivy, it doesn't throw an error.
let backupWarn: typeof console.warn;
let backupError: typeof console.error;

beforeAll(() => {
backupWarn = console.warn;
backupError = console.error;
console.error = console.warn = (...args: any[]) => {
throw new Error(args.join(' '));
};
});

afterAll(() => {
console.error = backupError;
console.warn = backupWarn;
});
ngMocks.throwOnConsole();

beforeEach(() =>
TestBed.configureTestingModule({
Expand Down
19 changes: 2 additions & 17 deletions tests/export-all/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
PipeTransform,
} from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { MockBuilder, MockRender } from 'ng-mocks';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

@Pipe({
name: 'target',
Expand Down Expand Up @@ -38,22 +38,7 @@ class TargetComponent {
class TargetModule {}

describe('export-all', () => {
// Thanks Ivy, it doesn't throw an error and we have to use injector.
let backupWarn: typeof console.warn;
let backupError: typeof console.error;

beforeAll(() => {
backupWarn = console.warn;
backupError = console.error;
console.error = console.warn = (...args: any[]) => {
throw new Error(args.join(' '));
};
});

afterAll(() => {
console.error = backupError;
console.warn = backupWarn;
});
ngMocks.throwOnConsole();

// The goal is to get access to declarations of the mock TargetModule
// when TargetComponent is used externally.
Expand Down
19 changes: 2 additions & 17 deletions tests/internal-only-nested/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
// tslint:disable no-console

import { MockBuilder, MockRender } from 'ng-mocks';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

import { InternalComponent, TargetModule } from './fixtures';

describe('InternalOnlyNested:real', () => {
// Thanks Ivy, it doesn't throw an error and we have to use injector.
let backupWarn: typeof console.warn;
let backupError: typeof console.error;

beforeAll(() => {
backupWarn = console.warn;
backupError = console.error;
console.error = console.warn = (...args: any[]) => {
throw new Error(args.join(' '));
};
});

afterAll(() => {
console.error = backupError;
console.warn = backupWarn;
});
ngMocks.throwOnConsole();

beforeEach(() => MockBuilder(TargetModule));

Expand Down
19 changes: 2 additions & 17 deletions tests/internal-only/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
// tslint:disable no-console

import { MockBuilder, MockRender } from 'ng-mocks';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

import { InternalComponent, TargetModule } from './fixtures';

describe('InternalOnly:real', () => {
// Thanks Ivy, it doesn't throw an error and we have to use injector.
let backupWarn: typeof console.warn;
let backupError: typeof console.error;

beforeAll(() => {
backupWarn = console.warn;
backupError = console.error;
console.error = console.warn = (...args: any[]) => {
throw new Error(args.join(' '));
};
});

afterAll(() => {
console.error = backupError;
console.warn = backupWarn;
});
ngMocks.throwOnConsole();

beforeEach(() => MockBuilder(TargetModule));

Expand Down
36 changes: 3 additions & 33 deletions tests/internal-vs-external/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// tslint:disable no-console

import { MockBuilder, MockRender } from 'ng-mocks';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

import {
ExternalComponent,
Expand All @@ -9,22 +9,7 @@ import {
} from './fixtures';

describe('InternalVsExternal:real', () => {
// Thanks Ivy, it doesn't throw an error and we have to use injector.
let backupWarn: typeof console.warn;
let backupError: typeof console.error;

beforeAll(() => {
backupWarn = console.warn;
backupError = console.error;
console.error = console.warn = (...args: any[]) => {
throw new Error(args.join(' '));
};
});

afterAll(() => {
console.error = backupError;
console.warn = backupWarn;
});
ngMocks.throwOnConsole();

beforeEach(() => MockBuilder(TargetModule));

Expand All @@ -43,22 +28,7 @@ describe('InternalVsExternal:real', () => {
});

describe('InternalVsExternal:mock', () => {
// Thanks Ivy, it doesn't throw an error and we have to use injector.
let backupWarn: typeof console.warn;
let backupError: typeof console.error;

beforeAll(() => {
backupWarn = console.warn;
backupError = console.error;
console.error = console.warn = (...args: any[]) => {
throw new Error(args.join(' '));
};
});

afterAll(() => {
console.error = backupError;
console.warn = backupWarn;
});
ngMocks.throwOnConsole();

beforeEach(() => MockBuilder().mock(TargetModule));

Expand Down
21 changes: 2 additions & 19 deletions tests/issue-175/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// tslint:disable no-console

import { NgModule } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { MockBuilder, MockRender } from 'ng-mocks';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

import {
Target1Component,
Expand All @@ -17,22 +15,7 @@ import {
} from './fixtures';

describe('issue-175', () => {
// Thanks Ivy, it doesn't throw an error and we have to use injector.
let backupWarn: typeof console.warn;
let backupError: typeof console.error;

beforeAll(() => {
backupWarn = console.warn;
backupError = console.error;
console.error = console.warn = (...args: any[]) => {
throw new Error(args.join(' '));
};
});

afterAll(() => {
console.error = backupError;
console.warn = backupWarn;
});
ngMocks.throwOnConsole();

describe('module', () => {
beforeEach(() =>
Expand Down
2 changes: 2 additions & 0 deletions tests/ng-mocks-default-exclude/test.override.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Target2Component {
class TargetModule {}

describe('ng-mocks-default-exclude:override', () => {
ngMocks.throwOnConsole();

afterAll(() => {
ngMocks.defaultWipe(Target1Component);
ngMocks.defaultWipe(Target2Component);
Expand Down
2 changes: 2 additions & 0 deletions tests/ng-mocks-default-exclude/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class TargetModule {}
ngMocks.defaultExclude(TargetComponent);

describe('ng-mocks-default-exclude', () => {
ngMocks.throwOnConsole();

describe('MockComponent', () => {
beforeEach(() =>
TestBed.configureTestingModule({
Expand Down
2 changes: 2 additions & 0 deletions tests/ng-mocks-default-keep-modules/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ ngMocks.defaultKeep(Target2Module);
ngMocks.defaultReplace(Target1Component, Fake1Component);

describe('ng-mocks-default-keep-modules', () => {
ngMocks.throwOnConsole();

beforeEach(() => {
return TestBed.configureTestingModule({
imports: [MockModule(Target3Module)],
Expand Down
2 changes: 2 additions & 0 deletions tests/ng-mocks-default-keep/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class TargetModule {}
ngMocks.defaultKeep(TargetComponent);

describe('ng-mocks-default-keep', () => {
ngMocks.throwOnConsole();

describe('MockComponent', () => {
beforeEach(() =>
TestBed.configureTestingModule({
Expand Down
2 changes: 2 additions & 0 deletions tests/ng-mocks-default-replace/test.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class TargetModule {}
ngMocks.defaultReplace(TargetComponent, FakeComponent);

describe('ng-mocks-default-replace:component', () => {
ngMocks.throwOnConsole();

describe('MockComponent', () => {
beforeEach(() =>
TestBed.configureTestingModule({
Expand Down
8 changes: 5 additions & 3 deletions tests/ng-mocks-default-replace/test.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class TargetModule {}
ngMocks.defaultReplace(TargetDirective, FakeDirective);

describe('ng-mocks-default-replace:directive', () => {
ngMocks.throwOnConsole();

describe('MockDirective', () => {
beforeEach(() =>
TestBed.configureTestingModule({
Expand All @@ -42,7 +44,7 @@ describe('ng-mocks-default-replace:directive', () => {

it('works as usual', () => {
const fixture = MockRender('<target></target>');
expect(fixture.nativeElement.innerHTML).toEqual(
expect(fixture.nativeElement.innerHTML).toContain(
'<target></target>',
);
});
Expand Down Expand Up @@ -99,7 +101,7 @@ describe('ng-mocks-default-replace:directive', () => {

it('switches to mock', () => {
const fixture = MockRender('<target></target>');
expect(fixture.nativeElement.innerHTML).toEqual(
expect(fixture.nativeElement.innerHTML).toContain(
'<target></target>',
);
});
Expand Down Expand Up @@ -148,7 +150,7 @@ describe('ng-mocks-default-replace:directive', () => {

it('switches to mock', () => {
const fixture = MockRender('<target></target>');
expect(fixture.nativeElement.innerHTML).toEqual(
expect(fixture.nativeElement.innerHTML).toContain(
'<target></target>',
);
});
Expand Down
2 changes: 2 additions & 0 deletions tests/ng-mocks-default-replace/test.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class TargetModule {}
ngMocks.defaultReplace(TargetPipe, FakePipe);

describe('ng-mocks-default-replace:pipe', () => {
ngMocks.throwOnConsole();

describe('MockPipe', () => {
beforeEach(() =>
TestBed.configureTestingModule({
Expand Down
Loading

0 comments on commit 7b0f2f8

Please sign in to comment.