Skip to content

Commit 4efcfb7

Browse files
committed
Some refactoring
1 parent 1ac980e commit 4efcfb7

File tree

1 file changed

+49
-27
lines changed

1 file changed

+49
-27
lines changed

src/testRunner/unittests/tsbuild/watchMode.ts

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,33 @@ namespace ts.tscWatch {
113113
}
114114
}
115115

116-
const core = subProjectFiles(SubProject.core, /*anotherModuleAndSomeDecl*/ true);
117-
const logic = subProjectFiles(SubProject.logic);
118-
const tests = subProjectFiles(SubProject.tests);
119-
const ui = subProjectFiles(SubProject.ui);
120-
const allFiles: ReadonlyArray<File> = [libFile, ...core, ...logic, ...tests, ...ui];
121-
const testProjectExpectedWatchedFiles = [core[0], core[1], core[2]!, ...logic, ...tests].map(f => f.path.toLowerCase()); // tslint:disable-line no-unnecessary-type-assertion (TODO: type assertion should be necessary)
122-
const testProjectExpectedWatchedDirectoriesRecursive = [projectPath(SubProject.core), projectPath(SubProject.logic)];
116+
let core: SubProjectFiles;
117+
let logic: SubProjectFiles;
118+
let tests: SubProjectFiles;
119+
let ui: SubProjectFiles;
120+
let allFiles: ReadonlyArray<File>;
121+
let testProjectExpectedWatchedFiles: string[];
122+
let testProjectExpectedWatchedDirectoriesRecursive: string[];
123+
124+
before(() => {
125+
core = subProjectFiles(SubProject.core, /*anotherModuleAndSomeDecl*/ true);
126+
logic = subProjectFiles(SubProject.logic);
127+
tests = subProjectFiles(SubProject.tests);
128+
ui = subProjectFiles(SubProject.ui);
129+
allFiles = [libFile, ...core, ...logic, ...tests, ...ui];
130+
testProjectExpectedWatchedFiles = [core[0], core[1], core[2]!, ...logic, ...tests].map(f => f.path.toLowerCase()); // tslint:disable-line no-unnecessary-type-assertion (TODO: type assertion should be necessary)
131+
testProjectExpectedWatchedDirectoriesRecursive = [projectPath(SubProject.core), projectPath(SubProject.logic)];
132+
});
133+
134+
after(() => {
135+
core = undefined!;
136+
logic = undefined!;
137+
tests = undefined!;
138+
ui = undefined!;
139+
allFiles = undefined!;
140+
testProjectExpectedWatchedFiles = undefined!;
141+
testProjectExpectedWatchedDirectoriesRecursive = undefined!;
142+
});
123143

