@@ -4,6 +4,9 @@ import path from 'path'
44import fs from 'fs'
55import { mkdirp } from 'mkdirp'
66import { fileURLToPath } from 'url'
7+ import sinon from 'sinon'
8+ import inquirer from 'inquirer'
9+ import * as initModule from '../../lib/command/init.js'
710
811const __filename = fileURLToPath ( import . meta. url )
912const __dirname = path . dirname ( __filename )
@@ -13,29 +16,24 @@ const codecept_dir = path.join(__dirname, '/../data/sandbox/configs/init')
1316describe ( 'Init Command' , function ( ) {
1417 this . timeout ( 20000 )
1518
19+ let promptStub ;
20+
1621 beforeEach ( ( ) => {
1722 mkdirp . sync ( codecept_dir )
1823 process . env . _INIT_DRY_RUN_INSTALL = true
24+ process . env . CODECEPT_TEST = 'true'
25+ promptStub = sinon . stub ( inquirer , 'prompt' )
1926 } )
2027
2128 afterEach ( ( ) => {
2229 try {
23- fs . unlinkSync ( `${ codecept_dir } /codecept.conf.ts` )
24- fs . unlinkSync ( `${ codecept_dir } /steps_file.ts` )
25- fs . unlinkSync ( `${ codecept_dir } /tsconfig.json` )
30+ fs . rmSync ( codecept_dir , { recursive : true , force : true } )
2631 } catch ( e ) {
2732 // continue regardless of error
2833 }
29-
30- try {
31- fs . unlinkSync ( `${ codecept_dir } /codecept.conf.js` )
32- fs . unlinkSync ( `${ codecept_dir } /steps_file.js` )
33- fs . unlinkSync ( `${ codecept_dir } /jsconfig.json` )
34- } catch ( e ) {
35- // continue regardless of error
36- }
37-
34+ sinon . restore ( )
3835 delete process . env . _INIT_DRY_RUN_INSTALL
36+ delete process . env . CODECEPT_TEST
3937 } )
4038
4139 it ( 'should have init command available and noTranslation defined' , async ( ) => {
@@ -63,4 +61,47 @@ describe('Init Command', function () {
6361 throw error
6462 }
6563 } )
64+
65+ it ( 'should initialize a JS project' , async ( ) => {
66+ // When yes: true, it skips prompts
67+ await initModule . default ( codecept_dir , { yes : true } )
68+
69+ fs . existsSync ( path . join ( codecept_dir , 'codecept.conf.js' ) ) . should . be . true
70+ fs . existsSync ( path . join ( codecept_dir , 'steps_file.js' ) ) . should . be . true
71+
72+ fs . readFile ( path . join ( codecept_dir , 'codecept.conf.js' ) , 'utf8' , ( err , data ) => {
73+ if ( err ) {
74+ throw Error ( err ) ;
75+ return ;
76+ }
77+ data . should . contain ( './steps_file.js' ) ;
78+ } ) ;
79+ } )
80+
81+ it ( 'should initialize a TS project' , async ( ) => {
82+ promptStub . onCall ( 0 ) . resolves ( {
83+ typescript : true ,
84+ tests : './tests/*_test.ts' ,
85+ helper : 'Playwright' ,
86+ output : './output' ,
87+ } )
88+ promptStub . onCall ( 1 ) . resolves ( {
89+ Playwright_browser : 'chromium' ,
90+ Playwright_url : 'http://localhost' ,
91+ Playwright_show : false ,
92+ } )
93+
94+ await initModule . default ( codecept_dir , { yes : false } )
95+
96+ fs . existsSync ( path . join ( codecept_dir , 'codecept.conf.ts' ) ) . should . be . true
97+ fs . existsSync ( path . join ( codecept_dir , 'steps_file.ts' ) ) . should . be . true
98+
99+ fs . readFile ( path . join ( codecept_dir , 'codecept.conf.ts' ) , 'utf8' , ( err , data ) => {
100+ if ( err ) {
101+ throw Error ( err ) ;
102+ return ;
103+ }
104+ data . should . contain ( './steps_file.ts' ) ;
105+ } ) ;
106+ } )
66107} )
0 commit comments