11/* eslint-disable sonarjs/no-duplicate-string */
2+ const fs = require ( 'fs' ) ;
23const path = require ( 'path' ) ;
34const Generator = require ( '../lib/generator' ) ;
45const log = require ( 'loglevel' ) ;
@@ -54,7 +55,7 @@ describe('Utils', () => {
5455 expect ( log . debug ) . toHaveBeenCalledWith ( logMessage . templateNotFound ( templateNpmName ) ) ;
5556 } ) ;
5657
57- it ( 'doesnt work with a url' , async ( ) => {
58+ it ( 'doesnot work with a url' , async ( ) => {
5859 resolvePkg . __resolvePkgValue = undefined ;
5960 resolveFrom . __resolveFromValue = undefined ;
6061 const result = utils . getTemplateDetails ( templateNpmName , 'package.json' ) ;
@@ -63,18 +64,38 @@ describe('Utils', () => {
6364 } ) ;
6465
6566 describe ( '#exists' , ( ) => {
66- it ( 'should return true if file exist' , async ( ) => {
67- const exists = await utils . exists ( `${ process . cwd ( ) } /package.json` ) ;
68- expect ( exists ) . toBeTruthy ( ) ;
67+ afterEach ( ( ) => {
68+ jest . restoreAllMocks ( ) ;
69+ } ) ;
70+
71+ it ( 'should return true if file exists' , async ( ) => {
72+ jest . spyOn ( fs . promises , 'stat' ) . mockResolvedValueOnce ( { } ) ;
73+
74+ const exists = await utils . exists ( 'existing-file' ) ;
75+
76+ expect ( fs . promises . stat ) . toHaveBeenCalledWith (
77+ 'existing-file' ,
78+ fs . constants . F_OK
79+ ) ;
80+ expect ( exists ) . toBe ( true ) ;
6981 } ) ;
7082
7183 it ( 'should return false if file does not exist' , async ( ) => {
72- const exists = await utils . exists ( './invalid-file' ) ;
73- expect ( exists ) . toBeFalsy ( ) ;
84+ jest . spyOn ( fs . promises , 'stat' ) . mockRejectedValueOnce (
85+ new Error ( 'File not found' )
86+ ) ;
87+
88+ const exists = await utils . exists ( 'non-existing-file' ) ;
89+
90+ expect ( fs . promises . stat ) . toHaveBeenCalledWith (
91+ 'non-existing-file' ,
92+ fs . constants . F_OK
93+ ) ;
94+ expect ( exists ) . toBe ( false ) ;
7495 } ) ;
7596 } ) ;
7697
77- describe ( '#isJsFile' , ( ) => {
98+ describe ( '#isJsFile' , ( ) => {
7899 it ( 'should return true if file extension is .js' , ( ) => {
79100 const isJsFile = utils . isJsFile ( './valid-file.js' ) ;
80101 expect ( isJsFile ) . toBeTruthy ( ) ;
0 commit comments