Skip to content

Commit 7ad8f39

Browse files
authored
Some tests coverage for --out and errors scenarios (#58652)
1 parent d0ef028 commit 7ad8f39

File tree

112 files changed

+21418
-3912
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+21418
-3912
lines changed

src/testRunner/tests.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ export * from "./unittests/tsc/incremental.js";
122122
export * from "./unittests/tsc/libraryResolution.js";
123123
export * from "./unittests/tsc/listFilesOnly.js";
124124
export * from "./unittests/tsc/moduleResolution.js";
125+
export * from "./unittests/tsc/noEmit.js";
126+
export * from "./unittests/tsc/noEmitOnError.js";
125127
export * from "./unittests/tsc/projectReferences.js";
126128
export * from "./unittests/tsc/projectReferencesConfig.js";
127129
export * from "./unittests/tsc/redirect.js";
@@ -135,6 +137,7 @@ export * from "./unittests/tscWatch/incremental.js";
135137
export * from "./unittests/tscWatch/libraryResolution.js";
136138
export * from "./unittests/tscWatch/moduleResolution.js";
137139
export * from "./unittests/tscWatch/nodeNextWatch.js";
140+
export * from "./unittests/tscWatch/noEmitOnError.js";
138141
export * from "./unittests/tscWatch/programUpdates.js";
139142
export * from "./unittests/tscWatch/projectsWithReferences.js";
140143
export * from "./unittests/tscWatch/resolutionCache.js";
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { CompilerOptions } from "../../_namespaces/ts.js";
2+
import { dedent } from "../../_namespaces/Utils.js";
3+
import { FileSystem } from "../../_namespaces/vfs.js";
4+
import { jsonToReadableText } from "../helpers.js";
5+
import { libContent } from "./contents.js";
6+
import { loadProjectFromFiles } from "./vfs.js";
7+
8+
export function getFsForDeclarationEmitWithErrors(options: CompilerOptions, incremental: true | undefined) {
9+
return loadProjectFromFiles({
10+
"/src/project/tsconfig.json": jsonToReadableText({
11+
compilerOptions: {
12+
module: "NodeNext",
13+
moduleResolution: "NodeNext",
14+
...options,
15+
incremental,
16+
skipLibCheck: true,
17+
skipDefaultLibCheck: true,
18+
},
19+
}),
20+
"/src/project/index.ts": dedent`
21+
import ky from 'ky';
22+
export const api = ky.extend({});
23+
`,
24+
"/src/project/package.json": jsonToReadableText({
25+
type: "module",
26+
}),
27+
"/src/project/node_modules/ky/distribution/index.d.ts": dedent`
28+
type KyInstance = {
29+
extend(options: Record<string,unknown>): KyInstance;
30+
}
31+
declare const ky: KyInstance;
32+
export default ky;
33+
`,
34+
"/src/project/node_modules/ky/package.json": jsonToReadableText({
35+
name: "ky",
36+
type: "module",
37+
main: "./distribution/index.js",
38+
}),
39+
"/lib/lib.esnext.full.d.ts": libContent,
40+
});
41+
}
42+
43+
export function getFsForDeclarationEmitWithErrorsWithOutFile(options: CompilerOptions, incremental: true | undefined) {
44+
return loadProjectFromFiles({
45+
"/src/project/tsconfig.json": jsonToReadableText({
46+
compilerOptions: {
47+
module: "amd",
48+
...options,
49+
incremental,
50+
skipLibCheck: true,
51+
skipDefaultLibCheck: true,
52+
outFile: "./outFile.js",
53+
},
54+
include: ["src"],
55+
}),
56+
"/src/project/src/index.ts": dedent`
57+
import ky from 'ky';
58+
export const api = ky.extend({});
59+
`,
60+
"/src/project/ky.d.ts": dedent`
61+
type KyInstance = {
62+
extend(options: Record<string,unknown>): KyInstance;
63+
}
64+
declare const ky: KyInstance;
65+
export default ky;
66+
`,
67+
"/lib/lib.esnext.full.d.ts": libContent,
68+
});
69+
}
70+
71+
export function forEachDeclarationEmitWithErrorsScenario(
72+
action: (
73+
scenarioName: (scenario: string) => string,
74+
fs: () => FileSystem,
75+
) => void,
76+
withComposite: boolean,
77+
) {
78+
for (const outFile of [false, true]) {
79+
for (const incremental of [undefined, true] as const) {
80+
action(
81+
scenario => `${scenario}${outFile ? " outFile" : ""}${incremental ? " with incremental" : ""}`,
82+
() =>
83+
(outFile ? getFsForDeclarationEmitWithErrorsWithOutFile :
84+
getFsForDeclarationEmitWithErrors)(
85+
withComposite && incremental ?
86+
{ composite: true } :
87+
{ declaration: true },
88+
incremental,
89+
),
90+
);
91+
}
92+
}
93+
}

src/testRunner/unittests/helpers/noEmitOnError.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ import {
44
FsContents,
55
libContent,
66
} from "./contents.js";
7-
import { loadProjectFromFiles } from "./vfs.js";
8-
import {
9-
createWatchedSystem,
10-
libFile,
11-
} from "./virtualFileSystemWithWatch.js";
7+
import { libFile } from "./virtualFileSystemWithWatch.js";
128

13-
export function getFsContentsForNoEmitOnError(): FsContents {
9+
function getFsContentsForNoEmitOnError(outFile: boolean, declaration: true | undefined, incremental: true | undefined): FsContents {
1410
return {
1511
"/user/username/projects/noEmitOnError/tsconfig.json": jsonToReadableText({
1612
compilerOptions: {
17-
outDir: "./dev-build",
13+
...outFile ? { outFile: "../dev-build.js", module: "amd" } : { outDir: "./dev-build" },
14+
declaration,
15+
incremental,
1816
noEmitOnError: true,
1917
},
2018
}),
@@ -37,21 +35,26 @@ export function getFsContentsForNoEmitOnError(): FsContents {
3735
};
3836
}
3937

40-
export function getFsForNoEmitOnError() {
41-
return loadProjectFromFiles(
42-
getFsContentsForNoEmitOnError(),
43-
{
44-
cwd: "/user/username/projects/noEmitOnError",
45-
executingFilePath: libFile.path,
46-
},
47-
);
48-
}
49-
50-
export function getSysForNoEmitOnError() {
51-
return createWatchedSystem(
52-
getFsContentsForNoEmitOnError(),
53-
{
54-
currentDirectory: "/user/username/projects/noEmitOnError",
55-
},
56-
);
38+
export function forEachNoEmitOnErrorScenario<T>(
39+
loadFs: (contents: FsContents, currentDirectory: string, executingFilePath: string) => T,
40+
action: (
41+
scenarioName: (scenario: string) => string,
42+
fs: () => T,
43+
) => void,
44+
) {
45+
for (const outFile of [false, true]) {
46+
for (const declaration of [undefined, true] as const) {
47+
for (const incremental of [undefined, true] as const) {
48+
action(
49+
scenario => `${scenario}${outFile ? " outFile" : ""}${declaration ? " with declaration" : ""}${incremental ? " with incremental" : ""}`,
50+
() =>
51+
loadFs(
52+
getFsContentsForNoEmitOnError(outFile, declaration, incremental),
53+
"/user/username/projects/noEmitOnError",
54+
libFile.path,
55+
),
56+
);
57+
}
58+
}
59+
}
5760
}

src/testRunner/unittests/tsbuild/configFileErrors.ts

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -43,57 +43,61 @@ describe("unittests:: tsbuild:: configFileErrors:: when tsconfig extends the mis
4343
});
4444

4545
describe("unittests:: tsbuild:: configFileErrors:: reports syntax errors in config file", () => {
46-
verifyTsc({
47-
scenario: "configFileErrors",
48-
subScenario: "reports syntax errors in config file",
49-
fs: () =>
50-
loadProjectFromFiles({
51-
"/src/a.ts": "export function foo() { }",
52-
"/src/b.ts": "export function bar() { }",
53-
"/src/tsconfig.json": dedent`
46+
function verify(outFile?: object) {
47+
verifyTsc({
48+
scenario: "configFileErrors",
49+
subScenario: `reports syntax errors in config file${outFile ? " with outFile" : ""}`,
50+
fs: () =>
51+
loadProjectFromFiles({
52+
"/src/a.ts": "export function foo() { }",
53+
"/src/b.ts": "export function bar() { }",
54+
"/src/tsconfig.json": dedent`
5455
{
5556
"compilerOptions": {
56-
"composite": true,
57+
"composite": true,${outFile ? jsonToReadableText(outFile).replace(/[{}]/g, "") : ""}
5758
},
5859
"files": [
5960
"a.ts"
6061
"b.ts"
6162
]
6263
}`,
63-
}),
64-
commandLineArgs: ["--b", "/src/tsconfig.json"],
65-
edits: [
66-
{
67-
edit: fs =>
68-
replaceText(
69-
fs,
70-
"/src/tsconfig.json",
71-
",",
72-
`,
64+
}),
65+
commandLineArgs: ["--b", "/src/tsconfig.json"],
66+
edits: [
67+
{
68+
edit: fs =>
69+
replaceText(
70+
fs,
71+
"/src/tsconfig.json",
72+
",",
73+
`,
7374
"declaration": true,`,
74-
),
75-
caption: "reports syntax errors after change to config file",
76-
discrepancyExplanation: () => [
77-
"During incremental build, tsbuildinfo is not emitted, so declaration option is not present",
78-
"Clean build has declaration option in tsbuildinfo",
79-
],
80-
},
81-
{
82-
edit: fs => appendText(fs, "/src/a.ts", "export function fooBar() { }"),
83-
caption: "reports syntax errors after change to ts file",
84-
},
85-
noChangeRun,
86-
{
87-
edit: fs =>
88-
fs.writeFileSync(
89-
"/src/tsconfig.json",
90-
jsonToReadableText({
91-
compilerOptions: { composite: true, declaration: true },
92-
files: ["a.ts", "b.ts"],
93-
}),
94-
),
95-
caption: "builds after fixing config file errors",
96-
},
97-
],
98-
});
75+
),
76+
caption: "reports syntax errors after change to config file",
77+
discrepancyExplanation: !outFile ? () => [
78+
"During incremental build, tsbuildinfo is not emitted, so declaration option is not present",
79+
"Clean build has declaration option in tsbuildinfo",
80+
] : undefined,
81+
},
82+
{
83+
edit: fs => appendText(fs, "/src/a.ts", "export function fooBar() { }"),
84+
caption: "reports syntax errors after change to ts file",
85+
},
86+
noChangeRun,
87+
{
88+
edit: fs =>
89+
fs.writeFileSync(
90+
"/src/tsconfig.json",
91+
jsonToReadableText({
92+
compilerOptions: { composite: true, declaration: true, ...outFile },
93+
files: ["a.ts", "b.ts"],
94+
}),
95+
),
96+
caption: "builds after fixing config file errors",
97+
},
98+
],
99+
});
100+
}
101+
verify();
102+
verify({ outFile: "../outFile.js", module: "amd" });
99103
});

