• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1namespace ts {
2    describe("unittests:: config:: showConfig", () => {
3        function showTSConfigCorrectly(name: string, commandLinesArgs: string[], configJson?: object) {
4            describe(name, () => {
5                const outputFileName = `config/showConfig/${name.replace(/[^a-z0-9\-./ ]/ig, "")}/tsconfig.json`;
6
7                it(`Correct output for ${outputFileName}`, () => {
8                    const cwd = `/${name}`;
9                    const configPath = combinePaths(cwd, "tsconfig.json");
10                    const configContents = configJson ? JSON.stringify(configJson) : undefined;
11                    const configParseHost: ParseConfigFileHost = {
12                        fileExists: path =>
13                            comparePaths(getNormalizedAbsolutePath(path, cwd), configPath) === Comparison.EqualTo ? true : false,
14                        getCurrentDirectory() { return cwd; },
15                        useCaseSensitiveFileNames: true,
16                        onUnRecoverableConfigFileDiagnostic: d => {
17                            throw new Error(flattenDiagnosticMessageText(d.messageText, "\n"));
18                        },
19                        readDirectory() { return []; },
20                        readFile: path =>
21                            comparePaths(getNormalizedAbsolutePath(path, cwd), configPath) === Comparison.EqualTo ? configContents : undefined,
22                    };
23                    let commandLine = parseCommandLine(commandLinesArgs);
24                    if (commandLine.options.project) {
25                        const result = getParsedCommandLineOfConfigFile(commandLine.options.project, commandLine.options, configParseHost);
26                        if (result) {
27                            commandLine = result;
28                        }
29                    }
30                    const initResult = convertToTSConfig(commandLine, configPath, configParseHost);
31
32                    // eslint-disable-next-line no-null/no-null
33                    Harness.Baseline.runBaseline(outputFileName, JSON.stringify(initResult, null, 4) + "\n");
34                });
35            });
36        }
37
38        showTSConfigCorrectly("Default initialized TSConfig", ["--showConfig"]);
39
40        showTSConfigCorrectly("Show TSConfig with files options", ["--showConfig", "file0.ts", "file1.ts", "file2.ts"]);
41
42        showTSConfigCorrectly("Show TSConfig with boolean value compiler options", ["--showConfig", "--noUnusedLocals"]);
43
44        showTSConfigCorrectly("Show TSConfig with enum value compiler options", ["--showConfig", "--target", "es5", "--jsx", "react"]);
45
46        showTSConfigCorrectly("Show TSConfig with list compiler options", ["--showConfig", "--types", "jquery,mocha"]);
47
48        showTSConfigCorrectly("Show TSConfig with list compiler options with enum value", ["--showConfig", "--lib", "es5,es2015.core"]);
49
50        showTSConfigCorrectly("Show TSConfig with incorrect compiler option", ["--showConfig", "--someNonExistOption"]);
51
52        showTSConfigCorrectly("Show TSConfig with incorrect compiler option value", ["--showConfig", "--lib", "nonExistLib,es5,es2015.promise"]);
53
54        showTSConfigCorrectly("Show TSConfig with advanced options", ["--showConfig", "--declaration", "--declarationDir", "lib", "--skipLibCheck", "--noErrorTruncation"]);
55
56        showTSConfigCorrectly("Show TSConfig with compileOnSave and more", ["-p", "tsconfig.json"], {
57            compilerOptions: {
58                esModuleInterop: true,
59                target: "es5",
60                module: "commonjs",
61                strict: true,
62            },
63            compileOnSave: true,
64            exclude: [
65                "dist"
66            ],
67            files: [],
68            include: [
69                "src/*"
70            ],
71            references: [
72                { path: "./test" }
73            ],
74        });
75
76        // Regression test for https://github.com/Microsoft/TypeScript/issues/28836
77        showTSConfigCorrectly("Show TSConfig with paths and more", ["-p", "tsconfig.json"], {
78            compilerOptions: {
79                allowJs: true,
80                outDir: "./lib",
81                esModuleInterop: true,
82                module: "commonjs",
83                moduleResolution: "node",
84                target: "ES2017",
85                sourceMap: true,
86                baseUrl: ".",
87                paths: {
88                    "@root/*": ["./*"],
89                    "@configs/*": ["src/configs/*"],
90                    "@common/*": ["src/common/*"],
91                    "*": [
92                        "node_modules/*",
93                        "src/types/*"
94                    ]
95                },
96                experimentalDecorators: true,
97                emitDecoratorMetadata: true,
98                resolveJsonModule: true
99            },
100            include: [
101                "./src/**/*"
102            ]
103        });
104
105        showTSConfigCorrectly("Show TSConfig with watch options", ["-p", "tsconfig.json"], {
106            watchOptions: {
107                watchFile: "DynamicPriorityPolling"
108            },
109            include: [
110                "./src/**/*"
111            ]
112        });
113
114        // Bulk validation of all option declarations
115        for (const option of optionDeclarations) {
116            baselineOption(option, /*isCompilerOptions*/ true);
117        }
118
119        for (const option of optionsForWatch) {
120            baselineOption(option, /*isCompilerOptions*/ false);
121        }
122
123        function baselineOption(option: CommandLineOption, isCompilerOptions: boolean) {
124            if (option.name === "project") return;
125            let args: string[];
126            let optionValue: object | undefined;
127            switch (option.type) {
128                case "boolean": {
129                    if (option.isTSConfigOnly) {
130                        args = ["-p", "tsconfig.json"];
131                        optionValue = { [option.name]: true };
132                    }
133                    else {
134                        args = [`--${option.name}`];
135                    }
136                    break;
137                }
138                case "list": {
139                    if (option.isTSConfigOnly) {
140                        args = ["-p", "tsconfig.json"];
141                        optionValue = { [option.name]: [] };
142                    }
143                    else {
144                        args = [`--${option.name}`];
145                    }
146                    break;
147                }
148                case "string": {
149                    if (option.isTSConfigOnly) {
150                        args = ["-p", "tsconfig.json"];
151                        optionValue = { [option.name]: "someString" };
152                    }
153                    else {
154                        args = [`--${option.name}`, "someString"];
155                    }
156                    break;
157                }
158                case "number": {
159                    if (option.isTSConfigOnly) {
160                        args = ["-p", "tsconfig.json"];
161                        optionValue = { [option.name]: 0 };
162                    }
163                    else {
164                        args = [`--${option.name}`, "0"];
165                    }
166                    break;
167                }
168                case "object": {
169                    args = ["-p", "tsconfig.json"];
170                    optionValue = { [option.name]: {} };
171                    break;
172                }
173                default: {
174                    const iterResult = option.type.keys().next();
175                    if (iterResult.done) return Debug.fail("Expected 'option.type' to have entries");
176                    const val = iterResult.value;
177                    if (option.isTSConfigOnly) {
178                        args = ["-p", "tsconfig.json"];
179                        optionValue = { [option.name]: val };
180                    }
181                    else {
182                        args = [`--${option.name}`, val];
183                    }
184                    break;
185                }
186            }
187
188            const configObject = optionValue &&
189                (isCompilerOptions ? { compilerOptions: optionValue } : { watchOptions: optionValue });
190            showTSConfigCorrectly(`Shows tsconfig for single option/${option.name}`, args, configObject);
191        }
192    });
193}
194