Skip to content

Commit 6a05abd

Browse files
committed
Add test for tsbuildinfo text verification
1 parent c7b5005 commit 6a05abd

File tree

1 file changed

+56
-9
lines changed

1 file changed

+56
-9
lines changed

src/testRunner/unittests/tscWatch/watchApi.ts

+56-9
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,15 @@ namespace ts.tscWatch {
125125
});
126126

127127
describe("unittests:: tsc-watch:: watchAPI:: when watchHost uses createSemanticDiagnosticsBuilderProgram", () => {
128-
it("verifies that noEmit is handled on createSemanticDiagnosticsBuilderProgram and typechecking happens only on affected files", () => {
128+
function getWatch<T extends BuilderProgram>(config: File, optionsToExtend: CompilerOptions | undefined, sys: System, createProgram: CreateProgram<T>) {
129+
const watchCompilerHost = createWatchCompilerHost(config.path, optionsToExtend, sys, createProgram);
130+
return createWatchProgram(watchCompilerHost);
131+
}
132+
133+
function setup<T extends BuilderProgram>(createProgram: CreateProgram<T>, configText: string) {
129134
const config: File = {
130135
path: `${projectRoot}/tsconfig.json`,
131-
content: "{}"
136+
content: configText
132137
};
133138
const mainFile: File = {
134139
path: `${projectRoot}/main.ts`,
@@ -139,13 +144,11 @@ namespace ts.tscWatch {
139144
content: "export const y = 10;"
140145
};
141146
const sys = createWatchedSystem([config, mainFile, otherFile, libFile]);
142-
const watchCompilerHost = createWatchCompilerHost(
143-
config.path,
144-
{ noEmit: true },
145-
sys,
146-
createSemanticDiagnosticsBuilderProgram
147-
);
148-
const watch = createWatchProgram(watchCompilerHost);
147+
const watch = getWatch(config, { noEmit: true }, sys, createProgram);
148+
return { sys, watch, mainFile, otherFile, config };
149+
}
150+
it("verifies that noEmit is handled on createSemanticDiagnosticsBuilderProgram and typechecking happens only on affected files", () => {
151+
const { sys, watch, mainFile, otherFile } = setup(createSemanticDiagnosticsBuilderProgram, "{}");
149152
checkProgramActualFiles(watch.getProgram().getProgram(), [mainFile.path, otherFile.path, libFile.path]);
150153
sys.appendFile(mainFile.path, "\n// SomeComment");
151154
sys.runQueuedTimeoutCallbacks();
@@ -154,5 +157,49 @@ namespace ts.tscWatch {
154157
// Should not retrieve diagnostics for other file thats not changed
155158
assert.deepEqual(program.getCachedSemanticDiagnostics(program.getSourceFile(otherFile.path)), /*expected*/ undefined);
156159
});
160+
161+
it("noEmit with composite writes the tsbuildinfo with pending affected files correctly", () => {
162+
const configText = JSON.stringify({ compilerOptions: { composite: true } });
163+
const { sys, watch, config, mainFile } = setup(createSemanticDiagnosticsBuilderProgram, configText);
164+
const { sys: emitSys, watch: emitWatch } = setup(createEmitAndSemanticDiagnosticsBuilderProgram, configText);
165+
verifyOutputs(sys, emitSys);
166+
167+
watch.close();
168+
emitWatch.close();
169+
170+
// Emit on both sys should result in same output
171+
verifyBuilder(createEmitAndSemanticDiagnosticsBuilderProgram, createEmitAndSemanticDiagnosticsBuilderProgram);
172+
173+
// Change file
174+
sys.appendFile(mainFile.path, "\n// SomeComment");
175+
emitSys.appendFile(mainFile.path, "\n// SomeComment");
176+
177+
// Verify noEmit results in same output
178+
verifyBuilder(createSemanticDiagnosticsBuilderProgram, createEmitAndSemanticDiagnosticsBuilderProgram, { noEmit: true });
179+
180+
// Emit on both sys should result in same output
181+
verifyBuilder(createEmitAndSemanticDiagnosticsBuilderProgram, createEmitAndSemanticDiagnosticsBuilderProgram);
182+
183+
// Change file
184+
sys.appendFile(mainFile.path, "\n// SomeComment");
185+
emitSys.appendFile(mainFile.path, "\n// SomeComment");
186+
187+
// Emit on both the builders should result in same files
188+
verifyBuilder(createSemanticDiagnosticsBuilderProgram, createEmitAndSemanticDiagnosticsBuilderProgram);
189+
190+
function verifyOutputs(sys: System, emitSys: System) {
191+
for (const output of [`${projectRoot}/main.js`, `${projectRoot}/main.d.ts`, `${projectRoot}/other.js`, `${projectRoot}/other.d.ts`, `${projectRoot}/tsconfig.tsbuildinfo`]) {
192+
assert.strictEqual(sys.readFile(output), emitSys.readFile(output), `Output file text for ${output}`);
193+
}
194+
}
195+
196+
function verifyBuilder<T extends BuilderProgram, U extends BuilderProgram>(createProgram: CreateProgram<T>, createEmitProgram: CreateProgram<U>, optionsToExtend?: CompilerOptions) {
197+
const watch = getWatch(config, /*optionsToExtend*/ optionsToExtend, sys, createProgram);
198+
const emitWatch = getWatch(config, /*optionsToExtend*/ optionsToExtend, emitSys, createEmitProgram);
199+
verifyOutputs(sys, emitSys);
200+
watch.close();
201+
emitWatch.close();
202+
}
203+
});
157204
});
158205
}

0 commit comments

Comments
 (0)