66 */
77/* eslint-disable @typescript-eslint/no-var-requires */
88const fs = require ( 'fs' )
9+ const { execSync } = require ( 'child_process' )
910
1011jest . mock ( '../assets/plugin-config' , ( ) => ( {
1112 plugins : {
@@ -19,6 +20,37 @@ jest.mock('../assets/plugin-config', () => ({
1920} ) )
2021
2122jest . mock ( 'fs' )
23+ jest . mock ( 'child_process' )
24+
25+ // custom matcher to compare strings line by line with trimming
26+ expect . extend ( {
27+ toEqualTrimmedLines ( received , expected ) {
28+ const clean = str =>
29+ str
30+ . split ( '\n' )
31+ . map ( line => line . trim ( ) ) // Trim each line
32+ . filter ( line => line . length > 0 ) // Optional: remove empty lines
33+
34+ const receivedLines = clean ( received )
35+ const expectedLines = clean ( expected )
36+
37+ const pass = this . equals ( receivedLines , expectedLines )
38+
39+ if ( pass ) {
40+ return {
41+ pass : true ,
42+ message : ( ) =>
43+ `✅ Expected strings not to match line by line (but they did).\n\nExpected: ${ this . utils . printExpected ( expectedLines ) } \nReceived: ${ this . utils . printReceived ( receivedLines ) } ` ,
44+ } ;
45+ } else {
46+ return {
47+ pass : false ,
48+ message : ( ) =>
49+ `❌ Expected strings to match line by line (with trimming).\n\nExpected: ${ this . utils . printExpected ( expectedLines ) } \nReceived: ${ this . utils . printReceived ( receivedLines ) } ` ,
50+ } ;
51+ }
52+ } ,
53+ } )
2254
2355const trimExtensions = require ( './trim-extensions' )
2456
@@ -47,6 +79,7 @@ describe('trim-extensions', () => {
4779 }
4880 } )
4981 fs . unlinkSync . mockReturnValue ( true )
82+ execSync . mockReturnValue ( true )
5083 } )
5184
5285 it ( 'handles OR operator correctly' , ( ) => {
@@ -62,8 +95,9 @@ describe('trim-extensions', () => {
6295 trimExtensions ( '/mock/dir' , { SFDC_EXT_featureA : true , SFDC_EXT_featureB : false } )
6396 expect ( fs . writeFileSync ) . toHaveBeenCalledWith (
6497 expect . any ( String ) ,
65- "const feature = 'Feature Enabled';"
98+ expect . toEqualTrimmedLines ( "const feature = 'Feature Enabled';" )
6699 )
100+ expect ( execSync ) . toHaveBeenCalledWith ( 'npx prettier --write /mock/dir/src/components/featureComponent.jsx' )
67101 } )
68102
69103 it ( 'handles variable declarations correctly' , ( ) => {
@@ -82,12 +116,13 @@ describe('trim-extensions', () => {
82116
83117 expect ( fs . writeFileSync ) . toHaveBeenCalledWith (
84118 expect . any ( String ) ,
85- "const featureAFunc = () => 'Feature A';"
119+ expect . toEqualTrimmedLines ( "const featureAFunc = () => 'Feature A';" )
86120 )
87121 expect ( fs . writeFileSync ) . toHaveBeenCalledWith (
88122 expect . any ( String ) ,
89123 expect . not . stringContaining ( "const featureBFunc = () => 'Feature B';" )
90124 )
125+ expect ( execSync ) . toHaveBeenCalledWith ( 'npx prettier --write /mock/dir/src/components/featureComponent.jsx' )
91126 } )
92127
93128 it ( 'handles variable with ternary expressions correctly' , ( ) => {
@@ -104,12 +139,13 @@ describe('trim-extensions', () => {
104139
105140 expect ( fs . writeFileSync ) . toHaveBeenCalledWith (
106141 expect . any ( String ) ,
107- ' const showFeature = Feature_A;'
142+ expect . toEqualTrimmedLines ( " const showFeature = Feature_A;" )
108143 )
109144 expect ( fs . writeFileSync ) . toHaveBeenCalledWith (
110145 expect . any ( String ) ,
111146 expect . not . stringContaining ( 'const showFeature = Feature_B' )
112147 )
148+ expect ( execSync ) . toHaveBeenCalledWith ( 'npx prettier --write /mock/dir/src/components/featureComponent.jsx' )
113149 } )
114150
115151 it ( 'handles return with ternary expressions correctly' , ( ) => {
@@ -122,10 +158,56 @@ describe('trim-extensions', () => {
122158
123159 trimExtensions ( '/mock/dir' , { SFDC_EXT_featureA : true } )
124160
161+ const expected = `
162+ function test() {
163+ return Feature_A;
164+ }
165+ `
125166 expect ( fs . writeFileSync ) . toHaveBeenCalledWith (
126167 expect . any ( String ) ,
127- expect . stringContaining ( 'return Feature_A' )
168+ expect . toEqualTrimmedLines ( expected )
128169 )
170+ expect ( execSync ) . toHaveBeenCalledWith ( 'npx prettier --write /mock/dir/src/components/featureComponent.jsx' )
171+ } )
172+
173+ it ( 'handles PropTypes declarations correctly' , ( ) => {
174+ const code = `
175+ MyClass.PropTypes = {
176+ name: PropTypes.string,
177+ description: PropTypes.string
178+ };
179+ SFDC_EXT_featureA && (MyClass.PropType = {
180+ ...MyClass.PropType,
181+ featureAProp: PropTypes.string
182+ });
183+ SFDC_EXT_featureB && (MyClass.PropType = {
184+ ...MyClass.PropType,
185+ featureBProp: PropTypes.string
186+ });
187+ `
188+ fs . readFileSync . mockReturnValue ( code )
189+
190+ trimExtensions ( '/mock/dir' , { SFDC_EXT_featureA : true } )
191+
192+ const expected = `
193+ MyClass.PropTypes = {
194+ name: PropTypes.string,
195+ description: PropTypes.string
196+ };
197+ MyClass.PropType = {
198+ ...MyClass.PropType,
199+ featureAProp: PropTypes.string
200+ };
201+ `
202+ expect ( fs . writeFileSync ) . toHaveBeenCalledWith (
203+ expect . any ( String ) ,
204+ expect . toEqualTrimmedLines ( expected )
205+ )
206+ expect ( fs . writeFileSync ) . not . toHaveBeenCalledWith (
207+ expect . any ( String ) ,
208+ expect . stringContaining ( 'featureBProp: PropTypes.string' )
209+ )
210+ expect ( execSync ) . toHaveBeenCalledWith ( 'npx prettier --write /mock/dir/src/components/featureComponent.jsx' )
129211 } )
130212
131213 it ( 'handles JSX elements in return statements correctly' , ( ) => {
@@ -143,14 +225,23 @@ describe('trim-extensions', () => {
143225
144226 trimExtensions ( '/mock/dir' , { SFDC_EXT_featureA : true , SFDC_EXT_featureB : false } )
145227
228+ const expected = `
229+ function test() {
230+ return (
231+ <div>
232+ <ComponentA />
233+ </div>);
234+ }
235+ `
146236 expect ( fs . writeFileSync ) . toHaveBeenCalledWith (
147237 expect . any ( String ) ,
148- expect . stringContaining ( '<ComponentA />' )
238+ expect . toEqualTrimmedLines ( expected )
149239 )
150240 expect ( fs . writeFileSync ) . not . toHaveBeenCalledWith (
151241 expect . any ( String ) ,
152242 expect . stringContaining ( '<ComponentB />' )
153243 )
244+ expect ( execSync ) . toHaveBeenCalledWith ( 'npx prettier --write /mock/dir/src/components/featureComponent.jsx' )
154245 } )
155246
156247 it ( 'does not remove referenced imports' , ( ) => {
0 commit comments