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