fix: Update test code to type-check under TS 2.5 (#20175)

PR Close #20175
This commit is contained in:
Alex Eagle 2017-11-15 08:43:35 -08:00 committed by Miško Hevery
parent c2a24b4241
commit 5ec1717c58
19 changed files with 55 additions and 48 deletions

View File

@ -3,7 +3,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository( git_repository(
name = "build_bazel_rules_nodejs", name = "build_bazel_rules_nodejs",
remote = "https://github.com/bazelbuild/rules_nodejs.git", remote = "https://github.com/bazelbuild/rules_nodejs.git",
tag = "0.0.2", tag = "0.2.1",
) )
load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories") load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories")

View File

@ -9,7 +9,7 @@
"typescript": ">=2.4.2 <2.6" "typescript": ">=2.4.2 <2.6"
}, },
"dependencies": { "dependencies": {
"@bazel/typescript": "0.2.x", "@bazel/typescript": "0.3.x",
"@types/node": "6.0.84" "@types/node": "6.0.84"
}, },
"repository": { "repository": {

View File

@ -17,7 +17,7 @@ export function main() {
ids.map(id => ({provide: id, useValue: new MockMetric(id)})), ids.map(id => ({provide: id, useValue: new MockMetric(id)})),
MultiMetric.provideWith(ids) MultiMetric.provideWith(ids)
]) ])
.get(MultiMetric); .get<MultiMetric>(MultiMetric);
return Promise.resolve(m); return Promise.resolve(m);
} }

View File

@ -34,7 +34,7 @@ export function main() {
} }
} }
]; ];
return Injector.create(providers).get(JsonFileReporter); return Injector.create(providers).get<JsonFileReporter>(JsonFileReporter);
} }
it('should write all data into a file', it('should write all data into a file',

View File

@ -17,7 +17,7 @@ export function main() {
ids.map(id => ({provide: id, useValue: new MockReporter(id)})), ids.map(id => ({provide: id, useValue: new MockReporter(id)})),
MultiReporter.provideWith(ids) MultiReporter.provideWith(ids)
]) ])
.get(MultiReporter); .get<MultiReporter>(MultiReporter);
return Promise.resolve(r); return Promise.resolve(r);
} }

View File

@ -27,7 +27,7 @@ describe('metadata bundler', () => {
const originalOne = './src/one'; const originalOne = './src/one';
const originalTwo = './src/two/index'; const originalTwo = './src/two/index';
expect(Object.keys(result.metadata.origins) expect(Object.keys(result.metadata.origins !)
.sort() .sort()
.map(name => ({name, value: result.metadata.origins ![name]}))) .map(name => ({name, value: result.metadata.origins ![name]})))
.toEqual([ .toEqual([

View File

@ -102,6 +102,7 @@ export class MockNode implements ts.Node {
export class MockIdentifier extends MockNode implements ts.Identifier { export class MockIdentifier extends MockNode implements ts.Identifier {
public text: string; public text: string;
public escapedText: ts.__String;
// tslint:disable // tslint:disable
public _primaryExpressionBrand: any; public _primaryExpressionBrand: any;
public _memberExpressionBrand: any; public _memberExpressionBrand: any;
@ -137,12 +138,14 @@ export class MockVariableDeclaration extends MockNode implements ts.VariableDecl
} }
export class MockSymbol implements ts.Symbol { export class MockSymbol implements ts.Symbol {
public escapedName: ts.__String;
constructor( constructor(
public name: string, private node: ts.Declaration = MockVariableDeclaration.of(name), public name: string, private node: ts.Declaration = MockVariableDeclaration.of(name),
public flags: ts.SymbolFlags = 0) {} public flags: ts.SymbolFlags = 0) {}
getFlags(): ts.SymbolFlags { return this.flags; } getFlags(): ts.SymbolFlags { return this.flags; }
getName(): string { return this.name; } getName(): string { return this.name; }
getEscapedName(): ts.__String { return this.escapedName; }
getDeclarations(): ts.Declaration[] { return [this.node]; } getDeclarations(): ts.Declaration[] { return [this.node]; }
getDocumentationComment(): ts.SymbolDisplayPart[] { return []; } getDocumentationComment(): ts.SymbolDisplayPart[] { return []; }
// TODO(vicb): removed in TS 2.2 // TODO(vicb): removed in TS 2.2

View File

@ -61,8 +61,10 @@ export function main() {
const injector = Injector.create([{provide: IterableDiffers, useValue: parent}]); const injector = Injector.create([{provide: IterableDiffers, useValue: parent}]);
const childInjector = Injector.create([IterableDiffers.extend([factory2])], injector); const childInjector = Injector.create([IterableDiffers.extend([factory2])], injector);
expect(injector.get(IterableDiffers).factories).toEqual([factory1]); expect(injector.get<IterableDiffers>(IterableDiffers).factories).toEqual([factory1]);
expect(childInjector.get(IterableDiffers).factories).toEqual([factory2, factory1]); expect(childInjector.get<IterableDiffers>(IterableDiffers).factories).toEqual([
factory2, factory1
]);
}); });
}); });
}); });

