test(language-service): Make project service a singleton (#39308)

Constructing a project service is expensive. Making it a singleton could
speed up tests considerably.

On my MacBook Pro, test execution went from 24.4s to 14.5s (~40% improvement).

PR Close #39308
This commit is contained in:
Keen Yee Liau 2020-10-16 12:04:36 -07:00 committed by Andrew Kushnir
parent 0c01c4a898
commit 1b21350e17
1 changed files with 19 additions and 11 deletions

View File

@ -69,6 +69,12 @@ export const host: ts.server.ServerHost = {
},
};
/**
* Constructing a project service is expensive (~2.5s on MacBook Pro), so it
* should be a singleton service shared throughout all tests.
*/
let projectService: ts.server.ProjectService;
/**
* Create a ConfiguredProject and an actual program for the test project located
* in packages/language-service/test/project. Project creation exercises the
@ -76,7 +82,8 @@ export const host: ts.server.ServerHost = {
* and modify test files.
*/
export function setup() {
const projectService = new ts.server.ProjectService({
if (!projectService) {
projectService = new ts.server.ProjectService({
host,
logger,
cancellationToken: ts.server.nullCancellationToken,
@ -87,6 +94,7 @@ export function setup() {
// Opening APP_COMPONENT forces a new ConfiguredProject to be created based
// on the tsconfig.json in the test project.
projectService.openClientFile(APP_COMPONENT);
}
const project = projectService.findProject(TSCONFIG);
if (!project) {
throw new Error(`Failed to create project for ${TSCONFIG}`);