|
1 | 1 | import * as fakes from "../../_namespaces/fakes";
|
2 |
| -import * as ts from "../../_namespaces/ts"; |
3 | 2 | import * as vfs from "../../_namespaces/vfs";
|
| 3 | +import { baselineParseConfig } from "./helpers"; |
4 | 4 |
|
5 |
| -interface ExpectedResult { typeAcquisition: ts.TypeAcquisition; errors: ts.Diagnostic[]; } |
6 | 5 | describe("unittests:: config:: convertTypeAcquisitionFromJson", () => {
|
7 |
| - function assertTypeAcquisition(json: any, configFileName: string, expectedResult: ExpectedResult) { |
8 |
| - assertTypeAcquisitionWithJson(json, configFileName, expectedResult); |
9 |
| - assertTypeAcquisitionWithJsonNode(json, configFileName, expectedResult); |
10 |
| - } |
11 |
| - |
12 |
| - function verifyAcquisition(actualTypeAcquisition: ts.TypeAcquisition | undefined, expectedResult: ExpectedResult) { |
13 |
| - const parsedTypeAcquisition = JSON.stringify(actualTypeAcquisition); |
14 |
| - const expectedTypeAcquisition = JSON.stringify(expectedResult.typeAcquisition); |
15 |
| - assert.equal(parsedTypeAcquisition, expectedTypeAcquisition); |
16 |
| - } |
17 |
| - |
18 |
| - function verifyErrors(actualErrors: ts.Diagnostic[], expectedResult: ExpectedResult, hasLocation?: boolean) { |
19 |
| - const expectedErrors = expectedResult.errors; |
20 |
| - assert.isTrue(expectedResult.errors.length === actualErrors.length, `Expected error: ${JSON.stringify(expectedResult.errors)}. Actual error: ${JSON.stringify(actualErrors)}.`); |
21 |
| - for (let i = 0; i < actualErrors.length; i++) { |
22 |
| - const actualError = actualErrors[i]; |
23 |
| - const expectedError = expectedErrors[i]; |
24 |
| - assert.equal(actualError.code, expectedError.code, `Expected error-code: ${JSON.stringify(expectedError.code)}. Actual error-code: ${JSON.stringify(actualError.code)}.`); |
25 |
| - assert.equal(actualError.category, expectedError.category, `Expected error-category: ${JSON.stringify(expectedError.category)}. Actual error-category: ${JSON.stringify(actualError.category)}.`); |
26 |
| - if (hasLocation) { |
27 |
| - assert(actualError.file); |
28 |
| - assert(actualError.start); |
29 |
| - assert(actualError.length); |
30 |
| - } |
31 |
| - } |
32 |
| - } |
33 |
| - |
34 |
| - function assertTypeAcquisitionWithJson(json: any, configFileName: string, expectedResult: ExpectedResult) { |
35 |
| - const jsonOptions = json.typeAcquisition; |
36 |
| - const { options: actualTypeAcquisition, errors: actualErrors } = ts.convertTypeAcquisitionFromJson(jsonOptions, "/apath/", configFileName); |
37 |
| - verifyAcquisition(actualTypeAcquisition, expectedResult); |
38 |
| - verifyErrors(actualErrors, expectedResult); |
39 |
| - } |
40 |
| - |
41 |
| - function assertTypeAcquisitionWithJsonNode(json: any, configFileName: string, expectedResult: ExpectedResult) { |
42 |
| - const fileText = JSON.stringify(json); |
43 |
| - const result = ts.parseJsonText(configFileName, fileText); |
44 |
| - assert(!result.parseDiagnostics.length); |
45 |
| - assert(!!result.endOfFileToken); |
46 |
| - const host: ts.ParseConfigHost = new fakes.ParseConfigHost(new vfs.FileSystem(/*ignoreCase*/ false, { cwd: "/apath/" })); |
47 |
| - const { typeAcquisition: actualTypeAcquisition, errors: actualParseErrors } = ts.parseJsonSourceFileConfigFileContent(result, host, "/apath/", /*existingOptions*/ undefined, configFileName); |
48 |
| - verifyAcquisition(actualTypeAcquisition, expectedResult); |
49 |
| - |
50 |
| - const actualErrors = ts.filter(actualParseErrors, error => error.code !== ts.Diagnostics.No_inputs_were_found_in_config_file_0_Specified_include_paths_were_1_and_exclude_paths_were_2.code); |
51 |
| - verifyErrors(actualErrors, expectedResult, /*hasLocation*/ true); |
| 6 | + function baselineTypeAcquisition(subScenario: string, json: any, configFileName: string) { |
| 7 | + baselineParseConfig({ |
| 8 | + scenario: "convertTypeAcquisitionFromJson", |
| 9 | + subScenario, |
| 10 | + input: () => { |
| 11 | + const jsonText = JSON.stringify(json, undefined, " "); |
| 12 | + return [{ |
| 13 | + createHost: () => new fakes.ParseConfigHost(new vfs.FileSystem( |
| 14 | + /*ignoreCase*/ false, |
| 15 | + { |
| 16 | + cwd: "/apath/", |
| 17 | + files: { |
| 18 | + [`/apath/${configFileName}`]: jsonText, |
| 19 | + "/apath/a.ts": "", |
| 20 | + "/apath/b.js": "", |
| 21 | + } |
| 22 | + }, |
| 23 | + )), |
| 24 | + jsonText, |
| 25 | + configFileName, |
| 26 | + basePath: "/apath", |
| 27 | + baselineParsed: (baseline, parsed) => baseline.push("TypeAcquisition::", JSON.stringify(parsed.typeAcquisition, undefined, " ")), |
| 28 | + }]; |
| 29 | + }, |
| 30 | + }); |
52 | 31 | }
|
53 | 32 |
|
54 |
| - it("Convert correctly format tsconfig.json to typeAcquisition ", () => { |
55 |
| - assertTypeAcquisition( |
56 |
| - { |
57 |
| - typeAcquisition: |
58 |
| - { |
59 |
| - enable: true, |
60 |
| - include: ["0.d.ts", "1.d.ts"], |
61 |
| - exclude: ["0.js", "1.js"] |
62 |
| - } |
63 |
| - }, |
64 |
| - "tsconfig.json", |
| 33 | + baselineTypeAcquisition("Convert correctly format tsconfig.json to typeAcquisition ", |
| 34 | + { |
| 35 | + typeAcquisition: |
65 | 36 | {
|
66 |
| - typeAcquisition: |
67 |
| - { |
68 |
| - enable: true, |
69 |
| - include: ["0.d.ts", "1.d.ts"], |
70 |
| - exclude: ["0.js", "1.js"] |
71 |
| - }, |
72 |
| - errors: [] as ts.Diagnostic[] |
73 |
| - }); |
74 |
| - }); |
| 37 | + enable: true, |
| 38 | + include: ["0.d.ts", "1.d.ts"], |
| 39 | + exclude: ["0.js", "1.js"] |
| 40 | + } |
| 41 | + }, |
| 42 | + "tsconfig.json" |
| 43 | + ); |
75 | 44 |
|
76 |
| - it("Convert incorrect format tsconfig.json to typeAcquisition ", () => { |
77 |
| - assertTypeAcquisition( |
78 |
| - { |
79 |
| - typeAcquisition: |
80 |
| - { |
81 |
| - enableAutoDiscovy: true, |
82 |
| - } |
83 |
| - }, "tsconfig.json", |
| 45 | + baselineTypeAcquisition("Convert incorrect format tsconfig.json to typeAcquisition ", |
| 46 | + { |
| 47 | + typeAcquisition: |
84 | 48 | {
|
85 |
| - typeAcquisition: |
86 |
| - { |
87 |
| - enable: false, |
88 |
| - include: [], |
89 |
| - exclude: [] |
90 |
| - }, |
91 |
| - errors: [ |
92 |
| - { |
93 |
| - category: ts.Diagnostics.Unknown_type_acquisition_option_0.category, |
94 |
| - code: ts.Diagnostics.Unknown_type_acquisition_option_0.code, |
95 |
| - file: undefined, |
96 |
| - start: 0, |
97 |
| - length: 0, |
98 |
| - messageText: undefined!, // TODO: GH#18217 |
99 |
| - } |
100 |
| - ] |
101 |
| - }); |
102 |
| - }); |
| 49 | + enableAutoDiscovy: true, |
| 50 | + } |
| 51 | + }, "tsconfig.json", |
| 52 | + ); |
103 | 53 |
|
104 |
| - it("Convert default tsconfig.json to typeAcquisition ", () => { |
105 |
| - assertTypeAcquisition({}, "tsconfig.json", |
106 |
| - { |
107 |
| - typeAcquisition: |
108 |
| - { |
109 |
| - enable: false, |
110 |
| - include: [], |
111 |
| - exclude: [] |
112 |
| - }, |
113 |
| - errors: [] as ts.Diagnostic[] |
114 |
| - }); |
115 |
| - }); |
| 54 | + baselineTypeAcquisition("Convert default tsconfig.json to typeAcquisition ", {}, "tsconfig.json"); |
116 | 55 |
|
117 |
| - it("Convert tsconfig.json with only enable property to typeAcquisition ", () => { |
118 |
| - assertTypeAcquisition( |
| 56 | + baselineTypeAcquisition("Convert tsconfig.json with only enable property to typeAcquisition ", |
| 57 | + { |
| 58 | + typeAcquisition: |
119 | 59 | {
|
120 |
| - typeAcquisition: |
121 |
| - { |
122 |
| - enable: true |
123 |
| - } |
124 |
| - }, "tsconfig.json", |
125 |
| - { |
126 |
| - typeAcquisition: |
127 |
| - { |
128 |
| - enable: true, |
129 |
| - include: [], |
130 |
| - exclude: [] |
131 |
| - }, |
132 |
| - errors: [] as ts.Diagnostic[] |
133 |
| - }); |
134 |
| - }); |
| 60 | + enable: true |
| 61 | + } |
| 62 | + }, "tsconfig.json", |
| 63 | + ); |
135 | 64 |
|
136 | 65 | // jsconfig.json
|
137 |
| - it("Convert jsconfig.json to typeAcquisition ", () => { |
138 |
| - assertTypeAcquisition( |
139 |
| - { |
140 |
| - typeAcquisition: |
141 |
| - { |
142 |
| - enable: false, |
143 |
| - include: ["0.d.ts"], |
144 |
| - exclude: ["0.js"] |
145 |
| - } |
146 |
| - }, "jsconfig.json", |
| 66 | + baselineTypeAcquisition("Convert jsconfig.json to typeAcquisition ", |
| 67 | + { |
| 68 | + typeAcquisition: |
147 | 69 | {
|
148 |
| - typeAcquisition: |
149 |
| - { |
150 |
| - enable: false, |
151 |
| - include: ["0.d.ts"], |
152 |
| - exclude: ["0.js"] |
153 |
| - }, |
154 |
| - errors: [] as ts.Diagnostic[] |
155 |
| - }); |
156 |
| - }); |
| 70 | + enable: false, |
| 71 | + include: ["0.d.ts"], |
| 72 | + exclude: ["0.js"] |
| 73 | + } |
| 74 | + }, "jsconfig.json", |
| 75 | + ); |
157 | 76 |
|
158 |
| - it("Convert default jsconfig.json to typeAcquisition ", () => { |
159 |
| - assertTypeAcquisition({ }, "jsconfig.json", |
160 |
| - { |
161 |
| - typeAcquisition: |
162 |
| - { |
163 |
| - enable: true, |
164 |
| - include: [], |
165 |
| - exclude: [] |
166 |
| - }, |
167 |
| - errors: [] as ts.Diagnostic[] |
168 |
| - }); |
169 |
| - }); |
| 77 | + baselineTypeAcquisition("Convert default jsconfig.json to typeAcquisition ", {}, "jsconfig.json"); |
170 | 78 |
|
171 |
| - it("Convert incorrect format jsconfig.json to typeAcquisition ", () => { |
172 |
| - assertTypeAcquisition( |
173 |
| - { |
174 |
| - typeAcquisition: |
175 |
| - { |
176 |
| - enableAutoDiscovy: true, |
177 |
| - } |
178 |
| - }, "jsconfig.json", |
| 79 | + baselineTypeAcquisition("Convert incorrect format jsconfig.json to typeAcquisition ", |
| 80 | + { |
| 81 | + typeAcquisition: |
179 | 82 | {
|
180 |
| - typeAcquisition: |
181 |
| - { |
182 |
| - enable: true, |
183 |
| - include: [], |
184 |
| - exclude: [] |
185 |
| - }, |
186 |
| - errors: [ |
187 |
| - { |
188 |
| - category: ts.Diagnostics.Unknown_type_acquisition_option_0.category, |
189 |
| - code: ts.Diagnostics.Unknown_type_acquisition_option_0.code, |
190 |
| - file: undefined, |
191 |
| - start: 0, |
192 |
| - length: 0, |
193 |
| - messageText: undefined!, // TODO: GH#18217 |
194 |
| - } |
195 |
| - ] |
196 |
| - }); |
197 |
| - }); |
| 83 | + enableAutoDiscovy: true, |
| 84 | + } |
| 85 | + }, "jsconfig.json", |
| 86 | + ); |
198 | 87 |
|
199 |
| - it("Convert jsconfig.json with only enable property to typeAcquisition ", () => { |
200 |
| - assertTypeAcquisition( |
201 |
| - { |
202 |
| - typeAcquisition: |
203 |
| - { |
204 |
| - enable: false |
205 |
| - } |
206 |
| - }, "jsconfig.json", |
| 88 | + baselineTypeAcquisition("Convert jsconfig.json with only enable property to typeAcquisition ", |
| 89 | + { |
| 90 | + typeAcquisition: |
207 | 91 | {
|
208 |
| - typeAcquisition: |
209 |
| - { |
210 |
| - enable: false, |
211 |
| - include: [], |
212 |
| - exclude: [] |
213 |
| - }, |
214 |
| - errors: [] as ts.Diagnostic[] |
215 |
| - }); |
216 |
| - }); |
| 92 | + enable: false |
| 93 | + } |
| 94 | + }, "jsconfig.json", |
| 95 | + ); |
217 | 96 | });
|
0 commit comments