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
This commit is contained in:
Keen Yee Liau 2019-09-11 22:08:20 -07:00 committed by Kara Erickson
parent 5cf597249c
commit 7a569a7c52
3 changed files with 40 additions and 11 deletions

View File

@ -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;
}
}
}

View File

@ -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', () => {

View File

@ -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",