|
1 | 1 | namespace ts.tscWatch { |
2 | 2 | describe("unittests:: tsc-watch:: watchAPI:: tsc-watch with custom module resolution", () => { |
3 | | - const configFileJson: any = { |
4 | | - compilerOptions: { module: "commonjs", resolveJsonModule: true }, |
5 | | - files: ["index.ts"] |
6 | | - }; |
7 | | - const mainFile: File = { |
8 | | - path: `${projectRoot}/index.ts`, |
9 | | - content: "import settings from './settings.json';" |
10 | | - }; |
11 | | - const config: File = { |
12 | | - path: `${projectRoot}/tsconfig.json`, |
13 | | - content: JSON.stringify(configFileJson) |
14 | | - }; |
15 | | - const settingsJson: File = { |
16 | | - path: `${projectRoot}/settings.json`, |
17 | | - content: JSON.stringify({ content: "Print this" }) |
18 | | - }; |
19 | | - |
20 | 3 | it("verify that module resolution with json extension works when returned without extension", () => { |
| 4 | + const configFileJson: any = { |
| 5 | + compilerOptions: { module: "commonjs", resolveJsonModule: true }, |
| 6 | + files: ["index.ts"] |
| 7 | + }; |
| 8 | + const mainFile: File = { |
| 9 | + path: `${projectRoot}/index.ts`, |
| 10 | + content: "import settings from './settings.json';" |
| 11 | + }; |
| 12 | + const config: File = { |
| 13 | + path: `${projectRoot}/tsconfig.json`, |
| 14 | + content: JSON.stringify(configFileJson) |
| 15 | + }; |
| 16 | + const settingsJson: File = { |
| 17 | + path: `${projectRoot}/settings.json`, |
| 18 | + content: JSON.stringify({ content: "Print this" }) |
| 19 | + }; |
21 | 20 | const { sys, baseline, oldSnap, cb, getPrograms } = createBaseline(createWatchedSystem( |
22 | 21 | [libFile, mainFile, config, settingsJson], |
23 | 22 | { currentDirectory: projectRoot }), |
@@ -50,6 +49,64 @@ namespace ts.tscWatch { |
50 | 49 | watchOrSolution: watch |
51 | 50 | }); |
52 | 51 | }); |
| 52 | + |
| 53 | + describe("hasInvalidatedResolution", () => { |
| 54 | + function verifyWatch(subScenario: string, implementHasInvalidatedResolution: boolean) { |
| 55 | + it(subScenario, () => { |
| 56 | + const { sys, baseline, oldSnap, cb, getPrograms } = createBaseline(createWatchedSystem({ |
| 57 | + [`${projectRoot}/tsconfig.json`]: JSON.stringify({ |
| 58 | + compilerOptions: { traceResolution: true, extendedDiagnostics: true }, |
| 59 | + files: ["main.ts"] |
| 60 | + }), |
| 61 | + [`${projectRoot}/main.ts`]: `import { foo } from "./other";`, |
| 62 | + [`${projectRoot}/other.d.ts`]: "export function foo(): void;", |
| 63 | + [libFile.path]: libFile.content, |
| 64 | + }, { currentDirectory: projectRoot })); |
| 65 | + const host = createWatchCompilerHostOfConfigFileForBaseline({ |
| 66 | + configFileName: `${projectRoot}/tsconfig.json`, |
| 67 | + system: sys, |
| 68 | + cb, |
| 69 | + }); |
| 70 | + host.resolveModuleNames = (moduleNames, containingFile, _reusedNames, _redirectedReference, options) => |
| 71 | + moduleNames.map(m => resolveModuleName(m, containingFile, options, host).resolvedModule); |
| 72 | + // Invalidate resolutions only when ts file is created |
| 73 | + if (implementHasInvalidatedResolution) host.hasInvalidatedResolution = () => sys.fileExists(`${projectRoot}/other.ts`); |
| 74 | + const watch = createWatchProgram(host); |
| 75 | + runWatchBaseline({ |
| 76 | + scenario: "watchApi", |
| 77 | + subScenario, |
| 78 | + commandLineArgs: ["--w"], |
| 79 | + sys, |
| 80 | + baseline, |
| 81 | + oldSnap, |
| 82 | + getPrograms, |
| 83 | + changes: [ |
| 84 | + { |
| 85 | + caption: "write other with same contents", |
| 86 | + change: sys => sys.appendFile(`${projectRoot}/other.d.ts`, ""), |
| 87 | + timeouts: sys => sys.runQueuedTimeoutCallbacks(), |
| 88 | + }, |
| 89 | + { |
| 90 | + caption: "change other file", |
| 91 | + change: sys => sys.appendFile(`${projectRoot}/other.d.ts`, "export function bar(): void;"), |
| 92 | + timeouts: sys => sys.runQueuedTimeoutCallbacks(), |
| 93 | + }, |
| 94 | + { |
| 95 | + caption: "write other with same contents but write ts file", |
| 96 | + change: sys => { |
| 97 | + sys.appendFile(`${projectRoot}/other.d.ts`, ""); |
| 98 | + sys.writeFile(`${projectRoot}/other.ts`, "export function foo() {}"); |
| 99 | + }, |
| 100 | + timeouts: sys => sys.runQueuedTimeoutCallbacks(), |
| 101 | + }, |
| 102 | + ], |
| 103 | + watchOrSolution: watch |
| 104 | + }); |
| 105 | + }); |
| 106 | + } |
| 107 | + verifyWatch("host implements does not implement hasInvalidatedResolution", /*implementHasInvalidatedResolution*/ false); |
| 108 | + verifyWatch("host implements hasInvalidatedResolution", /*implementHasInvalidatedResolution*/ true); |
| 109 | + }); |
53 | 110 | }); |
54 | 111 |
|
55 | 112 | describe("unittests:: tsc-watch:: watchAPI:: tsc-watch expose error count to watch status reporter", () => { |
|
0 commit comments