From 7a569a7c525a75782e1f3c045eb17b96a4a07cc6 Mon Sep 17 00:00:00 2001 From: Keen Yee Liau Date: Wed, 11 Sep 2019 22:08:20 -0700 Subject: [PATCH] test(language-service): Make tests better by adding more assertions (#32630) This commit adds more assertions to a few smoke tests in typescript_host_spec.ts PR Close #32630 --- packages/language-service/test/test_utils.ts | 16 +++++++++ .../test/typescript_host_spec.ts | 34 +++++++++++++------ packages/tsconfig.json | 1 + 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/packages/language-service/test/test_utils.ts b/packages/language-service/test/test_utils.ts index 010847527e..fb7511568f 100644 --- a/packages/language-service/test/test_utils.ts +++ b/packages/language-service/test/test_utils.ts @@ -6,6 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ +import {CompileNgModuleMetadata, NgAnalyzedModules} from '@angular/compiler'; import {setup} from '@angular/compiler-cli/test/test_support'; import * as fs from 'fs'; import * as path from 'path'; @@ -403,3 +404,18 @@ function getReferenceMarkers(value: string): ReferenceResult { function removeReferenceMarkers(value: string): string { return value.replace(referenceMarker, (match, text) => text.replace(/ᐱ/g, '')); } + +/** + * Find the StaticSymbol that has the specified `directiveName` and return its + * Angular metadata, if any. + * @param ngModules analyzed modules + * @param directiveName + */ +export function findDirectiveMetadataByName( + ngModules: NgAnalyzedModules, directiveName: string): CompileNgModuleMetadata|undefined { + for (const [key, value] of ngModules.ngModuleByPipeOrDirective) { + if (key.name === directiveName) { + return value; + } + } +} diff --git a/packages/language-service/test/typescript_host_spec.ts b/packages/language-service/test/typescript_host_spec.ts index b130c8938b..d57f3d89b3 100644 --- a/packages/language-service/test/typescript_host_spec.ts +++ b/packages/language-service/test/typescript_host_spec.ts @@ -12,35 +12,47 @@ import * as ts from 'typescript'; import {TypeScriptServiceHost} from '../src/typescript_host'; import {toh} from './test_data'; -import {MockTypescriptHost} from './test_utils'; +import {MockTypescriptHost, findDirectiveMetadataByName} from './test_utils'; describe('TypeScriptServiceHost', () => { - it('should be able to create a typescript host', () => { - const tsLSHost = new MockTypescriptHost(['/app/main.ts'], toh); - const tsLS = ts.createLanguageService(tsLSHost); - expect(() => new TypeScriptServiceHost(tsLSHost, tsLS)).not.toThrow(); - }); - - it('should be able to analyze modules', () => { + it('should be able to create a typescript host and analyze modules', () => { const tsLSHost = new MockTypescriptHost(['/app/main.ts'], toh); const tsLS = ts.createLanguageService(tsLSHost); const ngLSHost = new TypeScriptServiceHost(tsLSHost, tsLS); - expect(ngLSHost.getAnalyzedModules()).toBeDefined(); + const analyzedModules = ngLSHost.getAnalyzedModules(); + expect(analyzedModules.files.length).toBeGreaterThan(0); + expect(analyzedModules.ngModules.length).toBeGreaterThan(0); + expect(analyzedModules.ngModuleByPipeOrDirective.size).toBeGreaterThan(0); + expect(analyzedModules.symbolsMissingModule).toEqual([]); + // NgClass is defined in @angular/common, which is imported in main.ts + const ngClass = findDirectiveMetadataByName(analyzedModules, 'NgClass'); + expect(ngClass).toBeDefined(); + // AppComponent is defined in app.component.ts + const appComp = findDirectiveMetadataByName(analyzedModules, 'AppComponent'); + expect(appComp).toBeDefined(); }); it('should be able to analyze modules without a tsconfig.json file', () => { const tsLSHost = new MockTypescriptHost(['foo.ts'], toh); const tsLS = ts.createLanguageService(tsLSHost); const ngLSHost = new TypeScriptServiceHost(tsLSHost, tsLS); - expect(ngLSHost.getAnalyzedModules()).toBeDefined(); + const analyzedModules = ngLSHost.getAnalyzedModules(); + expect(analyzedModules.files.length).toBeGreaterThan(0); + expect(analyzedModules.ngModules.length).toBe(0); + expect(analyzedModules.ngModuleByPipeOrDirective.size).toBe(0); + expect(analyzedModules.symbolsMissingModule).toEqual([]); }); it('should not throw if there is no script names', () => { const tsLSHost = new MockTypescriptHost([], toh); const tsLS = ts.createLanguageService(tsLSHost); const ngLSHost = new TypeScriptServiceHost(tsLSHost, tsLS); - expect(() => ngLSHost.getAnalyzedModules()).not.toThrow(); + const analyzedModules = ngLSHost.getAnalyzedModules(); + expect(analyzedModules.files.length).toBe(0); + expect(analyzedModules.ngModules.length).toBe(0); + expect(analyzedModules.ngModuleByPipeOrDirective.size).toBe(0); + expect(analyzedModules.symbolsMissingModule).toBeUndefined(); }); it('should clear the caches if program changes', () => { diff --git a/packages/tsconfig.json b/packages/tsconfig.json index e9b7d309eb..25db54d1ea 100644 --- a/packages/tsconfig.json +++ b/packages/tsconfig.json @@ -4,6 +4,7 @@ // packages like the native "http" module are resolved to the Angular "http" package. "baseUrl": "..", "declaration": true, + "downlevelIteration": true, "experimentalDecorators": true, "emitDecoratorMetadata": true, "module": "commonjs",