Skip to content

Some tests coverage for --out and errors scenarios #58652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/testRunner/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export * from "./unittests/tsc/incremental.js";
export * from "./unittests/tsc/libraryResolution.js";
export * from "./unittests/tsc/listFilesOnly.js";
export * from "./unittests/tsc/moduleResolution.js";
export * from "./unittests/tsc/noEmit.js";
export * from "./unittests/tsc/noEmitOnError.js";
export * from "./unittests/tsc/projectReferences.js";
export * from "./unittests/tsc/projectReferencesConfig.js";
export * from "./unittests/tsc/redirect.js";
Expand All @@ -134,6 +136,7 @@ export * from "./unittests/tscWatch/incremental.js";
export * from "./unittests/tscWatch/libraryResolution.js";
export * from "./unittests/tscWatch/moduleResolution.js";
export * from "./unittests/tscWatch/nodeNextWatch.js";
export * from "./unittests/tscWatch/noEmitOnError.js";
export * from "./unittests/tscWatch/programUpdates.js";
export * from "./unittests/tscWatch/projectsWithReferences.js";
export * from "./unittests/tscWatch/resolutionCache.js";
Expand Down
93 changes: 93 additions & 0 deletions src/testRunner/unittests/helpers/declarationEmit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { CompilerOptions } from "../../_namespaces/ts.js";
import { dedent } from "../../_namespaces/Utils.js";
import { FileSystem } from "../../_namespaces/vfs.js";
import { jsonToReadableText } from "../helpers.js";
import { libContent } from "./contents.js";
import { loadProjectFromFiles } from "./vfs.js";

export function getFsForDeclarationEmitWithErrors(options: CompilerOptions, incremental: true | undefined) {
return loadProjectFromFiles({
"/src/project/tsconfig.json": jsonToReadableText({
compilerOptions: {
module: "NodeNext",
moduleResolution: "NodeNext",
...options,
incremental,
skipLibCheck: true,
skipDefaultLibCheck: true,
},
}),
"/src/project/index.ts": dedent`
import ky from 'ky';
export const api = ky.extend({});
`,
"/src/project/package.json": jsonToReadableText({
type: "module",
}),
"/src/project/node_modules/ky/distribution/index.d.ts": dedent`
type KyInstance = {
extend(options: Record<string,unknown>): KyInstance;
}
declare const ky: KyInstance;
export default ky;
`,
"/src/project/node_modules/ky/package.json": jsonToReadableText({
name: "ky",
type: "module",
main: "./distribution/index.js",
}),
"/lib/lib.esnext.full.d.ts": libContent,
});
}

export function getFsForDeclarationEmitWithErrorsWithOutFile(options: CompilerOptions, incremental: true | undefined) {
return loadProjectFromFiles({
"/src/project/tsconfig.json": jsonToReadableText({
compilerOptions: {
module: "amd",
...options,
incremental,
skipLibCheck: true,
skipDefaultLibCheck: true,
outFile: "./outFile.js",
},
include: ["src"],
}),
"/src/project/src/index.ts": dedent`
import ky from 'ky';
export const api = ky.extend({});
`,
"/src/project/ky.d.ts": dedent`
type KyInstance = {
extend(options: Record<string,unknown>): KyInstance;
}
declare const ky: KyInstance;
export default ky;
`,
"/lib/lib.esnext.full.d.ts": libContent,
});
}

export function forEachDeclarationEmitWithErrorsScenario(
action: (
scenarioName: (scenario: string) => string,
fs: () => FileSystem,
) => void,
withComposite: boolean,
) {
for (const outFile of [false, true]) {
for (const incremental of [undefined, true] as const) {
action(
scenario => `${scenario}${outFile ? " outFile" : ""}${incremental ? " with incremental" : ""}`,
() =>
(outFile ? getFsForDeclarationEmitWithErrorsWithOutFile :
getFsForDeclarationEmitWithErrors)(
withComposite && incremental ?
{ composite: true } :
{ declaration: true },
incremental,
),
);
}
}
}
51 changes: 27 additions & 24 deletions src/testRunner/unittests/helpers/noEmitOnError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import {
FsContents,
libContent,
} from "./contents.js";
import { loadProjectFromFiles } from "./vfs.js";
import {
createWatchedSystem,
libFile,
} from "./virtualFileSystemWithWatch.js";
import { libFile } from "./virtualFileSystemWithWatch.js";

