• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1namespace ts {
2    describe("unittests:: config:: tsconfigParsingWatchOptions:: parseConfigFileTextToJson", () => {
3        function createParseConfigHost(additionalFiles?: vfs.FileSet) {
4            return new fakes.ParseConfigHost(
5                new vfs.FileSystem(
6                    /*ignoreCase*/ false,
7                    {
8                        cwd: "/",
9                        files: { "/": {}, "/a.ts": "", ...additionalFiles }
10                    }
11                )
12            );
13        }
14        function getParsedCommandJson(json: object, additionalFiles?: vfs.FileSet, existingWatchOptions?: WatchOptions) {
15            return parseJsonConfigFileContent(
16                json,
17                createParseConfigHost(additionalFiles),
18                "/",
19                /*existingOptions*/ undefined,
20                "tsconfig.json",
21                /*resolutionStack*/ undefined,
22                /*extraFileExtensions*/ undefined,
23                /*extendedConfigCache*/ undefined,
24                existingWatchOptions,
25            );
26        }
27
28        function getParsedCommandJsonNode(json: object, additionalFiles?: vfs.FileSet, existingWatchOptions?: WatchOptions) {
29            const parsed = parseJsonText("tsconfig.json", JSON.stringify(json));
30            return parseJsonSourceFileConfigFileContent(
31                parsed,
32                createParseConfigHost(additionalFiles),
33                "/",
34                /*existingOptions*/ undefined,
35                "tsconfig.json",
36                /*resolutionStack*/ undefined,
37                /*extraFileExtensions*/ undefined,
38                /*extendedConfigCache*/ undefined,
39                existingWatchOptions,
40            );
41        }
42
43        interface VerifyWatchOptions {
44            json: object;
45            additionalFiles?: vfs.FileSet;
46            existingWatchOptions?: WatchOptions | undefined;
47        }
48
49        function verifyWatchOptions(subScenario: string, scenario: () => VerifyWatchOptions[]) {
50            describe(subScenario, () => {
51                it("with json api", () => {
52                    const baseline: string[] = [];
53                    for (const { json, additionalFiles, existingWatchOptions } of scenario()) {
54                        addToBaseLine(baseline, json, getParsedCommandJson(json, additionalFiles, existingWatchOptions));
55                    }
56                    runBaseline(`${subScenario} with json api`, baseline);
57                });
58
59                it("with json source file api", () => {
60                    const baseline: string[] = [];
61                    for (const { json, additionalFiles, existingWatchOptions, } of scenario()) {
62                        addToBaseLine(baseline, json, getParsedCommandJsonNode(json, additionalFiles, existingWatchOptions));
63                    }
64                    runBaseline(`${subScenario} with jsonSourceFile api`, baseline);
65                });
66            });
67            function addToBaseLine(baseline: string[], json: object, parsed: ParsedCommandLine) {
68                baseline.push(`Input:: ${JSON.stringify(json, /*replacer*/ undefined, " ")}`);
69                baseline.push(`Result: WatchOptions::`);
70                baseline.push(JSON.stringify(parsed.watchOptions, /*replacer*/ undefined, " "));
71                baseline.push(`Result: Errors::`);
72                baseline.push(formatDiagnosticsWithColorAndContext(parsed.errors, {
73                    getCurrentDirectory: () => "/",
74                    getCanonicalFileName: identity,
75                    getNewLine: () => "\n"
76                }));
77            }
78            function runBaseline(subScenario: string, baseline: readonly string[]) {
79                Harness.Baseline.runBaseline(`config/tsconfigParsingWatchOptions/${subScenario}.js`, baseline.join("\n"));
80            }
81        }
82
83        verifyWatchOptions("no watchOptions specified option", () => [{
84            json: {},
85        }]);
86
87        verifyWatchOptions("empty watchOptions specified option", () => [{
88            json: { watchOptions: {} },
89        }]);
90
91        verifyWatchOptions("when extending config file without watchOptions", () => [
92            {
93                json: {
94                    extends: "./base.json",
95                    watchOptions: { watchFile: "UseFsEvents" }
96                },
97                additionalFiles: { "/base.json": "{}" }
98            },
99            {
100                json: { extends: "./base.json", },
101                additionalFiles: { "/base.json": "{}" }
102            }
103        ]);
104
105        verifyWatchOptions("when extending config file with watchOptions", () => [
106            {
107                json: {
108                    extends: "./base.json",
109                    watchOptions: {
110                        watchFile: "UseFsEvents",
111                    }
112                },
113                additionalFiles: {
114                    "/base.json": JSON.stringify({
115                        watchOptions: {
116                            watchFile: "UseFsEventsOnParentDirectory",
117                            watchDirectory: "FixedPollingInterval"
118                        }
119                    })
120                }
121            },
122            {
123                json: {
124                    extends: "./base.json",
125                },
126                additionalFiles: {
127                    "/base.json": JSON.stringify({
128                        watchOptions: {
129                            watchFile: "UseFsEventsOnParentDirectory",
130                            watchDirectory: "FixedPollingInterval"
131                        }
132                    })
133                }
134            }
135        ]);
136
137        verifyWatchOptions("different options", () => [
138            {
139                json: { watchOptions: { watchFile: "UseFsEvents" } },
140            },
141            {
142                json: { watchOptions: { watchDirectory: "UseFsEvents" } },
143            },
144            {
145                json: { watchOptions: { fallbackPolling: "DynamicPriority" } },
146            },
147            {
148                json: { watchOptions: { synchronousWatchDirectory: true } },
149            },
150            {
151                json: { watchOptions: { excludeDirectories: ["**/temp"] } },
152            },
153            {
154                json: { watchOptions: { excludeFiles: ["**/temp/*.ts"] } },
155            },
156            {
157                json: { watchOptions: { excludeDirectories: ["**/../*"] } },
158            },
159            {
160                json: { watchOptions: { excludeFiles: ["**/../*"] } },
161            },
162        ]);
163
164        verifyWatchOptions("watch options extending passed in watch options", () => [
165            {
166                json: { watchOptions: { watchFile: "UseFsEvents" } },
167            },
168            {
169                json: {},
170            },
171        ]);
172    });
173}
174