diff --git a/README.md b/README.md index 1e487764c5..6972c52940 100644 --- a/README.md +++ b/README.md @@ -2516,6 +2516,7 @@ describe('MockInstance', () => { - [`.faster()`](#ngmocksfaster) - [`.flushTestBed()`](#ngmocksflushtestbed) - [`.reset()`](#ngmocksreset) +- [`.throwOnConsole()`](#ngmocksthrowonconsole) #### ngMocks.guts @@ -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**. diff --git a/lib/mock-builder/mock-builder.promise.spec.ts b/lib/mock-builder/mock-builder.promise.spec.ts index 7eebd2c1e6..2ef5c811f6 100644 --- a/lib/mock-builder/mock-builder.promise.spec.ts +++ b/lib/mock-builder/mock-builder.promise.spec.ts @@ -9,6 +9,7 @@ import { Pipe, PipeTransform, } from '@angular/core'; +import { ngMocks } from 'ng-mocks'; import { getTestBedInjection } from '../common/core.helpers'; @@ -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 }); diff --git a/lib/mock-helper/mock-helper.throw-on-console.ts b/lib/mock-helper/mock-helper.throw-on-console.ts new file mode 100644 index 0000000000..f89c36bb56 --- /dev/null +++ b/lib/mock-helper/mock-helper.throw-on-console.ts @@ -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; + }); +}; diff --git a/lib/mock-helper/mock-helper.ts b/lib/mock-helper/mock-helper.ts index 1cc64073f2..9d3af49ad6 100644 --- a/lib/mock-helper/mock-helper.ts +++ b/lib/mock-helper/mock-helper.ts @@ -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 @@ -266,6 +267,13 @@ export const ngMocks: { * @see https://github.com/ike18t/ng-mocks#ngmocksstub */ stub(instance: I, overrides: Partial): 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, @@ -285,4 +293,5 @@ export const ngMocks: { output: mockHelperOutput, reset: mockHelperReset, stub: mockHelperStub, + throwOnConsole: mockHelperThrowOnConsole, }; diff --git a/tests/correct-module-exports-11/test.spec.ts b/tests/correct-module-exports-11/test.spec.ts index de757ac762..3a25faf088 100644 --- a/tests/correct-module-exports-11/test.spec.ts +++ b/tests/correct-module-exports-11/test.spec.ts @@ -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({ diff --git a/tests/export-all/test.spec.ts b/tests/export-all/test.spec.ts index ceca181c9d..7330f95f4d 100644 --- a/tests/export-all/test.spec.ts +++ b/tests/export-all/test.spec.ts @@ -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', @@ -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. diff --git a/tests/internal-only-nested/test.spec.ts b/tests/internal-only-nested/test.spec.ts index 6c0b0b7c8c..200be78dda 100644 --- a/tests/internal-only-nested/test.spec.ts +++ b/tests/internal-only-nested/test.spec.ts @@ -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)); diff --git a/tests/internal-only/test.spec.ts b/tests/internal-only/test.spec.ts index 854be36c86..3a9d9e2f0a 100644 --- a/tests/internal-only/test.spec.ts +++ b/tests/internal-only/test.spec.ts @@ -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)); diff --git a/tests/internal-vs-external/test.spec.ts b/tests/internal-vs-external/test.spec.ts index 5db51b2a23..dbfe27da47 100644 --- a/tests/internal-vs-external/test.spec.ts +++ b/tests/internal-vs-external/test.spec.ts @@ -1,6 +1,6 @@ // tslint:disable no-console -import { MockBuilder, MockRender } from 'ng-mocks'; +import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; import { ExternalComponent, @@ -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)); @@ -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)); diff --git a/tests/issue-175/test.spec.ts b/tests/issue-175/test.spec.ts index b8370c9176..5ecae43bb7 100644 --- a/tests/issue-175/test.spec.ts +++ b/tests/issue-175/test.spec.ts @@ -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, @@ -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(() => diff --git a/tests/ng-mocks-default-exclude/test.override.spec.ts b/tests/ng-mocks-default-exclude/test.override.spec.ts index edc51935f4..d986dae62e 100644 --- a/tests/ng-mocks-default-exclude/test.override.spec.ts +++ b/tests/ng-mocks-default-exclude/test.override.spec.ts @@ -25,6 +25,8 @@ class Target2Component { class TargetModule {} describe('ng-mocks-default-exclude:override', () => { + ngMocks.throwOnConsole(); + afterAll(() => { ngMocks.defaultWipe(Target1Component); ngMocks.defaultWipe(Target2Component); diff --git a/tests/ng-mocks-default-exclude/test.spec.ts b/tests/ng-mocks-default-exclude/test.spec.ts index e9f2d46290..84b399e92f 100644 --- a/tests/ng-mocks-default-exclude/test.spec.ts +++ b/tests/ng-mocks-default-exclude/test.spec.ts @@ -33,6 +33,8 @@ class TargetModule {} ngMocks.defaultExclude(TargetComponent); describe('ng-mocks-default-exclude', () => { + ngMocks.throwOnConsole(); + describe('MockComponent', () => { beforeEach(() => TestBed.configureTestingModule({ diff --git a/tests/ng-mocks-default-keep-modules/test.spec.ts b/tests/ng-mocks-default-keep-modules/test.spec.ts index 63ecacb95b..7aba476dbb 100644 --- a/tests/ng-mocks-default-keep-modules/test.spec.ts +++ b/tests/ng-mocks-default-keep-modules/test.spec.ts @@ -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)], diff --git a/tests/ng-mocks-default-keep/test.spec.ts b/tests/ng-mocks-default-keep/test.spec.ts index ed7e2bc700..0032d87a37 100644 --- a/tests/ng-mocks-default-keep/test.spec.ts +++ b/tests/ng-mocks-default-keep/test.spec.ts @@ -33,6 +33,8 @@ class TargetModule {} ngMocks.defaultKeep(TargetComponent); describe('ng-mocks-default-keep', () => { + ngMocks.throwOnConsole(); + describe('MockComponent', () => { beforeEach(() => TestBed.configureTestingModule({ diff --git a/tests/ng-mocks-default-replace/test.component.spec.ts b/tests/ng-mocks-default-replace/test.component.spec.ts index 54539f7b96..b688b0a035 100644 --- a/tests/ng-mocks-default-replace/test.component.spec.ts +++ b/tests/ng-mocks-default-replace/test.component.spec.ts @@ -33,6 +33,8 @@ class TargetModule {} ngMocks.defaultReplace(TargetComponent, FakeComponent); describe('ng-mocks-default-replace:component', () => { + ngMocks.throwOnConsole(); + describe('MockComponent', () => { beforeEach(() => TestBed.configureTestingModule({ diff --git a/tests/ng-mocks-default-replace/test.directive.spec.ts b/tests/ng-mocks-default-replace/test.directive.spec.ts index 51819d449d..e1517a4b64 100644 --- a/tests/ng-mocks-default-replace/test.directive.spec.ts +++ b/tests/ng-mocks-default-replace/test.directive.spec.ts @@ -33,6 +33,8 @@ class TargetModule {} ngMocks.defaultReplace(TargetDirective, FakeDirective); describe('ng-mocks-default-replace:directive', () => { + ngMocks.throwOnConsole(); + describe('MockDirective', () => { beforeEach(() => TestBed.configureTestingModule({ @@ -42,7 +44,7 @@ describe('ng-mocks-default-replace:directive', () => { it('works as usual', () => { const fixture = MockRender(''); - expect(fixture.nativeElement.innerHTML).toEqual( + expect(fixture.nativeElement.innerHTML).toContain( '', ); }); @@ -99,7 +101,7 @@ describe('ng-mocks-default-replace:directive', () => { it('switches to mock', () => { const fixture = MockRender(''); - expect(fixture.nativeElement.innerHTML).toEqual( + expect(fixture.nativeElement.innerHTML).toContain( '', ); }); @@ -148,7 +150,7 @@ describe('ng-mocks-default-replace:directive', () => { it('switches to mock', () => { const fixture = MockRender(''); - expect(fixture.nativeElement.innerHTML).toEqual( + expect(fixture.nativeElement.innerHTML).toContain( '', ); }); diff --git a/tests/ng-mocks-default-replace/test.pipe.spec.ts b/tests/ng-mocks-default-replace/test.pipe.spec.ts index bf9a1a7883..3a057fecbb 100644 --- a/tests/ng-mocks-default-replace/test.pipe.spec.ts +++ b/tests/ng-mocks-default-replace/test.pipe.spec.ts @@ -35,6 +35,8 @@ class TargetModule {} ngMocks.defaultReplace(TargetPipe, FakePipe); describe('ng-mocks-default-replace:pipe', () => { + ngMocks.throwOnConsole(); + describe('MockPipe', () => { beforeEach(() => TestBed.configureTestingModule({ diff --git a/tests/ng-mocks-default-wipe/test.spec.ts b/tests/ng-mocks-default-wipe/test.spec.ts index dafadab752..ee59eed6bf 100644 --- a/tests/ng-mocks-default-wipe/test.spec.ts +++ b/tests/ng-mocks-default-wipe/test.spec.ts @@ -45,6 +45,8 @@ ngMocks.defaultReplace(TargetComponent, FakeComponent); ngMocks.defaultWipe(TargetComponent); describe('ng-mocks-default-replace', () => { + ngMocks.throwOnConsole(); + describe('MockComponent', () => { beforeEach(() => TestBed.configureTestingModule({