1namespace ts.projectSystem { 2 const packageJson: File = { 3 path: "/package.json", 4 content: `{ "dependencies": { "mobx": "*" } }` 5 }; 6 const aTs: File = { 7 path: "/a.ts", 8 content: "export const foo = 0;", 9 }; 10 const bTs: File = { 11 path: "/b.ts", 12 content: "foo", 13 }; 14 const tsconfig: File = { 15 path: "/tsconfig.json", 16 content: "{}", 17 }; 18 const ambientDeclaration: File = { 19 path: "/ambient.d.ts", 20 content: "declare module 'ambient' {}" 21 }; 22 const mobxDts: File = { 23 path: "/node_modules/mobx/index.d.ts", 24 content: "export declare function observable(): unknown;" 25 }; 26 27 describe("unittests:: tsserver:: importSuggestionsCache", () => { 28 it("caches auto-imports in the same file", () => { 29 const { importSuggestionsCache, checker } = setup(); 30 assert.ok(importSuggestionsCache.get(bTs.path, checker)); 31 }); 32 33 it("invalidates the cache when new files are added", () => { 34 const { host, importSuggestionsCache, checker } = setup(); 35 host.writeFile("/src/a2.ts", aTs.content); 36 host.runQueuedTimeoutCallbacks(); 37 assert.isUndefined(importSuggestionsCache.get(bTs.path, checker)); 38 }); 39 40 it("invalidates the cache when files are deleted", () => { 41 const { host, projectService, importSuggestionsCache, checker } = setup(); 42 projectService.closeClientFile(aTs.path); 43 host.deleteFile(aTs.path); 44 host.runQueuedTimeoutCallbacks(); 45 assert.isUndefined(importSuggestionsCache.get(bTs.path, checker)); 46 }); 47 48 it("invalidates the cache when package.json is changed", () => { 49 const { host, importSuggestionsCache, checker } = setup(); 50 host.writeFile("/package.json", "{}"); 51 host.runQueuedTimeoutCallbacks(); 52 assert.isUndefined(importSuggestionsCache.get(bTs.path, checker)); 53 }); 54 }); 55 56 function setup() { 57 const host = createServerHost([aTs, bTs, ambientDeclaration, tsconfig, packageJson, mobxDts]); 58 const session = createSession(host); 59 openFilesForSession([aTs, bTs], session); 60 const projectService = session.getProjectService(); 61 const project = configuredProjectAt(projectService, 0); 62 const requestLocation: protocol.FileLocationRequestArgs = { 63 file: bTs.path, 64 line: 1, 65 offset: 3, 66 }; 67 executeSessionRequest<protocol.CompletionsRequest, protocol.CompletionInfoResponse>(session, protocol.CommandTypes.CompletionInfo, { 68 ...requestLocation, 69 includeExternalModuleExports: true, 70 prefix: "foo", 71 }); 72 const checker = project.getLanguageService().getProgram()!.getTypeChecker(); 73 return { host, project, projectService, importSuggestionsCache: project.getImportSuggestionsCache(), checker }; 74 } 75} 76