@@ -37,7 +37,7 @@ describe("tsconfig", () => {
3737 } ) ;
3838
3939 describe ( "update" , ( ) => {
40- test ( "adds strictNullChecks to tsconfig " , async ( ) => {
40+ test ( "skips modification when strict: true is already set " , async ( ) => {
4141 const mockWriteFile = mock ( ( ) => Promise . resolve ( ) ) ;
4242 mock . module ( "glob" , ( ) => ( {
4343 glob : mock ( ( ) => Promise . resolve ( [ "tsconfig.json" ] ) ) ,
@@ -52,11 +52,75 @@ describe("tsconfig", () => {
5252
5353 await tsconfig . update ( ) ;
5454
55+ // Should not write because strict: true already enables strictNullChecks
56+ expect ( mockWriteFile ) . not . toHaveBeenCalled ( ) ;
57+ } ) ;
58+
59+ test ( "skips modification when strictNullChecks: true is already set" , async ( ) => {
60+ const mockWriteFile = mock ( ( ) => Promise . resolve ( ) ) ;
61+ mock . module ( "glob" , ( ) => ( {
62+ glob : mock ( ( ) => Promise . resolve ( [ "tsconfig.json" ] ) ) ,
63+ } ) ) ;
64+ mock . module ( "node:fs/promises" , ( ) => ( {
65+ access : mock ( ( ) => Promise . resolve ( ) ) ,
66+ readFile : mock ( ( ) =>
67+ Promise . resolve ( '{"compilerOptions": {"strictNullChecks": true}}' )
68+ ) ,
69+ writeFile : mockWriteFile ,
70+ } ) ) ;
71+
72+ await tsconfig . update ( ) ;
73+
74+ // Should not write because strictNullChecks is already true
75+ expect ( mockWriteFile ) . not . toHaveBeenCalled ( ) ;
76+ } ) ;
77+
78+ test ( "adds strictNullChecks when not present" , async ( ) => {
79+ const mockWriteFile = mock ( ( ) => Promise . resolve ( ) ) ;
80+ mock . module ( "glob" , ( ) => ( {
81+ glob : mock ( ( ) => Promise . resolve ( [ "tsconfig.json" ] ) ) ,
82+ } ) ) ;
83+ mock . module ( "node:fs/promises" , ( ) => ( {
84+ access : mock ( ( ) => Promise . resolve ( ) ) ,
85+ readFile : mock ( ( ) =>
86+ Promise . resolve ( '{"compilerOptions": {"target": "ES2020"}}' )
87+ ) ,
88+ writeFile : mockWriteFile ,
89+ } ) ) ;
90+
91+ await tsconfig . update ( ) ;
92+
5593 expect ( mockWriteFile ) . toHaveBeenCalled ( ) ;
5694 const writeCall = mockWriteFile . mock . calls [ 0 ] ;
5795 const writtenContent = JSON . parse ( writeCall [ 1 ] as string ) ;
5896 expect ( writtenContent . compilerOptions . strictNullChecks ) . toBe ( true ) ;
59- expect ( writtenContent . compilerOptions . strict ) . toBe ( true ) ;
97+ expect ( writtenContent . compilerOptions . target ) . toBe ( "ES2020" ) ;
98+ } ) ;
99+
100+ test ( "preserves comments when modifying tsconfig" , async ( ) => {
101+ const mockWriteFile = mock ( ( ) => Promise . resolve ( ) ) ;
102+ const tsconfigWithComments = `{
103+ // This is a comment
104+ "compilerOptions": {
105+ "target": "ES2020"
106+ }
107+ }` ;
108+ mock . module ( "glob" , ( ) => ( {
109+ glob : mock ( ( ) => Promise . resolve ( [ "tsconfig.json" ] ) ) ,
110+ } ) ) ;
111+ mock . module ( "node:fs/promises" , ( ) => ( {
112+ access : mock ( ( ) => Promise . resolve ( ) ) ,
113+ readFile : mock ( ( ) => Promise . resolve ( tsconfigWithComments ) ) ,
114+ writeFile : mockWriteFile ,
115+ } ) ) ;
116+
117+ await tsconfig . update ( ) ;
118+
119+ expect ( mockWriteFile ) . toHaveBeenCalled ( ) ;
120+ const writeCall = mockWriteFile . mock . calls [ 0 ] ;
121+ const writtenContent = writeCall [ 1 ] as string ;
122+ // Comments should be preserved
123+ expect ( writtenContent ) . toContain ( "// This is a comment" ) ;
60124 } ) ;
61125
62126 test ( "updates multiple tsconfig files" , async ( ) => {
0 commit comments