Skip to content

Commit 1fd0a8c

Browse files
authored
added unittest for watching @types (#11027)
added unittest for watching @types * remove debugger statement, fix linter issues
1 parent 3c4786d commit 1fd0a8c

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

Jakefile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ desc("Runs the tests using the built run.js file like 'jake runtests'. Syntax is
937937
task("runtests-browser", ["tests", "browserify", builtLocalDirectory, servicesFileInBrowserTest], function () {
938938
cleanTestDirs();
939939
host = "node";
940-
browser = process.env.browser || process.env.b || "IE";
940+
browser = process.env.browser || process.env.b || (os.platform() === "linux" ? "chrome" : "IE");
941941
tests = process.env.test || process.env.tests || process.env.t;
942942
var light = process.env.light || false;
943943
var testConfigFile = 'test.config';

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,7 @@ namespace ts.projectSystem {
19391939
content: JSON.stringify({ compilerOptions: { allowJs: true }, exclude: ["node_modules"] })
19401940
};
19411941
const host = createServerHost([f1, barjs, barTypings, config]);
1942-
const projectService = createProjectService(host, { typingsInstaller: new TestTypingsInstaller(typingsCacheLocation, host) });
1942+
const projectService = createProjectService(host, { typingsInstaller: new TestTypingsInstaller(typingsCacheLocation, /*throttleLimit*/ 5, host) });
19431943

19441944
projectService.openClientFile(f1.path);
19451945
projectService.checkNumberOfProjects({ configuredProjects: 1 });
@@ -1987,4 +1987,52 @@ namespace ts.projectSystem {
19871987
assert.deepEqual(s3, newPerFileSettings, "file settings should still be the same with per-file settings");
19881988
});
19891989
});
1990+
1991+
describe("watching @types", () => {
1992+
it("works correctly when typings are added or removed", () => {
1993+
const f1 = {
1994+
path: "/a/b/app.ts",
1995+
content: "let x = 1;"
1996+
};
1997+
const t1 = {
1998+
path: "/a/b/node_modules/@types/lib1/index.d.ts",
1999+
content: "export let a: number"
2000+
};
2001+
const t2 = {
2002+
path: "/a/b/node_modules/@types/lib2/index.d.ts",
2003+
content: "export let b: number"
2004+
};
2005+
const tsconfig = {
2006+
path: "/a/b/tsconfig.json",
2007+
content: JSON.stringify({
2008+
compilerOptions: {},
2009+
exclude: ["node_modules"]
2010+
})
2011+
};
2012+
const host = createServerHost([f1, t1, tsconfig]);
2013+
const projectService = createProjectService(host);
2014+
2015+
projectService.openClientFile(f1.path);
2016+
projectService.checkNumberOfProjects({ configuredProjects: 1 });
2017+
checkProjectActualFiles(projectService.configuredProjects[0], [f1.path, t1.path]);
2018+
2019+
// delete t1
2020+
host.reloadFS([f1, tsconfig]);
2021+
host.triggerDirectoryWatcherCallback("/a/b/node_modules/@types", "lib1");
2022+
// run throttled operation
2023+
host.runQueuedTimeoutCallbacks();
2024+
2025+
projectService.checkNumberOfProjects({ configuredProjects: 1 });
2026+
checkProjectActualFiles(projectService.configuredProjects[0], [f1.path]);
2027+
2028+
// create t2
2029+
host.reloadFS([f1, tsconfig, t2]);
2030+
host.triggerDirectoryWatcherCallback("/a/b/node_modules/@types", "lib2");
2031+
// run throttled operation
2032+
host.runQueuedTimeoutCallbacks();
2033+
2034+
projectService.checkNumberOfProjects({ configuredProjects: 1 });
2035+
checkProjectActualFiles(projectService.configuredProjects[0], [f1.path, t2.path]);
2036+
});
2037+
});
19902038
}

src/services/services.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3154,6 +3154,13 @@ namespace ts {
31543154
}
31553155
}
31563156

3157+
const typeRootsVersion = host.getTypeRootsVersion ? host.getTypeRootsVersion() : 0;
3158+
if (lastTypesRootVersion !== typeRootsVersion) {
3159+
log("TypeRoots version has changed; provide new program");
3160+
program = undefined;
3161+
lastTypesRootVersion = typeRootsVersion;
3162+
}
3163+
31573164
// Get a fresh cache of the host information
31583165
let hostCache = new HostCache(host, getCanonicalFileName);
31593166

@@ -3221,13 +3228,6 @@ namespace ts {
32213228
};
32223229
}
32233230

3224-
const typeRootsVersion = host.getTypeRootsVersion ? host.getTypeRootsVersion() : 0;
3225-
if (lastTypesRootVersion !== typeRootsVersion) {
3226-
log("TypeRoots version has changed; provide new program");
3227-
program = undefined;
3228-
lastTypesRootVersion = typeRootsVersion;
3229-
}
3230-
32313231
const documentRegistryBucketKey = documentRegistry.getKeyForCompilationSettings(newSettings);
32323232
const newProgram = createProgram(hostCache.getRootFileNames(), newSettings, compilerHost, program);
32333233

0 commit comments

Comments
 (0)