From d2296bc2659ea70d16c936103fcc436e20ee2801 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 7 May 2021 14:17:46 +0200 Subject: [PATCH] test: refactor test to work with latest @types/jasmine (#41956) In some cases we are using private APIs. This change adds casting were needed to make the build successful. PR Close #41956 --- .../test/testing_public_browser_spec.ts | 51 +++-------------- .../test/testing_public_spec.ts | 55 ++++--------------- 2 files changed, 18 insertions(+), 88 deletions(-) diff --git a/packages/platform-browser-dynamic/test/testing_public_browser_spec.ts b/packages/platform-browser-dynamic/test/testing_public_browser_spec.ts index d8b51bea02..5639ec7393 100644 --- a/packages/platform-browser-dynamic/test/testing_public_browser_spec.ts +++ b/packages/platform-browser-dynamic/test/testing_public_browser_spec.ts @@ -11,8 +11,6 @@ import {Compiler, Component, NgModule} from '@angular/core'; import {fakeAsync, inject, TestBed, tick, waitForAsync} from '@angular/core/testing'; import {ResourceLoaderImpl} from '@angular/platform-browser-dynamic/src/resource_loader/resource_loader_impl'; - - // Components for the tests. class FancyService { value: string = 'real value'; @@ -106,48 +104,13 @@ if (isBrowser) { }); describe('errors', () => { - let originalJasmineIt: any; - - const patchJasmineIt = () => { - let resolve: (result: any) => void; - let reject: (error: any) => void; - const promise = new Promise((res, rej) => { - resolve = res; - reject = rej; - }); - originalJasmineIt = jasmine.getEnv().it; - jasmine.getEnv().it = (description: string, fn: (done: DoneFn) => void): any => { - const done = (() => resolve(null)) as DoneFn; - done.fail = reject; - fn(done); - return null; - }; - return promise; - }; - - const restoreJasmineIt = () => { - jasmine.getEnv().it = originalJasmineIt; - }; - - it('should fail when an ResourceLoader fails', done => { - const itPromise = patchJasmineIt(); - - it('should fail with an error from a promise', waitForAsync(() => { - TestBed.configureTestingModule({declarations: [BadTemplateUrl]}); - TestBed.compileComponents(); - })); - - itPromise.then( - () => { - done.fail('Expected test to fail, but it did not'); - }, - (err: any) => { - expect(err.message) - .toEqual('Uncaught (in promise): Failed to load non-existent.html'); - done(); - }); - restoreJasmineIt(); - }, 10000); + describe('should fail when an ResourceLoader fails', () => { + it('should fail with an error from a promise', async () => { + TestBed.configureTestingModule({declarations: [BadTemplateUrl]}); + await expectAsync(TestBed.compileComponents()) + .toBeRejectedWith('Failed to load non-existent.html'); + }, 10000); + }); }); describe('TestBed createComponent', function() { diff --git a/packages/platform-browser/test/testing_public_spec.ts b/packages/platform-browser/test/testing_public_spec.ts index bb6fa0e575..f1e5c8430f 100644 --- a/packages/platform-browser/test/testing_public_spec.ts +++ b/packages/platform-browser/test/testing_public_spec.ts @@ -836,7 +836,6 @@ const bTok = new InjectionToken('b'); describe('errors', () => { let originalJasmineIt: (description: string, func: () => void) => jasmine.Spec; - let originalJasmineBeforeEach: (beforeEachFunction: (done: DoneFn) => void) => void; const patchJasmineIt = () => { let resolve: (result: any) => void; @@ -845,8 +844,9 @@ const bTok = new InjectionToken('b'); resolve = res; reject = rej; }); - originalJasmineIt = jasmine.getEnv().it; - jasmine.getEnv().it = (description: string, fn: (done: DoneFn) => void): any => { + const jasmineEnv = jasmine.getEnv() as any; + originalJasmineIt = jasmineEnv.it; + jasmineEnv.it = (description: string, fn: (done: DoneFn) => void): any => { const done = (() => resolve(null)); done.fail = (err) => reject(err); fn(done); @@ -855,26 +855,7 @@ const bTok = new InjectionToken('b'); return promise; }; - const restoreJasmineIt = () => jasmine.getEnv().it = originalJasmineIt; - - const patchJasmineBeforeEach = () => { - let resolve: (result: any) => void; - let reject: (error: any) => void; - const promise = new Promise((res, rej) => { - resolve = res; - reject = rej; - }); - originalJasmineBeforeEach = jasmine.getEnv().beforeEach; - jasmine.getEnv().beforeEach = (fn: (done: DoneFn) => void) => { - const done = (() => resolve(null)); - done.fail = (err) => reject(err); - fn(done); - }; - return promise; - }; - - const restoreJasmineBeforeEach = () => jasmine.getEnv().beforeEach = - originalJasmineBeforeEach; + const restoreJasmineIt = () => ((jasmine.getEnv() as any).it = originalJasmineIt); it('should fail when an asynchronous error is thrown', (done) => { const itPromise = patchJasmineIt(); @@ -921,21 +902,16 @@ const bTok = new InjectionToken('b'); it('should report an error for declared components with templateUrl which never call TestBed.compileComponents', () => { - const itPromise = patchJasmineIt(); - @Component({ selector: 'comp', - templateUrl: '/base/angular/packages/platform-browser/test/static_assets/test.html' + templateUrl: '/base/angular/packages/platform-browser/test/static_assets/test.html', }) class InlineCompWithUrlTemplate { } - expect( - () => - it('should fail', - withModule( - {declarations: [InlineCompWithUrlTemplate]}, - () => TestBed.createComponent(InlineCompWithUrlTemplate)))) + expect(withModule( + {declarations: [InlineCompWithUrlTemplate]}, + () => TestBed.createComponent(InlineCompWithUrlTemplate))) .toThrowError( ivyEnabled ? `Component 'InlineCompWithUrlTemplate' is not resolved: @@ -945,29 +921,20 @@ Did you run and wait for 'resolveComponentResources()'?` : stringify( InlineCompWithUrlTemplate)} which is using a "templateUrl" or "styleUrls", but they were never compiled. ` + `Please call "TestBed.compileComponents" before your test.`); - - restoreJasmineIt(); }); }); - modifiedInIvy(`Unknown property error thrown instead of logging a message`) .it('should error on unknown bound properties on custom elements by default', () => { @Component({template: ''}) class ComponentUsingInvalidProperty { } - const itPromise = patchJasmineIt(); - expect( - () => - it('should fail', - withModule( - {declarations: [ComponentUsingInvalidProperty]}, - () => TestBed.createComponent(ComponentUsingInvalidProperty)))) + () => withModule( + {declarations: [ComponentUsingInvalidProperty]}, + () => TestBed.createComponent(ComponentUsingInvalidProperty))()) .toThrowError(/Can't bind to 'someUnknownProp'/); - - restoreJasmineIt(); }); onlyInIvy(`Unknown property error logged instead of throwing`)