export function getFsContentsForNoEmitOnError(): FsContents {
function getFsContentsForNoEmitOnError(outFile: boolean, declaration: true | undefined, incremental: true | undefined): FsContents {
return {
"/user/username/projects/noEmitOnError/tsconfig.json": jsonToReadableText({
compilerOptions: {
outDir: "./dev-build",
...outFile ? { outFile: "../dev-build.js", module: "amd" } : { outDir: "./dev-build" },
declaration,
incremental,
noEmitOnError: true,
},
}),
Expand All @@ -37,21 +35,26 @@ export function getFsContentsForNoEmitOnError(): FsContents {
};
}

export function getFsForNoEmitOnError() {
return loadProjectFromFiles(
getFsContentsForNoEmitOnError(),
{
cwd: "/user/username/projects/noEmitOnError",
executingFilePath: libFile.path,
},
);
}

export function getSysForNoEmitOnError() {
return createWatchedSystem(
getFsContentsForNoEmitOnError(),
{
currentDirectory: "/user/username/projects/noEmitOnError",
},
);
export function forEachNoEmitOnErrorScenario<T>(
loadFs: (contents: FsContents, currentDirectory: string, executingFilePath: string) => T,
action: (
scenarioName: (scenario: string) => string,
fs: () => T,
) => void,
) {
for (const outFile of [false, true]) {
for (const declaration of [undefined, true] as const) {
for (const incremental of [undefined, true] as const) {
action(
scenario => `${scenario}${outFile ? " outFile" : ""}${declaration ? " with declaration" : ""}${incremental ? " with incremental" : ""}`,
() =>
loadFs(
getFsContentsForNoEmitOnError(outFile, declaration, incremental),
"/user/username/projects/noEmitOnError",
libFile.path,
),
);
}
}
}
}
92 changes: 48 additions & 44 deletions src/testRunner/unittests/tsbuild/configFileErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,57 +43,61 @@ describe("unittests:: tsbuild:: configFileErrors:: when tsconfig extends the mis
});

describe("unittests:: tsbuild:: configFileErrors:: reports syntax errors in config file", () => {
verifyTsc({
scenario: "configFileErrors",
subScenario: "reports syntax errors in config file",
fs: () =>
loadProjectFromFiles({
"/src/a.ts": "export function foo() { }",
"/src/b.ts": "export function bar() { }",
"/src/tsconfig.json": dedent`
function verify(outFile?: object) {
verifyTsc({
scenario: "configFileErrors",
subScenario: `reports syntax errors in config file${outFile ? " with outFile" : ""}`,
fs: () =>
loadProjectFromFiles({
"/src/a.ts": "export function foo() { }",
"/src/b.ts": "export function bar() { }",
"/src/tsconfig.json": dedent`
{
"compilerOptions": {
"composite": true,
"composite": true,${outFile ? jsonToReadableText(outFile).replace(/[{}]/g, "") : ""}
},
"files": [
"a.ts"
"b.ts"
]
}`,
}),
commandLineArgs: ["--b", "/src/tsconfig.json"],
edits: [
{
edit: fs =>
replaceText(
fs,
"/src/tsconfig.json",
",",
`,
}),
commandLineArgs: ["--b", "/src/tsconfig.json"],
edits: [
{
edit: fs =>
replaceText(
fs,
"/src/tsconfig.json",
",",
`,
"declaration": true,`,
),
caption: "reports syntax errors after change to config file",
discrepancyExplanation: () => [
"During incremental build, tsbuildinfo is not emitted, so declaration option is not present",
"Clean build has declaration option in tsbuildinfo",
],
},
{
edit: fs => appendText(fs, "/src/a.ts", "export function fooBar() { }"),
caption: "reports syntax errors after change to ts file",
},
noChangeRun,
{
edit: fs =>
fs.writeFileSync(
"/src/tsconfig.json",
jsonToReadableText({
compilerOptions: { composite: true, declaration: true },
files: ["a.ts", "b.ts"],
}),
),
caption: "builds after fixing config file errors",
},
],
});
),
caption: "reports syntax errors after change to config file",
discrepancyExplanation: !outFile ? () => [
"During incremental build, tsbuildinfo is not emitted, so declaration option is not present",
"Clean build has declaration option in tsbuildinfo",
] : undefined,
},
{
edit: fs => appendText(fs, "/src/a.ts", "export function fooBar() { }"),
caption: "reports syntax errors after change to ts file",
},
noChangeRun,
{
edit: fs =>
fs.writeFileSync(
"/src/tsconfig.json",
jsonToReadableText({
compilerOptions: { composite: true, declaration: true, ...outFile },
files: ["a.ts", "b.ts"],
}),
),
caption: "builds after fixing config file errors",
},
],
});
}
verify();
verify({ outFile: "../outFile.js", module: "amd" });
});
53 changes: 13 additions & 40 deletions src/testRunner/unittests/tsbuild/declarationEmit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { dedent } from "../../_namespaces/Utils.js";
import { FileSet } from "../../_namespaces/vfs.js";
import { jsonToReadableText } from "../helpers.js";
import { libContent } from "../helpers/contents.js";
import { forEachDeclarationEmitWithErrorsScenario } from "../helpers/declarationEmit.js";
import {
noChangeOnlyRuns,
verifyTsc,
Expand Down Expand Up @@ -126,43 +126,16 @@ export function fn4() {
commandLineArgs: ["--b", "/src/packages/pkg2/tsconfig.json", "--verbose"],
});

