fix(language-service): reset MockHost after every spec instead of creating new LS (#33200)

This commit speeds up the tests by calling `MockHost.reset()` in
`beforeEach()` instead of destroying the entire language service and
creating a new one. The creation of a new language service instance is
expensive due to the need to initialize many core Symbols when creating
a new program.

This speeds ups the test (on my local machine) from 35 secs to 15 secs.

PR Close #33200
This commit is contained in:
Keen Yee Liau 2019-10-16 12:00:41 -07:00 committed by Matias Niemelä
parent 43241a560a
commit 11bf7679a1
6 changed files with 27 additions and 62 deletions

View File

@ -13,7 +13,6 @@ ts_library(
"//packages/compiler",
"//packages/compiler-cli/test:test_utils",
"//packages/language-service",
"@npm//reflect-metadata",
"@npm//typescript",
],
)

View File

@ -9,24 +9,17 @@
import * as ts from 'typescript';
import {createLanguageService} from '../src/language_service';
import {LanguageService} from '../src/types';
import {TypeScriptServiceHost} from '../src/typescript_host';
import {MockTypescriptHost} from './test_utils';
describe('definitions', () => {
let mockHost: MockTypescriptHost;
let service: ts.LanguageService;
let ngHost: TypeScriptServiceHost;
let ngService: LanguageService;
const mockHost = new MockTypescriptHost(['/app/main.ts']);
const service = ts.createLanguageService(mockHost);
const ngHost = new TypeScriptServiceHost(mockHost, service);
const ngService = createLanguageService(ngHost);
beforeEach(() => {
// Create a new mockHost every time to reset any files that are overridden.
mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
service = ts.createLanguageService(mockHost);
ngHost = new TypeScriptServiceHost(mockHost, service);
ngService = createLanguageService(ngHost);
});
beforeEach(() => { mockHost.reset(); });
it('should be able to find field in an interpolation', () => {
const fileName = mockHost.addCode(`
@ -201,7 +194,6 @@ describe('definitions', () => {
});
it('should be able to find an input provider', () => {
// '/app/parsing-cases.ts', 'tcName',
const fileName = mockHost.addCode(`
@Component({
template: '<test-comp ~{start-my}[«tcName»]="name"~{end-my}></div>'

View File

@ -8,7 +8,6 @@
import * as ts from 'typescript';
import {createLanguageService} from '../src/language_service';
import * as ng from '../src/types';
import {TypeScriptServiceHost} from '../src/typescript_host';
import {MockTypescriptHost} from './test_utils';
@ -29,17 +28,12 @@ const NG_FOR_CASES = '/app/ng-for-cases.ts';
const NG_IF_CASES = '/app/ng-if-cases.ts';
describe('diagnostics', () => {
let mockHost: MockTypescriptHost;
let ngHost: TypeScriptServiceHost;
let tsLS: ts.LanguageService;
let ngLS: ng.LanguageService;
const mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
const tsLS = ts.createLanguageService(mockHost);
const ngHost = new TypeScriptServiceHost(mockHost, tsLS);
const ngLS = createLanguageService(ngHost);
beforeEach(() => {
mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
tsLS = ts.createLanguageService(mockHost);
ngHost = new TypeScriptServiceHost(mockHost, tsLS);
ngLS = createLanguageService(ngHost);
});
beforeEach(() => { mockHost.reset(); });
it('should produce no diagnostics for test.ng', () => {
// there should not be any errors on existing external template

View File

@ -9,28 +9,17 @@
import * as ts from 'typescript';
import {createLanguageService} from '../src/language_service';
import {LanguageService} from '../src/types';
import {TypeScriptServiceHost} from '../src/typescript_host';
import {MockTypescriptHost} from './test_utils';
fdescribe('hover', () => {
// const mockHost: MockTypescriptHost;
// const tsLS: ts.LanguageService;
// const ngLSHost: TypeScriptServiceHost;
// const ngLS: LanguageService;
describe('hover', () => {
const mockHost = new MockTypescriptHost(['/app/main.ts']);
const tsLS = ts.createLanguageService(mockHost);
const ngLSHost = new TypeScriptServiceHost(mockHost, tsLS);
const ngLS = createLanguageService(ngLSHost);
beforeEach(() => {
// mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
// tsLS = ts.createLanguageService(mockHost);
// ngLSHost = new TypeScriptServiceHost(mockHost, tsLS);
// ngLS = createLanguageService(ngLSHost);
mockHost.reset();
});
beforeEach(() => { mockHost.reset(); });
it('should be able to find field in an interpolation', () => {
const fileName = mockHost.addCode(`
@ -188,20 +177,18 @@ fdescribe('hover', () => {
});
it('should be able to find the NgModule of a directive', () => {
const fileName = '/app/app.component.ts';
mockHost.override(fileName, `
import {Directive} from '@angular/core';
@Directive({
selector: '[string-model]',
})
export class «AppComponent» {}`);
const marker = mockHost.getReferenceMarkerFor(fileName, 'AppComponent');
const quickInfo = ngLS.getHoverAt(fileName, marker.start);
const fileName = '/app/parsing-cases.ts';
const content = mockHost.readFile(fileName) !;
const position = content.indexOf('StringModel');
expect(position).toBeGreaterThan(0);
const quickInfo = ngLS.getHoverAt(fileName, position);
expect(quickInfo).toBeTruthy();
const {textSpan, displayParts} = quickInfo !;
expect(textSpan).toEqual(marker);
expect(toText(displayParts)).toBe('(directive) AppModule.AppComponent: class');
expect(textSpan).toEqual({
start: position,
length: 'StringModel'.length,
});
expect(toText(displayParts)).toBe('(directive) AppModule.StringModel: class');
});
});

View File

@ -15,18 +15,12 @@ import {TypeScriptServiceHost} from '../src/typescript_host';
import {MockTypescriptHost} from './test_utils';
describe('references', () => {
let documentRegistry = ts.createDocumentRegistry();
let mockHost: MockTypescriptHost;
let service: ts.LanguageService;
let ngHost: TypeScriptServiceHost;
let ngService: LanguageService = createLanguageService(undefined !);
const mockHost = new MockTypescriptHost(['/app/main.ts']);
const service = ts.createLanguageService(mockHost);
const ngHost = new TypeScriptServiceHost(mockHost, service);
const ngService = createLanguageService(ngHost);
beforeEach(() => {
mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
service = ts.createLanguageService(mockHost, documentRegistry);
ngHost = new TypeScriptServiceHost(mockHost, service);
ngService = createLanguageService(ngHost);
});
beforeEach(() => { mockHost.reset(); });
it('should be able to get template references',
() => { expect(() => ngService.getTemplateReferences()).not.toThrow(); });

View File

@ -6,7 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/
import 'reflect-metadata';
import * as ts from 'typescript';
import {TypeScriptServiceHost} from '../src/typescript_host';