src/testRunner/unittests/tsbuild/declarationEmit.ts

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { dedent } from "../../_namespaces/Utils.js";
22
import { FileSet } from "../../_namespaces/vfs.js";
33
import { jsonToReadableText } from "../helpers.js";
4-
import { libContent } from "../helpers/contents.js";
4+
import { forEachDeclarationEmitWithErrorsScenario } from "../helpers/declarationEmit.js";
55
import {
66
noChangeOnlyRuns,
77
verifyTsc,
@@ -126,43 +126,16 @@ export function fn4() {
126126
commandLineArgs: ["--b", "/src/packages/pkg2/tsconfig.json", "--verbose"],
127127
});
128128

129-
verifyTsc({
130-
scenario: "declarationEmit",
131-
subScenario: "reports dts generation errors with incremental",
132-
commandLineArgs: ["-b", `/src/project`, "--explainFiles", "--listEmittedFiles", "--v"],
133-
fs: () =>
134-
loadProjectFromFiles({
135-
"/src/project/tsconfig.json": jsonToReadableText({
136-
compilerOptions: {
137-
module: "NodeNext",
138-
moduleResolution: "NodeNext",
139-
incremental: true,
140-
declaration: true,
141-
skipLibCheck: true,
142-
skipDefaultLibCheck: true,
143-
},
144-
}),
145-
"/src/project/index.ts": dedent`
146-
import ky from 'ky';
147-
export const api = ky.extend({});
148-
`,
149-
"/src/project/package.json": jsonToReadableText({
150-
type: "module",
151-
}),
152-
"/src/project/node_modules/ky/distribution/index.d.ts": dedent`
153-
type KyInstance = {
154-
extend(options: Record<string,unknown>): KyInstance;
155-
}
156-
declare const ky: KyInstance;
157-
export default ky;
158-
`,
159-
"/src/project/node_modules/ky/package.json": jsonToReadableText({
160-
name: "ky",
161-
type: "module",
162-
main: "./distribution/index.js",
163-
}),
164-
"/lib/lib.esnext.full.d.ts": libContent,
165-
}),
166-
edits: noChangeOnlyRuns,
167-
});
129+
forEachDeclarationEmitWithErrorsScenario(
130+
(scenario, fs) => {
131+
verifyTsc({
132+
scenario: "declarationEmit",
133+
subScenario: scenario("reports dts generation errors"),
134+
commandLineArgs: ["-b", `/src/project`, "--explainFiles", "--listEmittedFiles", "--v"],
135+
fs,
136+
edits: noChangeOnlyRuns,
137+
});
138+
},
139+
/*withComposite*/ false,
140+
);
168141
});

