• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1namespace ts.tscWatch {
2    describe("unittests:: tsbuildWatch:: watchMode:: moduleResolution", () => {
3        verifyTscWatch({
4            scenario: "moduleResolutionCache",
5            subScenario: "handles the cache correctly when two projects use different module resolution settings",
6            sys: () => createWatchedSystem(
7                [
8                    { path: `${projectRoot}/project1/index.ts`, content: `import { foo } from "file";` },
9                    { path: `${projectRoot}/project1/node_modules/file/index.d.ts`, content: "export const foo = 10;" },
10                    {
11                        path: `${projectRoot}/project1/tsconfig.json`,
12                        content: JSON.stringify({
13                            compilerOptions: { composite: true, types: ["foo", "bar"] },
14                            files: ["index.ts"]
15                        })
16                    },
17                    { path: `${projectRoot}/project2/index.ts`, content: `import { foo } from "file";` },
18                    { path: `${projectRoot}/project2/file.d.ts`, content: "export const foo = 10;" },
19                    {
20                        path: `${projectRoot}/project2/tsconfig.json`,
21                        content: JSON.stringify({
22                            compilerOptions: { composite: true, types: ["foo"], moduleResolution: "classic" },
23                            files: ["index.ts"]
24                        })
25                    },
26                    { path: `${projectRoot}/node_modules/@types/foo/index.d.ts`, content: "export const foo = 10;" },
27                    { path: `${projectRoot}/node_modules/@types/bar/index.d.ts`, content: "export const bar = 10;" },
28                    {
29                        path: `${projectRoot}/tsconfig.json`,
30                        content: JSON.stringify({
31                            files: [],
32                            references: [
33                                { path: "./project1" },
34                                { path: "./project2" }
35                            ]
36                        })
37                    },
38                    libFile
39                ],
40                { currentDirectory: projectRoot }
41            ),
42            commandLineArgs: ["--b", "-w", "-v"],
43            changes: [
44                {
45                    caption: "Append text",
46                    change: sys => sys.appendFile(`${projectRoot}/project1/index.ts`, "const bar = 10;"),
47                    timeouts: sys => {
48                        sys.checkTimeoutQueueLengthAndRun(1); // build project1 and solution
49                        sys.checkTimeoutQueueLength(0);
50                    }
51                },
52            ]
53        });
54
55        verifyTscWatch({
56            scenario: "moduleResolution",
57            subScenario: `resolves specifier in output declaration file from referenced project correctly with cts and mts extensions`,
58            sys: () => createWatchedSystem([
59                {
60                    path: `${projectRoot}/packages/pkg1/package.json`,
61                    content: JSON.stringify({
62                        name: "pkg1",
63                        version: "1.0.0",
64                        main: "build/index.js",
65                        type: "module"
66                    })
67                },
68                {
69                    path: `${projectRoot}/packages/pkg1/index.ts`,
70                    content: Utils.dedent`
71                import type { TheNum } from 'pkg2'
72                export const theNum: TheNum = 42;`
73                },
74                {
75                    path: `${projectRoot}/packages/pkg1/tsconfig.json`,
76                    content: JSON.stringify({
77                        compilerOptions: {
78                            outDir: "build",
79                            module: "node16",
80                        },
81                        references: [{ path: "../pkg2" }]
82                    })
83                },
84                {
85                    path: `${projectRoot}/packages/pkg2/const.cts`,
86                    content: `export type TheNum = 42;`
87                },
88                {
89                    path: `${projectRoot}/packages/pkg2/index.ts`,
90                    content: `export type { TheNum } from './const.cjs';`
91                },
92                {
93                    path: `${projectRoot}/packages/pkg2/tsconfig.json`,
94                    content: JSON.stringify({
95                        compilerOptions: {
96                            composite: true,
97                            outDir: "build",
98                            module: "node16",
99                        }
100                    })
101                },
102                {
103                    path: `${projectRoot}/packages/pkg2/package.json`,
104                    content: JSON.stringify({
105                        name: "pkg2",
106                        version: "1.0.0",
107                        main: "build/index.js",
108                        type: "module"
109                    })
110                },
111                {
112                    path: `${projectRoot}/node_modules/pkg2`,
113                    symLink: `${projectRoot}/packages/pkg2`,
114                },
115                { ...libFile, path: `/a/lib/lib.es2022.full.d.ts` }
116            ], { currentDirectory: projectRoot }),
117            commandLineArgs: ["-b", "packages/pkg1", "-w", "--verbose", "--traceResolution"],
118            changes: [
119                {
120                    caption: "reports import errors after change to package file",
121                    change: sys => replaceFileText(sys, `${projectRoot}/packages/pkg1/package.json`, `"module"`, `"commonjs"`),
122                    timeouts: runQueuedTimeoutCallbacks
123                },
124                {
125                    caption: "removes those errors when a package file is changed back",
126                    change: sys => replaceFileText(sys, `${projectRoot}/packages/pkg1/package.json`, `"commonjs"`, `"module"`),
127                    timeouts: runQueuedTimeoutCallbacks,
128                },
129                {
130                    caption: "reports import errors after change to package file",
131                    change: sys => replaceFileText(sys, `${projectRoot}/packages/pkg1/package.json`, `"module"`, `"commonjs"`),
132                    timeouts: runQueuedTimeoutCallbacks
133                },
134                {
135                    caption: "removes those errors when a package file is changed to cjs extensions",
136                    change: sys => {
137                        replaceFileText(sys, `${projectRoot}/packages/pkg2/package.json`, `"build/index.js"`, `"build/index.cjs"`);
138                        sys.renameFile(`${projectRoot}/packages/pkg2/index.ts`, `${projectRoot}/packages/pkg2/index.cts`);
139                    },
140                    timeouts: sys => {
141                        sys.runQueuedTimeoutCallbacks(); // building pkg2
142                        sys.runQueuedTimeoutCallbacks(); // building pkg1
143                    },
144                },
145            ]
146        });
147
148        verifyTscWatch({
149            scenario: "moduleResolution",
150            subScenario: `build mode watches for changes to package-json main fields`,
151            sys: () => createWatchedSystem([
152                {
153                    path: `${projectRoot}/packages/pkg1/package.json`,
154                    content: JSON.stringify({
155                        name: "pkg1",
156                        version: "1.0.0",
157                        main: "build/index.js",
158                    })
159                },
160                {
161                    path: `${projectRoot}/packages/pkg1/index.ts`,
162                    content: Utils.dedent`
163                    import type { TheNum } from 'pkg2'
164                    export const theNum: TheNum = 42;`
165                },
166                {
167                    path: `${projectRoot}/packages/pkg1/tsconfig.json`,
168                    content: JSON.stringify({
169                        compilerOptions: {
170                            outDir: "build",
171                        },
172                        references: [{ path: "../pkg2" }]
173                    })
174                },
175                {
176                    path: `${projectRoot}/packages/pkg2/tsconfig.json`,
177                    content: JSON.stringify({
178                        compilerOptions: {
179                            composite: true,
180                            outDir: "build",
181                            baseUrl: ".",
182                        }
183                    })
184                },
185                {
186                    path: `${projectRoot}/packages/pkg2/const.ts`,
187                    content: `export type TheNum = 42;`
188                },
189                {
190                    path: `${projectRoot}/packages/pkg2/index.ts`,
191                    content: `export type { TheNum } from './const.js';`
192                },
193                {
194                    path: `${projectRoot}/packages/pkg2/other.ts`,
195                    content: `export type TheStr = string;`
196                },
197                {
198                    path: `${projectRoot}/packages/pkg2/package.json`,
199                    content: JSON.stringify({
200                        name: "pkg2",
201                        version: "1.0.0",
202                        main: "build/index.js",
203                    })
204                },
205                {
206                    path: `${projectRoot}/node_modules/pkg2`,
207                    symLink: `${projectRoot}/packages/pkg2`,
208                },
209                libFile
210            ], { currentDirectory: projectRoot }),
211            commandLineArgs: ["-b", "packages/pkg1", "--verbose", "-w", "--traceResolution"],
212            changes: [
213                {
214                    caption: "reports import errors after change to package file",
215                    change: sys => replaceFileText(sys, `${projectRoot}/packages/pkg2/package.json`, `index.js`, `other.js`),
216                    timeouts: runQueuedTimeoutCallbacks,
217                },
218                {
219                    caption: "removes those errors when a package file is changed back",
220                    change: sys => replaceFileText(sys, `${projectRoot}/packages/pkg2/package.json`, `other.js`, `index.js`),
221                    timeouts: runQueuedTimeoutCallbacks,
222                },
223            ]
224        });
225    });
226}