@@ -152,6 +152,92 @@ describe('create-package/utils', () => {
152152 } ) ;
153153 } ) ;
154154
155+ it ( 'should write the expected files if the directory does not exist' , async ( ) => {
156+ const packageData : PackageData = {
157+ name : '@metamask/foo' ,
158+ description : 'A foo package.' ,
159+ directoryName : 'foo' ,
160+ nodeVersions : '>=18.0.0' ,
161+ currentYear : '2023' ,
162+ } ;
163+
164+ const monorepoFileData = {
165+ tsConfig : {
166+ references : [ { path : './packages/bar' } ] ,
167+ } ,
168+ tsConfigBuild : {
169+ references : [ { path : './packages/bar' } ] ,
170+ } ,
171+ nodeVersions : '>=18.0.0' ,
172+ } ;
173+
174+ const mockError = new Error ( 'Not found' ) as NodeJS . ErrnoException ;
175+ mockError . code = 'ENOENT' ;
176+
177+ ( fs . promises . stat as jest . Mock ) . mockResolvedValueOnce ( {
178+ isDirectory : ( ) => false ,
179+ } ) ;
180+
181+ ( fsUtils . readAllFiles as jest . Mock ) . mockResolvedValueOnce ( {
182+ 'src/index.ts' : 'export default 42;' ,
183+ 'src/index.test.ts' : 'export default 42;' ,
184+ 'mock1.file' :
185+ 'CURRENT_YEAR NODE_VERSIONS PACKAGE_NAME PACKAGE_DESCRIPTION PACKAGE_DIRECTORY_NAME' ,
186+ 'mock2.file' : 'CURRENT_YEAR NODE_VERSIONS PACKAGE_NAME' ,
187+ 'mock3.file' : 'PACKAGE_DESCRIPTION PACKAGE_DIRECTORY_NAME' ,
188+ } ) ;
189+
190+ ( prettier . format as jest . Mock ) . mockImplementation ( ( input ) => input ) ;
191+
192+ await finalizeAndWriteData ( packageData , monorepoFileData ) ;
193+
194+ // processTemplateFiles and writeFiles
195+ expect ( fsUtils . readAllFiles ) . toHaveBeenCalledTimes ( 1 ) ;
196+ expect ( fsUtils . readAllFiles ) . toHaveBeenCalledWith (
197+ expect . stringMatching ( / \/ p a c k a g e - t e m p l a t e $ / u) ,
198+ ) ;
199+
200+ expect ( fsUtils . writeFiles ) . toHaveBeenCalledTimes ( 1 ) ;
201+ expect ( fsUtils . writeFiles ) . toHaveBeenCalledWith (
202+ expect . stringMatching ( / p a c k a g e s \/ f o o $ / u) ,
203+ {
204+ 'src/index.ts' : 'export default 42;' ,
205+ 'src/index.test.ts' : 'export default 42;' ,
206+ 'mock1.file' : '2023 >=18.0.0 @metamask/foo A foo package. foo' ,
207+ 'mock2.file' : '2023 >=18.0.0 @metamask/foo' ,
208+ 'mock3.file' : 'A foo package. foo' ,
209+ } ,
210+ ) ;
211+
212+ // Writing monorepo files
213+ expect ( fs . promises . writeFile ) . toHaveBeenCalledTimes ( 2 ) ;
214+ expect ( prettier . format ) . toHaveBeenCalledTimes ( 2 ) ;
215+ expect ( fs . promises . writeFile ) . toHaveBeenCalledWith (
216+ expect . stringMatching ( / t s c o n f i g \. j s o n $ / u) ,
217+ JSON . stringify ( {
218+ references : [ { path : './packages/bar' } , { path : './packages/foo' } ] ,
219+ } ) ,
220+ ) ;
221+ expect ( fs . promises . writeFile ) . toHaveBeenCalledWith (
222+ expect . stringMatching ( / t s c o n f i g \. b u i l d \. j s o n $ / u) ,
223+ JSON . stringify ( {
224+ references : [
225+ { path : './packages/bar' } ,
226+ { path : './packages/foo/tsconfig.build.json' } ,
227+ ] ,
228+ } ) ,
229+ ) ;
230+
231+ // Postprocessing
232+ expect ( execa ) . toHaveBeenCalledTimes ( 2 ) ;
233+ expect ( execa ) . toHaveBeenCalledWith ( 'yarn' , [ 'install' ] , {
234+ cwd : expect . any ( String ) ,
235+ } ) ;
236+ expect ( execa ) . toHaveBeenCalledWith ( 'yarn' , [ 'update-readme-content' ] , {
237+ cwd : expect . any ( String ) ,
238+ } ) ;
239+ } ) ;
240+
155241 it ( 'throws if the package directory already exists' , async ( ) => {
156242 const packageData : PackageData = {
157243 name : '@metamask/foo' ,
0 commit comments