src/testRunner/unittests/tsbuild/noEmit.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import {
66
import { loadProjectFromFiles } from "../helpers/vfs.js";
77

88
describe("unittests:: tsbuild:: noEmit", () => {
9-
function verifyNoEmitWorker(subScenario: string, aTsContent: string, commandLineArgs: readonly string[]) {
9+
function verifyNoEmitWorker(subScenario: string, aTsContent: string, commandLineArgs: readonly string[], options?: object) {
1010
verifyTsc({
1111
scenario: "noEmit",
1212
subScenario,
1313
fs: () =>
1414
loadProjectFromFiles({
1515
"/src/a.ts": aTsContent,
1616
"/src/tsconfig.json": jsonToReadableText({
17-
compilerOptions: { noEmit: true },
17+
compilerOptions: { ...options, noEmit: true },
1818
}),
1919
}),
2020
commandLineArgs,
@@ -33,6 +33,8 @@ describe("unittests:: tsbuild:: noEmit", () => {
3333
function verifyNoEmit(subScenario: string, aTsContent: string) {
3434
verifyNoEmitWorker(subScenario, aTsContent, ["--b", "/src/tsconfig.json", "-v"]);
3535
verifyNoEmitWorker(`${subScenario} with incremental`, aTsContent, ["--b", "/src/tsconfig.json", "-v", "--incremental"]);
36+
verifyNoEmitWorker(`${subScenario} with outFile`, aTsContent, ["--b", "/src/tsconfig.json", "-v"], { outFile: "../outFile.js" });
37+
verifyNoEmitWorker(`${subScenario} with outFile with incremental`, aTsContent, ["--b", "/src/tsconfig.json", "-v", "--incremental"], { outFile: "../outFile.js" });
3638
}
3739

3840
verifyNoEmit("syntax errors", `const a = "hello`);

0 commit comments

Comments
 (0)