View File

@ -98,7 +98,7 @@ export function main() {
it('should resolve dependencies based on type information', () => { it('should resolve dependencies based on type information', () => {
const injector = Injector.create([Engine.PROVIDER, Car.PROVIDER]); const injector = Injector.create([Engine.PROVIDER, Car.PROVIDER]);
const car = injector.get(Car); const car = injector.get<Car>(Car);
expect(car).toBeAnInstanceOf(Car); expect(car).toBeAnInstanceOf(Car);
expect(car.engine).toBeAnInstanceOf(Engine); expect(car.engine).toBeAnInstanceOf(Engine);
@ -138,7 +138,7 @@ export function main() {
const injector = Injector.create( const injector = Injector.create(
[Engine.PROVIDER, {provide: Car, useFactory: sportsCarFactory, deps: [Engine]}]); [Engine.PROVIDER, {provide: Car, useFactory: sportsCarFactory, deps: [Engine]}]);
const car = injector.get(Car); const car = injector.get<Car>(Car);
expect(car).toBeAnInstanceOf(SportsCar); expect(car).toBeAnInstanceOf(SportsCar);
expect(car.engine).toBeAnInstanceOf(Engine); expect(car.engine).toBeAnInstanceOf(Engine);
}); });
@ -208,7 +208,7 @@ export function main() {
{provide: Car, useFactory: (e: Engine) => new SportsCar(e), deps: [Engine]} {provide: Car, useFactory: (e: Engine) => new SportsCar(e), deps: [Engine]}
]); ]);
const car = injector.get(Car); const car = injector.get<Car>(Car);
expect(car).toBeAnInstanceOf(SportsCar); expect(car).toBeAnInstanceOf(SportsCar);
expect(car.engine).toBeAnInstanceOf(Engine); expect(car.engine).toBeAnInstanceOf(Engine);
}); });
@ -216,7 +216,7 @@ export function main() {
it('should support optional dependencies', () => { it('should support optional dependencies', () => {
const injector = Injector.create([CarWithOptionalEngine.PROVIDER]); const injector = Injector.create([CarWithOptionalEngine.PROVIDER]);
const car = injector.get(CarWithOptionalEngine); const car = injector.get<CarWithOptionalEngine>(CarWithOptionalEngine);
expect(car.engine).toEqual(null); expect(car.engine).toEqual(null);
}); });
@ -288,7 +288,7 @@ export function main() {
Injector.create([CarWithDashboard.PROVIDER, Engine.PROVIDER, Dashboard.PROVIDER]); Injector.create([CarWithDashboard.PROVIDER, Engine.PROVIDER, Dashboard.PROVIDER]);
expect(() => injector.get(CarWithDashboard)) expect(() => injector.get(CarWithDashboard))
.toThrowError( .toThrowError(
`StaticInjectorError[${stringify(CarWithDashboard)} -> ${stringify(Dashboard)} -> DashboardSoftware]: `StaticInjectorError[${stringify(CarWithDashboard)} -> ${stringify(Dashboard)} -> DashboardSoftware]:
NullInjectorError: No provider for DashboardSoftware!`); NullInjectorError: No provider for DashboardSoftware!`);
}); });
@ -364,7 +364,7 @@ export function main() {
const parent = Injector.create([Car.PROVIDER, Engine.PROVIDER]); const parent = Injector.create([Car.PROVIDER, Engine.PROVIDER]);
const child = Injector.create([TurboEngine.PROVIDER], parent); const child = Injector.create([TurboEngine.PROVIDER], parent);
const carFromChild = child.get(Car); const carFromChild = child.get<Car>(Car);
expect(carFromChild.engine).toBeAnInstanceOf(Engine); expect(carFromChild.engine).toBeAnInstanceOf(Engine);
}); });
@ -391,7 +391,7 @@ export function main() {
it('should instantiate an object in the context of the injector', () => { it('should instantiate an object in the context of the injector', () => {
const inj = Injector.create([Engine.PROVIDER]); const inj = Injector.create([Engine.PROVIDER]);
const childInj = Injector.create([Car.PROVIDER], inj); const childInj = Injector.create([Car.PROVIDER], inj);
const car = childInj.get(Car); const car = childInj.get<Car>(Car);
expect(car).toBeAnInstanceOf(Car); expect(car).toBeAnInstanceOf(Car);
expect(car.engine).toBe(inj.get(Engine)); expect(car.engine).toBe(inj.get(Engine));
}); });
@ -415,7 +415,7 @@ export function main() {
parent); parent);
expect(() => child.get(Car)) expect(() => child.get(Car))
.toThrowError(`StaticInjectorError[${stringify(Car)} -> ${stringify(Engine)}]: .toThrowError(`StaticInjectorError[${stringify(Car)} -> ${stringify(Engine)}]:
NullInjectorError: No provider for Engine!`); NullInjectorError: No provider for Engine!`);
}); });
}); });
@ -430,7 +430,7 @@ export function main() {
], ],
parent); parent);
expect(child.get(Car).engine).toBeAnInstanceOf(Engine); expect(child.get<Car>(Car).engine).toBeAnInstanceOf(Engine);
}); });
}); });
}); });

