• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1namespace ts.projectSystem {
2    describe("unittests:: tsserver:: moduleResolution", () => {
3        describe("package json file is edited", () => {
4            function setup(packageFileContents: string) {
5                const configFile: File = {
6                    path: `${tscWatch.projectRoot}/src/tsconfig.json`,
7                    content: JSON.stringify({
8                        compilerOptions: {
9                            target: "es2016",
10                            module: "Node16",
11                            outDir: "../out",
12                            traceResolution: true,
13                        }
14                    })
15                };
16                const packageFile: File = {
17                    path: `${tscWatch.projectRoot}/package.json`,
18                    content: packageFileContents
19                };
20                const fileA: File = {
21                    path: `${tscWatch.projectRoot}/src/fileA.ts`,
22                    content: Utils.dedent`
23                        import { foo } from "./fileB.mjs";
24                        foo();
25                    `
26                };
27                const fileB: File = {
28                    path: `${tscWatch.projectRoot}/src/fileB.mts`,
29                    content: Utils.dedent`
30                        export function foo() {
31                        }
32                    `
33                };
34                const host = createServerHost([configFile, fileA, fileB, packageFile, { ...libFile, path: "/a/lib/lib.es2016.full.d.ts" }]);
35                const session = createSession(host, { canUseEvents: true, logger: createLoggerWithInMemoryLogs(host) });
36                openFilesForSession([fileA], session);
37                return {
38                    host, session, packageFile,
39                    verifyErr: () => verifyGetErrRequest({ files: [fileA], session, host }),
40                };
41            }
42            it("package json file is edited", () => {
43                const { host, session, packageFile, verifyErr } = setup(JSON.stringify({ name: "app", version: "1.0.0" }));
44
45                session.logger.info("Modify package json file to add type module");
46                host.writeFile(packageFile.path, JSON.stringify({
47                    name: "app", version: "1.0.0", type: "module",
48                }));
49                host.runQueuedTimeoutCallbacks(); // Failed lookup updates
50                host.runQueuedTimeoutCallbacks(); // Actual update
51                verifyErr();
52
53                session.logger.info("Modify package json file to remove type module");
54                host.writeFile(packageFile.path, packageFile.content);
55                host.runQueuedTimeoutCallbacks(); // Failed lookup updates
56                host.runQueuedTimeoutCallbacks(); // Actual update
57                verifyErr();
58
59                session.logger.info("Delete package.json");
60                host.deleteFile(packageFile.path);
61                host.runQueuedTimeoutCallbacks(); // Failed lookup updates
62                host.runQueuedTimeoutCallbacks(); // Actual update
63                verifyErr();
64
65                session.logger.info("Modify package json file to add type module");
66                host.writeFile(packageFile.path, JSON.stringify({
67                    name: "app", version: "1.0.0", type: "module",
68                }));
69                host.runQueuedTimeoutCallbacks(); // Failed lookup updates
70                host.runQueuedTimeoutCallbacks(); // Actual update
71                verifyErr();
72
73                session.logger.info("Delete package.json");
74                host.deleteFile(packageFile.path);
75                host.runQueuedTimeoutCallbacks(); // Failed lookup updates
76                host.runQueuedTimeoutCallbacks(); // Actual update
77                verifyErr();
78
79                baselineTsserverLogs("moduleResolution", "package json file is edited", session);
80            });
81
82            it("package json file is edited when package json with type module exists", () => {
83                const { host, session, packageFile, verifyErr } = setup(JSON.stringify({
84                    name: "app", version: "1.0.0", type: "module",
85                }));
86
87                session.logger.info("Modify package json file to remove type module");
88                host.writeFile(packageFile.path, JSON.stringify({ name: "app", version: "1.0.0" }));
89                host.runQueuedTimeoutCallbacks(); // Failed lookup updates
90                host.runQueuedTimeoutCallbacks(); // Actual update
91                verifyErr();
92
93                session.logger.info("Modify package json file to add type module");
94                host.writeFile(packageFile.path, packageFile.content);
95                host.runQueuedTimeoutCallbacks(); // Failed lookup updates
96                host.runQueuedTimeoutCallbacks(); // Actual update
97                verifyErr();
98
99                session.logger.info("Delete package.json");
100                host.deleteFile(packageFile.path);
101                host.runQueuedTimeoutCallbacks(); // Failed lookup updates
102                host.runQueuedTimeoutCallbacks(); // Actual update
103                verifyErr();
104
105                session.logger.info("Modify package json file to without type module");
106                host.writeFile(packageFile.path, JSON.stringify({ name: "app", version: "1.0.0" }));
107                host.runQueuedTimeoutCallbacks(); // Failed lookup updates
108                host.runQueuedTimeoutCallbacks(); // Actual update
109                verifyErr();
110
111                session.logger.info("Delete package.json");
112                host.deleteFile(packageFile.path);
113                host.runQueuedTimeoutCallbacks(); // Failed lookup updates
114                host.runQueuedTimeoutCallbacks(); // Actual update
115                verifyErr();
116
117                baselineTsserverLogs("moduleResolution", "package json file is edited when package json with type module exists", session);
118            });
119        });
120    });
121}