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 {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() {

View File

@ -836,7 +836,6 @@ const bTok = new InjectionToken<string>('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<string>('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 = <DoneFn>(() => resolve(null));
done.fail = (err) => reject(err);
fn(done);
@ -855,26 +855,7 @@ const bTok = new InjectionToken<string>('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 = <DoneFn>(() => 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<string>('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: '<some-element [someUnknownProp]="true"></some-element>'})
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`)