verifyTsc({
scenario: "declarationEmit",
subScenario: "reports dts generation errors with incremental",
commandLineArgs: ["-b", `/src/project`, "--explainFiles", "--listEmittedFiles", "--v"],
fs: () =>
loadProjectFromFiles({
"/src/project/tsconfig.json": jsonToReadableText({
compilerOptions: {
module: "NodeNext",
moduleResolution: "NodeNext",
incremental: true,
declaration: true,
skipLibCheck: true,
skipDefaultLibCheck: true,
},
}),
"/src/project/index.ts": dedent`
import ky from 'ky';
export const api = ky.extend({});
`,
"/src/project/package.json": jsonToReadableText({
type: "module",
}),
"/src/project/node_modules/ky/distribution/index.d.ts": dedent`
type KyInstance = {
extend(options: Record<string,unknown>): KyInstance;
}
declare const ky: KyInstance;
export default ky;
`,
"/src/project/node_modules/ky/package.json": jsonToReadableText({
name: "ky",
type: "module",
main: "./distribution/index.js",
}),
"/lib/lib.esnext.full.d.ts": libContent,
}),
edits: noChangeOnlyRuns,
});
forEachDeclarationEmitWithErrorsScenario(
(scenario, fs) => {
verifyTsc({
scenario: "declarationEmit",
subScenario: scenario("reports dts generation errors"),
commandLineArgs: ["-b", `/src/project`, "--explainFiles", "--listEmittedFiles", "--v"],
fs,
edits: noChangeOnlyRuns,
});
},
/*withComposite*/ false,
);
});
6 changes: 4 additions & 2 deletions src/testRunner/unittests/tsbuild/noEmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import {
import { loadProjectFromFiles } from "../helpers/vfs.js";

describe("unittests:: tsbuild:: noEmit", () => {
function verifyNoEmitWorker(subScenario: string, aTsContent: string, commandLineArgs: readonly string[]) {
function verifyNoEmitWorker(subScenario: string, aTsContent: string, commandLineArgs: readonly string[], options?: object) {
verifyTsc({
scenario: "noEmit",
subScenario,
fs: () =>
loadProjectFromFiles({
"/src/a.ts": aTsContent,
"/src/tsconfig.json": jsonToReadableText({
compilerOptions: { noEmit: true },
compilerOptions: { ...options, noEmit: true },
}),
}),
commandLineArgs,
Expand All @@ -33,6 +33,8 @@ describe("unittests:: tsbuild:: noEmit", () => {
function verifyNoEmit(subScenario: string, aTsContent: string) {
verifyNoEmitWorker(subScenario, aTsContent, ["--b", "/src/tsconfig.json", "-v"]);
verifyNoEmitWorker(`${subScenario} with incremental`, aTsContent, ["--b", "/src/tsconfig.json", "-v", "--incremental"]);
verifyNoEmitWorker(`${subScenario} with outFile`, aTsContent, ["--b", "/src/tsconfig.json", "-v"], { outFile: "../outFile.js" });
verifyNoEmitWorker(`${subScenario} with outFile with incremental`, aTsContent, ["--b", "/src/tsconfig.json", "-v", "--incremental"], { outFile: "../outFile.js" });
}

verifyNoEmit("syntax errors", `const a = "hello`);
Expand Down
Loading