124144
function createSolutionInWatchMode(allFiles: ReadonlyArray<File>, defaultOptions?: BuildOptions, disableConsoleClears?: boolean) {
125145
const host = createTsBuildWatchSystem(allFiles, { currentDirectory: projectsLocation });
@@ -172,9 +192,9 @@ namespace ts.tscWatch {
172192
content: `export const newFileConst = 30;`
173193
};
174194

175-
function verifyProjectChanges(allFiles: ReadonlyArray<File>) {
195+
function verifyProjectChanges(allFilesGetter: () => ReadonlyArray<File>) {
176196
function createSolutionInWatchModeToVerifyChanges(additionalFiles?: ReadonlyArray<[SubProject, string]>) {
177-
const host = createSolutionInWatchMode(allFiles);
197+
const host = createSolutionInWatchMode(allFilesGetter());
178198
return { host, verifyChangeWithFile, verifyChangeAfterTimeout, verifyWatches };
179199

180200
function verifyChangeWithFile(fileName: string, content: string, local?: boolean) {
@@ -277,19 +297,21 @@ export class someClass2 { }`);
277297
}
278298

279299
describe("with simple project reference graph", () => {
280-
verifyProjectChanges(allFiles);
300+
verifyProjectChanges(() => allFiles);
281301
});
282302

283303
describe("with circular project reference", () => {
284-
const [coreTsconfig, ...otherCoreFiles] = core;
285-
const circularCoreConfig: File = {
286-
path: coreTsconfig.path,
287-
content: JSON.stringify({
288-
compilerOptions: { composite: true, declaration: true },
289-
references: [{ path: "../tests", circular: true }]
290-
})
291-
};
292-
verifyProjectChanges([libFile, circularCoreConfig, ...otherCoreFiles, ...logic, ...tests]);
304+
verifyProjectChanges(() => {
305+
const [coreTsconfig, ...otherCoreFiles] = core;
306+
const circularCoreConfig: File = {
307+
path: coreTsconfig.path,
308+
content: JSON.stringify({
309+
compilerOptions: { composite: true, declaration: true },
310+
references: [{ path: "../tests", circular: true }]
311+
})
312+
};
313+
return [libFile, circularCoreConfig, ...otherCoreFiles, ...logic, ...tests];
314+
});
293315
});
294316
});
295317

@@ -681,9 +703,9 @@ let x: string = 10;`);
681703
const coreIndexDts = projectFileName(SubProject.core, "index.d.ts");
682704
const coreAnotherModuleDts = projectFileName(SubProject.core, "anotherModule.d.ts");
683705
const logicIndexDts = projectFileName(SubProject.logic, "index.d.ts");
684-
const expectedWatchedFiles = [core[0], logic[0], ...tests, libFile].map(f => f.path).concat([coreIndexDts, coreAnotherModuleDts, logicIndexDts].map(f => f.toLowerCase()));
706+
const expectedWatchedFiles = () => [core[0], logic[0], ...tests, libFile].map(f => f.path).concat([coreIndexDts, coreAnotherModuleDts, logicIndexDts].map(f => f.toLowerCase()));
685707
const expectedWatchedDirectoriesRecursive = projectSystem.getTypeRootsFromLocation(projectPath(SubProject.tests));
686-
const expectedProgramFiles = [tests[1].path, libFile.path, coreIndexDts, coreAnotherModuleDts, logicIndexDts];
708+
const expectedProgramFiles = () => [tests[1].path, libFile.path, coreIndexDts, coreAnotherModuleDts, logicIndexDts];
687709

688710
function createSolutionAndWatchMode() {
689711
return createSolutionAndWatchModeOfProject(allFiles, projectsLocation, `${project}/${SubProject.tests}`, tests[0].path, getOutputFileStamps);
@@ -694,12 +716,12 @@ let x: string = 10;`);
694716
}
695717

696718
function verifyWatches(host: TsBuildWatchSystem, withTsserver?: boolean) {
697-
verifyWatchesOfProject(host, withTsserver ? expectedWatchedFiles.filter(f => f !== tests[1].path.toLowerCase()) : expectedWatchedFiles, expectedWatchedDirectoriesRecursive);
719+
verifyWatchesOfProject(host, withTsserver ? expectedWatchedFiles().filter(f => f !== tests[1].path.toLowerCase()) : expectedWatchedFiles(), expectedWatchedDirectoriesRecursive);
698720
}
699721

700722
function verifyScenario(
701723
edit: (host: TsBuildWatchSystem, solutionBuilder: SolutionBuilder<EmitAndSemanticDiagnosticsBuilderProgram>) => void,
702-
expectedFilesAfterEdit: ReadonlyArray<string>
724+
expectedFilesAfterEdit: () => ReadonlyArray<string>
703725
) {
704726
it("with tsc-watch", () => {
705727
const { host, solutionBuilder, watch } = createSolutionAndWatchMode();
@@ -708,7 +730,7 @@ let x: string = 10;`);
708730

709731
host.checkTimeoutQueueLengthAndRun(1);
710732
checkOutputErrorsIncremental(host, emptyArray);
711-
checkProgramActualFiles(watch(), expectedFilesAfterEdit);
733+
checkProgramActualFiles(watch(), expectedFilesAfterEdit());
712734

713735
});
714736

@@ -718,7 +740,7 @@ let x: string = 10;`);
718740
edit(host, solutionBuilder);
719741

720742
host.checkTimeoutQueueLengthAndRun(2);
721-
checkProjectActualFiles(service, tests[0].path, [tests[0].path, ...expectedFilesAfterEdit]);
743+
checkProjectActualFiles(service, tests[0].path, [tests[0].path, ...expectedFilesAfterEdit()]);
722744
});
723745
}
724746

@@ -729,7 +751,7 @@ let x: string = 10;`);
729751
verifyDependencies(watch, coreIndexDts, [coreIndexDts]);
730752
verifyDependencies(watch, coreAnotherModuleDts, [coreAnotherModuleDts]);
731753
verifyDependencies(watch, logicIndexDts, [logicIndexDts, coreAnotherModuleDts]);
732-
verifyDependencies(watch, tests[1].path, expectedProgramFiles.filter(f => f !== libFile.path));
754+
verifyDependencies(watch, tests[1].path, expectedProgramFiles().filter(f => f !== libFile.path));
733755
});
734756

735757
it("with tsserver", () => {
@@ -769,7 +791,7 @@ export function gfoo() {
769791
}));
770792
solutionBuilder.invalidateProject(logic[0].path.toLowerCase() as ResolvedConfigFilePath, ConfigFileProgramReloadLevel.Full);
771793
solutionBuilder.buildNextInvalidatedProject();
772-
}, [tests[1].path, libFile.path, coreIndexDts, coreAnotherModuleDts, projectFilePath(SubProject.logic, "decls/index.d.ts")]);
794+
}, () => [tests[1].path, libFile.path, coreIndexDts, coreAnotherModuleDts, projectFilePath(SubProject.logic, "decls/index.d.ts")]);
773795
});
774796
});
775797

0 commit comments

Comments
 (0)