Skip to content

Commit 9a6512f

Browse files
author
Andy Hanson
committed
Assert that the declaration file is what we emit
1 parent 4171e4e commit 9a6512f

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

src/testRunner/unittests/tsserverProjectSystem.ts

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,19 @@ namespace ts.projectSystem {
491491
return newRequest;
492492
}
493493

494-
export function openFilesForSession(files: ReadonlyArray<File>, session: server.Session) {
494+
export function openFilesForSession(files: ReadonlyArray<File>, session: server.Session): void {
495495
for (const file of files) {
496496
const request = makeSessionRequest<protocol.OpenRequestArgs>(CommandNames.Open, { file: file.path });
497497
session.executeCommand(request);
498498
}
499499
}
500500

501+
export function closeFilesForSession(files: ReadonlyArray<File>, session: server.Session): void {
502+
for (const file of files) {
503+
session.executeCommand(makeSessionRequest<protocol.FileRequestArgs>(CommandNames.Close, { file: file.path }));
504+
}
505+
}
506+
501507
interface ErrorInformation {
502508
diagnosticMessage: DiagnosticMessage;
503509
errorTextArguments?: string[];
@@ -8800,15 +8806,6 @@ export const x = 10;`
88008806
path: "/a/a.ts",
88018807
content: "export function a() {}",
88028808
};
8803-
const aDts: File = {
8804-
path: "/a/bin/a.d.ts",
8805-
// Need to mangle the sourceMappingURL part or it breaks the build
8806-
content: `export declare function a(): void;\n//# source${""}MappingURL=a.d.ts.map`,
8807-
};
8808-
const aDtsMap: File = {
8809-
path: "/a/bin/a.d.ts.map",
8810-
content: '{"version":3,"file":"a.d.ts","sourceRoot":"","sources":["../a.ts"],"names":[],"mappings":"AAAA,kCAAsB"}',
8811-
};
88128809
const aTsconfig: File = {
88138810
path: "/a/tsconfig.json",
88148811
content: `{
@@ -8837,8 +8834,15 @@ export const x = 10;`
88378834
}`,
88388835
};
88398836

8840-
const host = createServerHost([aTs, aDts, aDtsMap, aTsconfig, bTs, bTsconfig]);
8837+
const host = createServerHost([aTs, aTsconfig, bTs, bTsconfig]);
88418838
const session = createSession(host);
8839+
8840+
writeDeclarationFiles(aTs, host, session, [
8841+
// Need to mangle the sourceMappingURL part or it breaks the build
8842+
{ name: "/a/bin/a.d.ts.map", text: '{"version":3,"file":"a.d.ts","sourceRoot":"","sources":["../a.ts"],"names":[],"mappings":"AAAA,kCAAsB"}' },
8843+
{ name: "/a/bin/a.d.ts", text: `export declare function a(): void;\n//# source${""}MappingURL=a.d.ts.map` },
8844+
]);
8845+
88428846
openFilesForSession([bTs], session);
88438847

88448848
const definitionRequest = makeSessionRequest<protocol.FileLocationRequestArgs>(CommandNames.Definition, { file: bTs.path, ...protocolLocationFromSubstring(bTs.content, "a()") });
@@ -8851,4 +8855,21 @@ export const x = 10;`
88518855
assert.deepEqual(definitionResponse, [expectedResponse]);
88528856
});
88538857
});
8858+
8859+
function writeDeclarationFiles(file: File, host: TestServerHost, session: TestSession, expectedFiles: ReadonlyArray<{ readonly name: string, readonly text: string }>): void {
8860+
openFilesForSession([file], session);
8861+
const project = Debug.assertDefined(session.getProjectService().getDefaultProjectForFile(file.path as server.NormalizedPath, /*ensureProject*/ false));
8862+
const program = project.getCurrentProgram();
8863+
const output = getFileEmitOutput(program, Debug.assertDefined(program.getSourceFile(file.path)), /*emitOnlyDtsFiles*/ true);
8864+
closeFilesForSession([file], session);
8865+
8866+
Debug.assert(!output.emitSkipped);
8867+
assert.deepEqual(output.outputFiles, expectedFiles.map(e => ({ ...e, writeByteOrderMark: false })));
8868+
8869+
for (const { name, text } of output.outputFiles) {
8870+
const directory: Folder = { path: getDirectoryPath(name) };
8871+
host.ensureFileOrFolder(directory);
8872+
host.writeFile(name, text);
8873+
}
8874+
}
88548875
}

0 commit comments

Comments
 (0)