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
This commit is contained in:
Alan Agius 2021-05-07 14:17:46 +02:00 committed by Alex Rickabaugh
parent 991ea6fc39
commit d2296bc265
2 changed files with 18 additions and 88 deletions

View File

@ -11,8 +11,6 @@ import {Compiler, Component, NgModule} from '@angular/core';
import {fakeAsync, inject, TestBed, tick, waitForAsync} from '@angular/core/testing'; import {fakeAsync, inject, TestBed, tick, waitForAsync} from '@angular/core/testing';
import {ResourceLoaderImpl} from '@angular/platform-browser-dynamic/src/resource_loader/resource_loader_impl'; import {ResourceLoaderImpl} from '@angular/platform-browser-dynamic/src/resource_loader/resource_loader_impl';
// Components for the tests. // Components for the tests.
class FancyService { class FancyService {
value: string = 'real value'; value: string = 'real value';
@ -106,49 +104,14 @@ if (isBrowser) {
}); });
describe('errors', () => { describe('errors', () => {
let originalJasmineIt: any; describe('should fail when an ResourceLoader fails', () => {
it('should fail with an error from a promise', async () => {
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.configureTestingModule({declarations: [BadTemplateUrl]});
TestBed.compileComponents(); await expectAsync(TestBed.compileComponents())
})); .toBeRejectedWith('Failed to load non-existent.html');
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); }, 10000);
}); });
});
describe('TestBed createComponent', function() { describe('TestBed createComponent', function() {
it('should allow an external templateUrl', waitForAsync(() => { it('should allow an external templateUrl', waitForAsync(() => {

View File

@ -836,7 +836,6 @@ const bTok = new InjectionToken<string>('b');
describe('errors', () => { describe('errors', () => {
let originalJasmineIt: (description: string, func: () => void) => jasmine.Spec; let originalJasmineIt: (description: string, func: () => void) => jasmine.Spec;
let originalJasmineBeforeEach: (beforeEachFunction: (done: DoneFn) => void) => void;
const patchJasmineIt = () => { const patchJasmineIt = () => {
let resolve: (result: any) => void; let resolve: (result: any) => void;
@ -845,8 +844,9 @@ const bTok = new InjectionToken<string>('b');
resolve = res; resolve = res;
reject = rej; reject = rej;
}); });
originalJasmineIt = jasmine.getEnv().it; const jasmineEnv = jasmine.getEnv() as any;
jasmine.getEnv().it = (description: string, fn: (done: DoneFn) => void): any => { originalJasmineIt = jasmineEnv.it;
jasmineEnv.it = (description: string, fn: (done: DoneFn) => void): any => {
const done = <DoneFn>(() => resolve(null)); const done = <DoneFn>(() => resolve(null));
done.fail = (err) => reject(err); done.fail = (err) => reject(err);
fn(done); fn(done);
@ -855,26 +855,7 @@ const bTok = new InjectionToken<string>('b');
return promise; return promise;
}; };
const restoreJasmineIt = () => jasmine.getEnv().it = originalJasmineIt; const restoreJasmineIt = () => ((jasmine.getEnv() as any).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 = <DoneFn>(() => resolve(null));
done.fail = (err) => reject(err);
fn(done);
};
return promise;
};
const restoreJasmineBeforeEach = () => jasmine.getEnv().beforeEach =
originalJasmineBeforeEach;
it('should fail when an asynchronous error is thrown', (done) => { it('should fail when an asynchronous error is thrown', (done) => {
const itPromise = patchJasmineIt(); const itPromise = patchJasmineIt();
@ -921,21 +902,16 @@ const bTok = new InjectionToken<string>('b');
it('should report an error for declared components with templateUrl which never call TestBed.compileComponents', it('should report an error for declared components with templateUrl which never call TestBed.compileComponents',
() => { () => {
const itPromise = patchJasmineIt();
@Component({ @Component({
selector: 'comp', 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 { class InlineCompWithUrlTemplate {
} }
expect( expect(withModule(
() =>
it('should fail',
withModule(
{declarations: [InlineCompWithUrlTemplate]}, {declarations: [InlineCompWithUrlTemplate]},
() => TestBed.createComponent(InlineCompWithUrlTemplate)))) () => TestBed.createComponent(InlineCompWithUrlTemplate)))
.toThrowError( .toThrowError(
ivyEnabled ? ivyEnabled ?
`Component 'InlineCompWithUrlTemplate' is not resolved: `Component 'InlineCompWithUrlTemplate' is not resolved:
@ -945,29 +921,20 @@ Did you run and wait for 'resolveComponentResources()'?` :
stringify( stringify(
InlineCompWithUrlTemplate)} which is using a "templateUrl" or "styleUrls", but they were never compiled. ` + InlineCompWithUrlTemplate)} which is using a "templateUrl" or "styleUrls", but they were never compiled. ` +
`Please call "TestBed.compileComponents" before your test.`); `Please call "TestBed.compileComponents" before your test.`);
restoreJasmineIt();
}); });
}); });
modifiedInIvy(`Unknown property error thrown instead of logging a message`) modifiedInIvy(`Unknown property error thrown instead of logging a message`)
.it('should error on unknown bound properties on custom elements by default', () => { .it('should error on unknown bound properties on custom elements by default', () => {
@Component({template: '<some-element [someUnknownProp]="true"></some-element>'}) @Component({template: '<some-element [someUnknownProp]="true"></some-element>'})
class ComponentUsingInvalidProperty { class ComponentUsingInvalidProperty {
} }
const itPromise = patchJasmineIt();
expect( expect(
() => () => withModule(
it('should fail',
withModule(
{declarations: [ComponentUsingInvalidProperty]}, {declarations: [ComponentUsingInvalidProperty]},
() => TestBed.createComponent(ComponentUsingInvalidProperty)))) () => TestBed.createComponent(ComponentUsingInvalidProperty))())
.toThrowError(/Can't bind to 'someUnknownProp'/); .toThrowError(/Can't bind to 'someUnknownProp'/);
restoreJasmineIt();
}); });
onlyInIvy(`Unknown property error logged instead of throwing`) onlyInIvy(`Unknown property error logged instead of throwing`)