Skip to content

Commit ab17600

Browse files
committed
Improve test to verify the count of callbacks for the watched directories through watchFile
1 parent c3db9fa commit ab17600

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

src/harness/unittests/tscWatchMode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2210,7 +2210,7 @@ declare module "fs" {
22102210
checkWatchedDirectories(host, emptyArray, /*recursive*/ true);
22112211

22122212
// Watching config file, file, lib file and directories
2213-
checkWatchedFiles(host, expectedWatchedFiles);
2213+
ts.TestFSWithWatch.checkMultiMapEachKeyWithCount("watchedFiles", host.watchedFiles, expectedWatchedFiles, 1);
22142214

22152215
checkProgramActualFiles(watch(), programFiles.map(f => f.path));
22162216
checkOutputErrors(host, emptyArray);

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5322,16 +5322,11 @@ namespace ts.projectSystem {
53225322
}
53235323

53245324
function verifyCalledOnEachEntry(callback: CalledMaps, expectedKeys: Map<number>) {
5325-
const calledMap = calledMaps[callback];
5326-
ts.TestFSWithWatch.verifyMapSize(callback, calledMap, arrayFrom(expectedKeys.keys()));
5327-
expectedKeys.forEach((called, name) => {
5328-
assert.isTrue(calledMap.has(name), `${callback} is expected to contain ${name}, actual keys: ${arrayFrom(calledMap.keys())}`);
5329-
assert.equal(calledMap.get(name).length, called, `${callback} is expected to be called ${called} times with ${name}. Actual entry: ${calledMap.get(name)}`);
5330-
});
5325+
ts.TestFSWithWatch.checkMultiMapKeyCount(callback, calledMaps[callback], expectedKeys);
53315326
}
53325327

53335328
function verifyCalledOnEachEntryNTimes(callback: CalledMaps, expectedKeys: string[], nTimes: number) {
5334-
return verifyCalledOnEachEntry(callback, zipToMap(expectedKeys, expectedKeys.map(() => nTimes)));
5329+
ts.TestFSWithWatch.checkMultiMapEachKeyWithCount(callback, calledMaps[callback], expectedKeys, nTimes);
53355330
}
53365331

53375332
function verifyNoHostCalls() {
@@ -6605,7 +6600,11 @@ namespace ts.projectSystem {
66056600
const files = [index, file1, configFile, libFile];
66066601
const fileNames = files.map(file => file.path);
66076602
// All closed files(files other than index), project folder, project/src folder and project/node_modules/@types folder
6608-
const expectedWatchedFiles = fileNames.slice(1).concat(projectFolder, projectSrcFolder, `${projectFolder}/${nodeModulesAtTypes}`);
6603+
const expectedWatchedFiles = arrayToMap(fileNames.slice(1).concat(`${projectFolder}/${nodeModulesAtTypes}`), s => s, () => 1);
6604+
// For failed resolution lookup and tsconfig files
6605+
expectedWatchedFiles.set(projectFolder, 2);
6606+
// Through above recursive watches
6607+
expectedWatchedFiles.set(projectSrcFolder, 2);
66096608
const expectedCompletions = ["file1"];
66106609
const completionPosition = index.content.lastIndexOf('"');
66116610
const environmentVariables = createMap<string>();
@@ -6625,7 +6624,7 @@ namespace ts.projectSystem {
66256624
};
66266625
files.push(file2);
66276626
fileNames.push(file2.path);
6628-
expectedWatchedFiles.push(file2.path);
6627+
expectedWatchedFiles.set(file2.path, 1);
66296628
expectedCompletions.push("file2");
66306629
host.reloadFS(files);
66316630
host.runQueuedTimeoutCallbacks();
@@ -6635,7 +6634,8 @@ namespace ts.projectSystem {
66356634
function verifyProjectAndCompletions() {
66366635
checkWatchedDirectories(host, emptyArray, /*recursive*/ false);
66376636
checkWatchedDirectories(host, emptyArray, /*recursive*/ true);
6638-
checkWatchedFiles(host, expectedWatchedFiles);
6637+
6638+
ts.TestFSWithWatch.checkMultiMapKeyCount("watchedFiles", host.watchedFiles, expectedWatchedFiles);
66396639
checkProjectActualFiles(project, fileNames);
66406640

66416641
const completions = project.getLanguageService().getCompletionsAtPosition(index.path, completionPosition, { includeExternalModuleExports: false, includeInsertTextCompletions: false });

src/harness/virtualFileSystemWithWatch.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ interface Array<T> {}`
147147
}
148148
}
149149

150+
export function checkMultiMapKeyCount(caption: string, actual: MultiMap<any>, expectedKeys: Map<number>) {
151+
verifyMapSize(caption, actual, arrayFrom(expectedKeys.keys()));
152+
expectedKeys.forEach((count, name) => {
153+
assert.isTrue(actual.has(name), `${caption}: expected to contain ${name}, actual keys: ${arrayFrom(actual.keys())}`);
154+
assert.equal(actual.get(name).length, count, `${caption}: Expected to be have ${count} entries for ${name}. Actual entry: ${JSON.stringify(actual.get(name))}`);
155+
});
156+
}
157+
158+
export function checkMultiMapEachKeyWithCount(caption: string, actual: MultiMap<any>, expectedKeys: ReadonlyArray<string>, count: number) {
159+
return checkMultiMapKeyCount(caption, actual, arrayToMap(expectedKeys, s => s, () => count));
160+
}
161+
150162
export function checkArray(caption: string, actual: ReadonlyArray<string>, expected: ReadonlyArray<string>) {
151163
assert.equal(actual.length, expected.length, `${caption}: incorrect actual number of files, expected:\r\n${expected.join("\r\n")}\r\ngot: ${actual.join("\r\n")}`);
152164
for (const f of expected) {

0 commit comments

Comments
 (0)