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 ( 'does not work with a url' , async ( ) => {
5859 resolvePkg . __resolvePkgValue = undefined ;
5960 resolveFrom . __resolveFromValue = undefined ;
6061 const result = utils . getTemplateDetails ( templateNpmName , 'package.json' ) ;
@@ -63,18 +64,44 @@ 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+ const error = new Error ( 'File not found' ) ;
85+
86+ jest . spyOn ( fs . promises , 'stat' ) . mockRejectedValueOnce ( error ) ;
87+ jest . spyOn ( log , 'debug' ) . mockImplementation ( ( ) => { } ) ;
88+
89+ const exists = await utils . exists ( 'non-existing-file' ) ;
90+
91+ expect ( fs . promises . stat ) . toHaveBeenCalledWith (
92+ 'non-existing-file' ,
93+ fs . constants . F_OK
94+ ) ;
95+
96+ expect ( log . debug ) . toHaveBeenCalledWith (
97+ `File non-existing-file couldn't be found. Error: ${ error . message } `
98+ ) ;
99+
100+ expect ( exists ) . toBe ( false ) ;
74101 } ) ;
75102 } ) ;
76103
77- describe ( '#isJsFile' , ( ) => {
104+ describe ( '#isJsFile' , ( ) => {
78105 it ( 'should return true if file extension is .js' , ( ) => {
79106 const isJsFile = utils . isJsFile ( './valid-file.js' ) ;
80107 expect ( isJsFile ) . toBeTruthy ( ) ;
0 commit comments