@@ -3,6 +3,9 @@ import * as fakes from "../_namespaces/fakes";
3
3
import * as Harness from "../_namespaces/Harness" ;
4
4
import * as ts from "../_namespaces/ts" ;
5
5
import * as vfs from "../_namespaces/vfs" ;
6
+ import { jsonToReadableText } from "./helpers" ;
7
+ import { libContent } from "./helpers/contents" ;
8
+ import { loadProjectFromFiles } from "./helpers/vfs" ;
6
9
7
10
describe ( "unittests:: Public APIs" , ( ) => {
8
11
function verifyApi ( fileName : string ) {
@@ -262,3 +265,70 @@ class C {
262
265
assert . equal ( ts . ModifierFlags . None , ts . getSyntacticModifierFlags ( propNode ) ) ;
263
266
} ) ;
264
267
} ) ;
268
+
269
+ describe ( "unittests:: Public APIs:: createProgram" , ( ) => {
270
+ function verifyAPI ( useJsonParsingApi : boolean ) {
271
+ const fs = loadProjectFromFiles ( {
272
+ "/src/projects/project/packages/a/index.js" : `export const a = 'a';` ,
273
+ "/src/projects/project/packages/a/test/index.js" : `import 'a';` ,
274
+ "/src/projects/project/packages/a/tsconfig.json" : jsonToReadableText ( {
275
+ compilerOptions : {
276
+ checkJs : true ,
277
+ composite : true ,
278
+ declaration : true ,
279
+ emitDeclarationOnly : true ,
280
+ module : "nodenext" ,
281
+ outDir : "types" ,
282
+ } ,
283
+ } ) ,
284
+ "/src/projects/project/packages/a/package.json" : jsonToReadableText ( {
285
+ name : "a" ,
286
+ version : "0.0.0" ,
287
+ type : "module" ,
288
+ exports : {
289
+ "." : {
290
+ types : "./types/index.d.ts" ,
291
+ default : "./index.js" ,
292
+ } ,
293
+ } ,
294
+ } ) ,
295
+ "/lib/lib.esnext.full.d.ts" : libContent ,
296
+ } , { cwd : "/src/projects/project" , executingFilePath : "/lib/tsc.js" } ) ;
297
+ const sys = new fakes . System ( fs , { executingFilePath : "/lib/tsc.js" } ) ;
298
+ const commandLine = ts . getParsedCommandLineOfConfigFile (
299
+ "/src/projects/project/packages/a/tsconfig.json" ,
300
+ /*optionsToExtend*/ undefined ,
301
+ {
302
+ fileExists : sys . fileExists . bind ( sys ) ,
303
+ getCurrentDirectory : sys . getCurrentDirectory . bind ( sys ) ,
304
+ onUnRecoverableConfigFileDiagnostic : ( ) => { } ,
305
+ readDirectory : sys . readDirectory . bind ( sys ) ,
306
+ readFile : sys . readFile . bind ( sys ) ,
307
+ useCaseSensitiveFileNames : sys . useCaseSensitiveFileNames ,
308
+ directoryExists : sys . directoryExists . bind ( sys ) ,
309
+ getDirectories : sys . getDirectories . bind ( sys ) ,
310
+ realpath : sys . realpath . bind ( sys ) ,
311
+ } ,
312
+ ) ! ;
313
+ const config = ! useJsonParsingApi ? JSON . parse ( fs . readFileSync ( "/src/projects/project/packages/a/tsconfig.json" , "utf-8" ) ) : undefined ;
314
+ // This is really createCompilerHost but we want to use our own sys so simple usage
315
+ const host = ts . createCompilerHostWorker (
316
+ useJsonParsingApi ? commandLine . options : config . compilerOptions ,
317
+ /*setParentNodes*/ undefined ,
318
+ sys ,
319
+ ) ;
320
+ ( useJsonParsingApi ? assert . doesNotThrow : assert . throws ) ( ( ) =>
321
+ ts . createProgram ( {
322
+ rootNames : commandLine . fileNames ,
323
+ options : useJsonParsingApi ? commandLine . options : config . compilerOptions ,
324
+ host,
325
+ } )
326
+ ) ;
327
+ }
328
+ it ( "when using correct config file API" , ( ) => {
329
+ verifyAPI ( /*useJsonParsingApi*/ true ) ;
330
+ } ) ;
331
+ it ( "when using direct json read" , ( ) => {
332
+ verifyAPI ( /*useJsonParsingApi*/ false ) ;
333
+ } ) ;
334
+ } ) ;
0 commit comments