@@ -133,7 +133,7 @@ describe("walkForTsConfig", () => {
133133} ) ;
134134
135135describe ( "loadConfig" , ( ) => {
136- it ( "It should load a config" , ( ) => {
136+ it ( "should load a config" , ( ) => {
137137 const config = { compilerOptions : { baseUrl : "hej" } } ;
138138 const res = loadTsconfig (
139139 "/root/dir1/tsconfig.json" ,
@@ -144,7 +144,7 @@ describe("loadConfig", () => {
144144 expect ( res ) . toStrictEqual ( config ) ;
145145 } ) ;
146146
147- it ( "It should load a config with comments" , ( ) => {
147+ it ( "should load a config with comments" , ( ) => {
148148 const config = { compilerOptions : { baseUrl : "hej" } } ;
149149 const res = loadTsconfig (
150150 "/root/dir1/tsconfig.json" ,
@@ -160,7 +160,7 @@ describe("loadConfig", () => {
160160 expect ( res ) . toStrictEqual ( config ) ;
161161 } ) ;
162162
163- it ( "It should load a config with trailing commas" , ( ) => {
163+ it ( "should load a config with trailing commas" , ( ) => {
164164 const config = { compilerOptions : { baseUrl : "hej" } } ;
165165 const res = loadTsconfig (
166166 "/root/dir1/tsconfig.json" ,
@@ -175,7 +175,21 @@ describe("loadConfig", () => {
175175 expect ( res ) . toStrictEqual ( config ) ;
176176 } ) ;
177177
178- it ( "It should load a config with extends and overwrite all options" , ( ) => {
178+ it ( "should throw an error including the file path when encountering invalid JSON5" , ( ) => {
179+ expect ( ( ) =>
180+ loadTsconfig (
181+ "/root/dir1/tsconfig.json" ,
182+ ( path ) => path === "/root/dir1/tsconfig.json" ,
183+ ( _ ) => `{
184+ "compilerOptions": {
185+ }`
186+ )
187+ ) . toThrowError (
188+ "/root/dir1/tsconfig.json is malformed JSON5: invalid end of input at 3:12"
189+ ) ;
190+ } ) ;
191+
192+ it ( "should load a config with string extends and overwrite all options" , ( ) => {
179193 const firstConfig = {
180194 extends : "../base-config.json" ,
181195 compilerOptions : { baseUrl : "kalle" , paths : { foo : [ "bar2" ] } } ,
@@ -221,7 +235,7 @@ describe("loadConfig", () => {
221235 } ) ;
222236 } ) ;
223237
224- it ( "It should load a config with extends from node_modules and overwrite all options" , ( ) => {
238+ it ( "should load a config with string extends from node_modules and overwrite all options" , ( ) => {
225239 const firstConfig = {
226240 extends : "my-package/base-config.json" ,
227241 compilerOptions : { baseUrl : "kalle" , paths : { foo : [ "bar2" ] } } ,
@@ -273,7 +287,7 @@ describe("loadConfig", () => {
273287 } ) ;
274288 } ) ;
275289
276- it ( "Should use baseUrl relative to location of extended tsconfig" , ( ) => {
290+ it ( "should use baseUrl relative to location of extended tsconfig" , ( ) => {
277291 const firstConfig = { compilerOptions : { baseUrl : "." } } ;
278292 const firstConfigPath = join ( "/root" , "first-config.json" ) ;
279293 const secondConfig = { extends : "../first-config.json" } ;
@@ -309,4 +323,94 @@ describe("loadConfig", () => {
309323 compilerOptions : { baseUrl : join ( ".." , ".." ) } ,
310324 } ) ;
311325 } ) ;
326+
327+ it ( "should load a config with array extends and overwrite all options" , ( ) => {
328+ const baseConfig1 = {
329+ compilerOptions : { baseUrl : "." , paths : { foo : [ "bar" ] } } ,
330+ } ;
331+ const baseConfig1Path = join ( "/root" , "base-config-1.json" ) ;
332+ const baseConfig2 = { compilerOptions : { baseUrl : "." } } ;
333+ const baseConfig2Path = join ( "/root" , "dir1" , "base-config-2.json" ) ;
334+ const baseConfig3 = {
335+ compilerOptions : { baseUrl : "." , paths : { foo : [ "bar2" ] } } ,
336+ } ;
337+ const baseConfig3Path = join ( "/root" , "dir1" , "dir2" , "base-config-3.json" ) ;
338+ const actualConfig = {
339+ extends : [
340+ "./base-config-1.json" ,
341+ "./dir1/base-config-2.json" ,
342+ "./dir1/dir2/base-config-3.json" ,
343+ ] ,
344+ } ;
345+ const actualConfigPath = join ( "/root" , "tsconfig.json" ) ;
346+
347+ const res = loadTsconfig (
348+ join ( "/root" , "tsconfig.json" ) ,
349+ ( path ) =>
350+ [
351+ baseConfig1Path ,
352+ baseConfig2Path ,
353+ baseConfig3Path ,
354+ actualConfigPath ,
355+ ] . indexOf ( path ) >= 0 ,
356+ ( path ) => {
357+ if ( path === baseConfig1Path ) {
358+ return JSON . stringify ( baseConfig1 ) ;
359+ }
360+ if ( path === baseConfig2Path ) {
361+ return JSON . stringify ( baseConfig2 ) ;
362+ }
363+ if ( path === baseConfig3Path ) {
364+ return JSON . stringify ( baseConfig3 ) ;
365+ }
366+ if ( path === actualConfigPath ) {
367+ return JSON . stringify ( actualConfig ) ;
368+ }
369+ return "" ;
370+ }
371+ ) ;
372+
373+ expect ( res ) . toEqual ( {
374+ extends : [
375+ "./base-config-1.json" ,
376+ "./dir1/base-config-2.json" ,
377+ "./dir1/dir2/base-config-3.json" ,
378+ ] ,
379+ compilerOptions : {
380+ baseUrl : join ( "dir1" , "dir2" ) ,
381+ paths : { foo : [ "bar2" ] } ,
382+ } ,
383+ } ) ;
384+ } ) ;
385+
386+ it ( "should load a config with array extends without .json extension" , ( ) => {
387+ const baseConfig = {
388+ compilerOptions : { baseUrl : "." , paths : { foo : [ "bar" ] } } ,
389+ } ;
390+ const baseConfigPath = join ( "/root" , "base-config-1.json" ) ;
391+ const actualConfig = { extends : [ "./base-config-1" ] } ;
392+ const actualConfigPath = join ( "/root" , "tsconfig.json" ) ;
393+
394+ const res = loadTsconfig (
395+ join ( "/root" , "tsconfig.json" ) ,
396+ ( path ) => [ baseConfigPath , actualConfigPath ] . indexOf ( path ) >= 0 ,
397+ ( path ) => {
398+ if ( path === baseConfigPath ) {
399+ return JSON . stringify ( baseConfig ) ;
400+ }
401+ if ( path === actualConfigPath ) {
402+ return JSON . stringify ( actualConfig ) ;
403+ }
404+ return "" ;
405+ }
406+ ) ;
407+
408+ expect ( res ) . toEqual ( {
409+ extends : [ "./base-config-1" ] ,
410+ compilerOptions : {
411+ baseUrl : "." ,
412+ paths : { foo : [ "bar" ] } ,
413+ } ,
414+ } ) ;
415+ } ) ;
312416} ) ;
0 commit comments