From 11bf7679a1acfb1e9c00d121b74102128e97517c Mon Sep 17 00:00:00 2001 From: Keen Yee Liau Date: Wed, 16 Oct 2019 12:00:41 -0700 Subject: [PATCH] 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 --- packages/language-service/test/BUILD.bazel | 1 - .../language-service/test/definitions_spec.ts | 18 +++------ .../language-service/test/diagnostics_spec.ts | 16 +++----- packages/language-service/test/hover_spec.ts | 37 ++++++------------- .../test/template_references_spec.ts | 16 +++----- .../test/typescript_host_spec.ts | 1 - 6 files changed, 27 insertions(+), 62 deletions(-) diff --git a/packages/language-service/test/BUILD.bazel b/packages/language-service/test/BUILD.bazel index 0312c107d2..0838517175 100644 --- a/packages/language-service/test/BUILD.bazel +++ b/packages/language-service/test/BUILD.bazel @@ -13,7 +13,6 @@ ts_library( "//packages/compiler", "//packages/compiler-cli/test:test_utils", "//packages/language-service", - "@npm//reflect-metadata", "@npm//typescript", ], ) diff --git a/packages/language-service/test/definitions_spec.ts b/packages/language-service/test/definitions_spec.ts index 81fdff7718..8a7d731d7c 100644 --- a/packages/language-service/test/definitions_spec.ts +++ b/packages/language-service/test/definitions_spec.ts @@ -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: '' diff --git a/packages/language-service/test/diagnostics_spec.ts b/packages/language-service/test/diagnostics_spec.ts index 0b33ae49a6..a4771f1a58 100644 --- a/packages/language-service/test/diagnostics_spec.ts +++ b/packages/language-service/test/diagnostics_spec.ts @@ -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 diff --git a/packages/language-service/test/hover_spec.ts b/packages/language-service/test/hover_spec.ts index ec2f3a3960..a192c0fbf2 100644 --- a/packages/language-service/test/hover_spec.ts +++ b/packages/language-service/test/hover_spec.ts @@ -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'); }); }); diff --git a/packages/language-service/test/template_references_spec.ts b/packages/language-service/test/template_references_spec.ts index 0614fd9ad7..4740bd671d 100644 --- a/packages/language-service/test/template_references_spec.ts +++ b/packages/language-service/test/template_references_spec.ts @@ -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(); }); diff --git a/packages/language-service/test/typescript_host_spec.ts b/packages/language-service/test/typescript_host_spec.ts index 8105ddf2c5..7aa0512cf6 100644 --- a/packages/language-service/test/typescript_host_spec.ts +++ b/packages/language-service/test/typescript_host_spec.ts @@ -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';