View File

@ -232,7 +232,7 @@ export function main() {
const fixture = const fixture =
TestBed.overrideProvider(SomeDep, {useFactory: () => overwrittenValue, deps: []}) TestBed.overrideProvider(SomeDep, {useFactory: () => overwrittenValue, deps: []})
.configureTestingModule({providers: [SomeDep], imports: [SomeModule]}) .configureTestingModule({providers: [SomeDep], imports: [SomeModule]})
.createComponent(SomePublicComponent); .createComponent<SomePublicComponent>(SomePublicComponent);
expect(fixture.componentInstance.dep).toBe(overwrittenValue); expect(fixture.componentInstance.dep).toBe(overwrittenValue);
}); });

View File

@ -135,7 +135,7 @@ ng1AppModule.component('ng1Hero', {
// This AngularJS service will be "upgraded" to be used in Angular // This AngularJS service will be "upgraded" to be used in Angular
ng1AppModule.factory( ng1AppModule.factory(
'titleCase', 'titleCase',
() => (value: string) => value.replace(/((^|\s)[a-z])/g, (_, c) => c.toUpperCase())); (() => (value: string) => value.replace(/((^|\s)[a-z])/g, (_, c) => c.toUpperCase())) as any);
// #enddocregion // #enddocregion
// #docregion downgrade-ng2-heroes-service // #docregion downgrade-ng2-heroes-service
@ -154,18 +154,20 @@ ng1AppModule.component('exampleApp', {
// We inject the "downgraded" HeroesService into this AngularJS component // We inject the "downgraded" HeroesService into this AngularJS component
// (We don't need the `HeroesService` type for AngularJS DI - it just helps with TypeScript // (We don't need the `HeroesService` type for AngularJS DI - it just helps with TypeScript
// compilation) // compilation)
controller: [ controller:
'heroesService', function(heroesService: HeroesService) { this.heroesService = heroesService; } [
], 'heroesService',
// This template make use of the downgraded `ng2-heroes` component function(heroesService: HeroesService) { this.heroesService = heroesService; }
// Note that because its element is compiled by AngularJS we must use kebab-case attributes for ],
// inputs and outputs // This template make use of the downgraded `ng2-heroes` component
template: `<link rel="stylesheet" href="./styles.css"> // Note that because its element is compiled by AngularJS we must use kebab-case attributes
// for inputs and outputs
template: `<link rel="stylesheet" href="./styles.css">
<ng2-heroes [heroes]="$ctrl.heroesService.heroes" (add-hero)="$ctrl.heroesService.addHero()" (remove-hero)="$ctrl.heroesService.removeHero($event)"> <ng2-heroes [heroes]="$ctrl.heroesService.heroes" (add-hero)="$ctrl.heroesService.addHero()" (remove-hero)="$ctrl.heroesService.removeHero($event)">
<h1>Heroes</h1> <h1>Heroes</h1>
<p class="extra">There are {{ $ctrl.heroesService.heroes.length }} heroes.</p> <p class="extra">There are {{ $ctrl.heroesService.heroes.length }} heroes.</p>
</ng2-heroes>` </ng2-heroes>`
}); } as any);
// #enddocregion // #enddocregion
// #enddocregion // #enddocregion

View File

@ -592,7 +592,7 @@ export function main() {
platform.bootstrapModule(ExampleModule).then(ref => { platform.bootstrapModule(ExampleModule).then(ref => {
const mock = ref.injector.get(MockBackend); const mock = ref.injector.get(MockBackend);
const http = ref.injector.get(Http); const http = ref.injector.get(Http);
ref.injector.get(NgZone).run(() => { ref.injector.get<NgZone>(NgZone).run(() => {
NgZone.assertInAngularZone(); NgZone.assertInAngularZone();
mock.connections.subscribe((mc: MockConnection) => { mock.connections.subscribe((mc: MockConnection) => {
NgZone.assertInAngularZone(); NgZone.assertInAngularZone();
@ -612,11 +612,11 @@ export function main() {
platform.bootstrapModule(ExampleModule).then(ref => { platform.bootstrapModule(ExampleModule).then(ref => {
const mock = ref.injector.get(MockBackend); const mock = ref.injector.get(MockBackend);
const http = ref.injector.get(Http); const http = ref.injector.get(Http);
expect(ref.injector.get(NgZone).hasPendingMacrotasks).toBeFalsy(); expect(ref.injector.get<NgZone>(NgZone).hasPendingMacrotasks).toBeFalsy();
ref.injector.get(NgZone).run(() => { ref.injector.get<NgZone>(NgZone).run(() => {
NgZone.assertInAngularZone(); NgZone.assertInAngularZone();
mock.connections.subscribe((mc: MockConnection) => { mock.connections.subscribe((mc: MockConnection) => {
expect(ref.injector.get(NgZone).hasPendingMacrotasks).toBeTruthy(); expect(ref.injector.get<NgZone>(NgZone).hasPendingMacrotasks).toBeTruthy();
mc.mockRespond(new Response(new ResponseOptions({body: 'success!', status: 200}))); mc.mockRespond(new Response(new ResponseOptions({body: 'success!', status: 200})));
}); });
http.get('http://localhost/testing').subscribe(resp => { http.get('http://localhost/testing').subscribe(resp => {
@ -631,11 +631,11 @@ export function main() {
platform.bootstrapModule(HttpBeforeExampleModule).then(ref => { platform.bootstrapModule(HttpBeforeExampleModule).then(ref => {
const mock = ref.injector.get(MockBackend); const mock = ref.injector.get(MockBackend);
const http = ref.injector.get(Http); const http = ref.injector.get(Http);
expect(ref.injector.get(NgZone).hasPendingMacrotasks).toBeFalsy(); expect(ref.injector.get<NgZone>(NgZone).hasPendingMacrotasks).toBeFalsy();
ref.injector.get(NgZone).run(() => { ref.injector.get<NgZone>(NgZone).run(() => {
NgZone.assertInAngularZone(); NgZone.assertInAngularZone();
mock.connections.subscribe((mc: MockConnection) => { mock.connections.subscribe((mc: MockConnection) => {
expect(ref.injector.get(NgZone).hasPendingMacrotasks).toBeTruthy(); expect(ref.injector.get<NgZone>(NgZone).hasPendingMacrotasks).toBeTruthy();
mc.mockRespond(new Response(new ResponseOptions({body: 'success!', status: 200}))); mc.mockRespond(new Response(new ResponseOptions({body: 'success!', status: 200})));
}); });
http.get('http://localhost/testing').subscribe(resp => { http.get('http://localhost/testing').subscribe(resp => {
@ -650,11 +650,11 @@ export function main() {
platform.bootstrapModule(HttpAfterExampleModule).then(ref => { platform.bootstrapModule(HttpAfterExampleModule).then(ref => {
const mock = ref.injector.get(MockBackend); const mock = ref.injector.get(MockBackend);
const http = ref.injector.get(Http); const http = ref.injector.get(Http);
expect(ref.injector.get(NgZone).hasPendingMacrotasks).toBeFalsy(); expect(ref.injector.get<NgZone>(NgZone).hasPendingMacrotasks).toBeFalsy();
ref.injector.get(NgZone).run(() => { ref.injector.get<NgZone>(NgZone).run(() => {
NgZone.assertInAngularZone(); NgZone.assertInAngularZone();
mock.connections.subscribe((mc: MockConnection) => { mock.connections.subscribe((mc: MockConnection) => {
expect(ref.injector.get(NgZone).hasPendingMacrotasks).toBeTruthy(); expect(ref.injector.get<NgZone>(NgZone).hasPendingMacrotasks).toBeTruthy();
mc.mockRespond(new Response(new ResponseOptions({body: 'success!', status: 200}))); mc.mockRespond(new Response(new ResponseOptions({body: 'success!', status: 200})));
}); });
http.get('http://localhost/testing').subscribe(resp => { http.get('http://localhost/testing').subscribe(resp => {
@ -688,7 +688,7 @@ export function main() {
platform.bootstrapModule(HttpClientExmapleModule).then(ref => { platform.bootstrapModule(HttpClientExmapleModule).then(ref => {
const mock = ref.injector.get(HttpTestingController) as HttpTestingController; const mock = ref.injector.get(HttpTestingController) as HttpTestingController;
const http = ref.injector.get(HttpClient); const http = ref.injector.get(HttpClient);
ref.injector.get(NgZone).run(() => { ref.injector.get<NgZone>(NgZone).run(() => {
http.get('http://localhost/testing').subscribe(body => { http.get('http://localhost/testing').subscribe(body => {
NgZone.assertInAngularZone(); NgZone.assertInAngularZone();
expect(body).toEqual('success!'); expect(body).toEqual('success!');
@ -703,13 +703,13 @@ export function main() {
platform.bootstrapModule(HttpClientExmapleModule).then(ref => { platform.bootstrapModule(HttpClientExmapleModule).then(ref => {
const mock = ref.injector.get(HttpTestingController) as HttpTestingController; const mock = ref.injector.get(HttpTestingController) as HttpTestingController;
const http = ref.injector.get(HttpClient); const http = ref.injector.get(HttpClient);
ref.injector.get(NgZone).run(() => { ref.injector.get<NgZone>(NgZone).run(() => {
http.get('http://localhost/testing').subscribe(body => { http.get('http://localhost/testing').subscribe(body => {
expect(body).toEqual('success!'); expect(body).toEqual('success!');
}); });
expect(ref.injector.get(NgZone).hasPendingMacrotasks).toBeTruthy(); expect(ref.injector.get<NgZone>(NgZone).hasPendingMacrotasks).toBeTruthy();
mock.expectOne('http://localhost/testing').flush('success!'); mock.expectOne('http://localhost/testing').flush('success!');
expect(ref.injector.get(NgZone).hasPendingMacrotasks).toBeFalsy(); expect(ref.injector.get<NgZone>(NgZone).hasPendingMacrotasks).toBeFalsy();
}); });
}); });
})); }));

View File

@ -32,7 +32,7 @@ export const WORKER_UI_LOCATION_PROVIDERS = <StaticProvider[]>[
function initUiLocation(injector: Injector): () => void { function initUiLocation(injector: Injector): () => void {
return () => { return () => {
const zone = injector.get(NgZone); const zone = injector.get<NgZone>(NgZone);
zone.runGuarded(() => injector.get(MessageBasedPlatformLocation).start()); zone.runGuarded(() => injector.get(MessageBasedPlatformLocation).start());
}; };

View File

@ -112,7 +112,7 @@ export const _WORKER_UI_PLATFORM_PROVIDERS: StaticProvider[] = [
function initializeGenericWorkerRenderer(injector: Injector) { function initializeGenericWorkerRenderer(injector: Injector) {
const bus = injector.get(MessageBus); const bus = injector.get(MessageBus);
const zone = injector.get(NgZone); const zone = injector.get<NgZone>(NgZone);
bus.attachToZone(zone); bus.attachToZone(zone);
// initialize message services after the bus has been created // initialize message services after the bus has been created

View File

@ -381,7 +381,7 @@ export class RouterInitializer {
const opts = this.injector.get(ROUTER_CONFIGURATION); const opts = this.injector.get(ROUTER_CONFIGURATION);
const preloader = this.injector.get(RouterPreloader); const preloader = this.injector.get(RouterPreloader);
const router = this.injector.get(Router); const router = this.injector.get(Router);
const ref = this.injector.get(ApplicationRef); const ref = this.injector.get<ApplicationRef>(ApplicationRef);
if (bootstrappedComponentRef !== ref.components[0]) { if (bootstrappedComponentRef !== ref.components[0]) {
return; return;

View File

@ -399,7 +399,7 @@ export class UpgradeAdapter {
Promise.all([this.ng2BootstrapDeferred.promise, ng1BootstrapPromise]).then(([ng1Injector]) => { Promise.all([this.ng2BootstrapDeferred.promise, ng1BootstrapPromise]).then(([ng1Injector]) => {
angular.element(element).data !(controllerKey(INJECTOR_KEY), this.moduleRef !.injector); angular.element(element).data !(controllerKey(INJECTOR_KEY), this.moduleRef !.injector);
this.moduleRef !.injector.get(NgZone).run( this.moduleRef !.injector.get<NgZone>(NgZone).run(
() => { (<any>upgrade)._bootstrapDone(this.moduleRef, ng1Injector); }); () => { (<any>upgrade)._bootstrapDone(this.moduleRef, ng1Injector); });
}, onError); }, onError);
return upgrade; return upgrade;

View File

@ -67,7 +67,7 @@ export class UpgradeNg1ComponentAdapterBuilder {
extractBindings() { extractBindings() {
const btcIsObject = typeof this.directive !.bindToController === 'object'; const btcIsObject = typeof this.directive !.bindToController === 'object';
if (btcIsObject && Object.keys(this.directive !.scope).length) { if (btcIsObject && Object.keys(this.directive !.scope !).length) {
throw new Error( throw new Error(
`Binding definitions on scope and controller at the same time are not supported.`); `Binding definitions on scope and controller at the same time are not supported.`);
} }

View File

@ -227,7 +227,7 @@ export class UpgradeComponent implements OnInit, OnChanges, DoCheck, OnDestroy {
private initializeBindings(directive: angular.IDirective) { private initializeBindings(directive: angular.IDirective) {
const btcIsObject = typeof directive.bindToController === 'object'; const btcIsObject = typeof directive.bindToController === 'object';
if (btcIsObject && Object.keys(directive.scope).length) { if (btcIsObject && Object.keys(directive.scope !).length) {
throw new Error( throw new Error(
`Binding definitions on scope and controller at the same time is not supported.`); `Binding definitions on scope and controller at the same time is not supported.`);
} }

View File

@ -93,7 +93,7 @@ export declare const WORKER_APP_LOCATION_PROVIDERS: ({
} | { } | {
provide: InjectionToken<Promise<any>>; provide: InjectionToken<Promise<any>>;
useFactory: (platformLocation: WebWorkerPlatformLocation) => Promise<any>; useFactory: (platformLocation: WebWorkerPlatformLocation) => Promise<any>;
deps: typeof PlatformLocation[]; deps: (typeof PlatformLocation)[];
})[]; })[];
/** @experimental */ /** @experimental */