@@ -87,9 +87,10 @@ describe('create-package/utils', () => {
8787 nodeVersions : '>=18.0.0' ,
8888 } ;
8989
90- ( fs . promises . stat as jest . Mock ) . mockResolvedValueOnce ( {
91- isDirectory : ( ) => false ,
92- } ) ;
90+ const mockError = new Error ( 'Not found' ) as NodeJS . ErrnoException ;
91+ mockError . code = 'ENOENT' ;
92+
93+ jest . spyOn ( fs . promises , 'stat' ) . mockRejectedValue ( mockError ) ;
9394
9495 ( fsUtils . readAllFiles as jest . Mock ) . mockResolvedValueOnce ( {
9596 'src/index.ts' : 'export default 42;' ,
@@ -170,9 +171,7 @@ describe('create-package/utils', () => {
170171 nodeVersions : '20.0.0' ,
171172 } ;
172173
173- ( fs . promises . stat as jest . Mock ) . mockResolvedValueOnce ( {
174- isDirectory : ( ) => true ,
175- } ) ;
174+ ( fs . promises . stat as jest . Mock ) . mockResolvedValue ( { } ) ;
176175
177176 await expect (
178177 finalizeAndWriteData ( packageData , monorepoFileData ) ,
@@ -181,5 +180,34 @@ describe('create-package/utils', () => {
181180 expect ( fs . promises . mkdir ) . not . toHaveBeenCalled ( ) ;
182181 expect ( fs . promises . writeFile ) . not . toHaveBeenCalled ( ) ;
183182 } ) ;
183+
184+ it ( 'throws if fs.stat fails with an error other than ENOENT' , async ( ) => {
185+ const mockError = new Error ( 'Permission denied' ) as NodeJS . ErrnoException ;
186+ mockError . code = 'EACCES' ;
187+
188+ jest . spyOn ( fs . promises , 'stat' ) . mockRejectedValue ( mockError ) ;
189+
190+ const packageData : PackageData = {
191+ name : '@metamask/foo' ,
192+ description : 'A foo package.' ,
193+ directoryName : 'foo' ,
194+ nodeVersions : '20.0.0' ,
195+ currentYear : '2023' ,
196+ } ;
197+
198+ const monorepoFileData = {
199+ tsConfig : {
200+ references : [ { path : './packages/bar' } ] ,
201+ } ,
202+ tsConfigBuild : {
203+ references : [ { path : './packages/bar' } ] ,
204+ } ,
205+ nodeVersions : '20.0.0' ,
206+ } ;
207+
208+ await expect (
209+ finalizeAndWriteData ( packageData , monorepoFileData ) ,
210+ ) . rejects . toThrow ( 'Permission denied' ) ;
211+ } ) ;
184212 } ) ;
185213} ) ;
0 commit comments