1
- 'use strict' ;
2
-
3
- import { run , rmTempDir , runPromptWithAnswers } from '../../jest/helpers' ;
1
+ import { run , runPromptWithAnswers } from '../../jest/helpers' ;
4
2
import { fetchProjectConfig } from '../../src/utils/helpers' ;
5
3
6
4
import { DOWN , ENTER } from 'cli-prompts-test' ;
7
5
import fs from 'fs' ;
8
6
import path from 'path' ;
9
7
8
+ // Utility for robust cleanup
9
+ const rmDirIfExists = ( dirPath ) => {
10
+ if ( fs . existsSync ( dirPath ) ) {
11
+ fs . rmSync ( dirPath , { recursive : true , force : true } ) ;
12
+ }
13
+ } ;
14
+
10
15
describe ( 'mevn init' , ( ) => {
11
16
const tempDirPath = path . join ( __dirname , 'init-cmd' ) ;
12
17
const genPath = path . join ( tempDirPath , 'my-app' ) ;
13
-
14
18
const clientPath = path . join ( genPath , 'client' ) ;
15
19
const serverPath = path . join ( genPath , 'server' ) ;
16
20
17
- // Cleanup
21
+ // Cleanup and setup before all tests
18
22
beforeAll ( ( ) => {
19
- rmTempDir ( tempDirPath ) ;
23
+ rmDirIfExists ( tempDirPath ) ;
20
24
fs . mkdirSync ( tempDirPath ) ;
21
25
} ) ;
22
26
23
- afterAll ( ( ) => rmTempDir ( tempDirPath ) ) ;
27
+ // Final cleanup after all tests
28
+ afterAll ( ( ) => rmDirIfExists ( tempDirPath ) ) ;
29
+
30
+ // Extra per-test cleanup
31
+ afterEach ( ( ) => {
32
+ // Each test is responsible for its own output, but this catches anything left over.
33
+ rmDirIfExists ( genPath ) ;
34
+ } ) ;
24
35
25
36
it ( 'shows an appropriate warning if multiple arguments were provided with init' , ( ) => {
26
37
const { exitCode, stderr } = run ( [ 'init' , 'my-app' , 'stray-arg' ] , {
27
38
reject : false ,
28
39
} ) ;
29
-
30
40
expect ( exitCode ) . toBe ( 1 ) ;
31
41
expect ( stderr ) . toContain (
32
42
'Error: Kindly provide only one argument as the directory name!!' ,
33
43
) ;
34
44
} ) ;
35
45
36
46
it ( 'creates a new MEVN stack webapp based on the Nuxt.js starter template' , async ( ) => {
47
+ rmDirIfExists ( genPath ) ;
37
48
const { exitCode } = await runPromptWithAnswers (
38
49
[ 'init' , 'my-app' ] ,
39
50
[
@@ -45,7 +56,6 @@ describe('mevn init', () => {
45
56
] ,
46
57
tempDirPath ,
47
58
) ;
48
-
49
59
expect ( exitCode ) . toBe ( 0 ) ;
50
60
51
61
// nuxt.config.js
@@ -58,10 +68,7 @@ describe('mevn init', () => {
58
68
// .mevnrc
59
69
const projectConfigContent = {
60
70
deployTarget : 'static' ,
61
- isConfigured : {
62
- client : false ,
63
- server : false ,
64
- } ,
71
+ isConfigured : { client : false , server : false } ,
65
72
modules : [ ] ,
66
73
name : 'my-app' ,
67
74
packageManager : 'npm' ,
@@ -75,6 +82,9 @@ describe('mevn init', () => {
75
82
} ) ;
76
83
77
84
it ( 'shows an appropriate warning if the specified directory already exists in path' , ( ) => {
85
+ rmDirIfExists ( genPath ) ;
86
+ fs . mkdirSync ( genPath ) ;
87
+
78
88
const { exitCode, stderr } = run ( [ 'init' , 'my-app' ] , {
79
89
cwd : tempDirPath ,
80
90
reject : false ,
@@ -85,23 +95,27 @@ describe('mevn init', () => {
85
95
} ) ;
86
96
87
97
it ( 'shows an appropriate warning if creating an application within a non-empty path' , ( ) => {
98
+ rmDirIfExists ( genPath ) ;
99
+ fs . mkdirSync ( genPath ) ; // create target directory
100
+
101
+ // Add dummy file so it's not empty
102
+ fs . writeFileSync ( path . join ( genPath , 'dummy.txt' ) , 'not empty' ) ;
103
+
88
104
const { exitCode, stderr } = run ( [ 'init' , '.' ] , {
89
105
cwd : genPath ,
90
106
reject : false ,
91
107
} ) ;
92
108
expect ( exitCode ) . toBe ( 1 ) ;
93
109
expect ( stderr ) . toContain ( `It seems the current directory isn't empty.` ) ;
94
-
95
- // Delete the generated directory
96
- rmTempDir ( genPath ) ;
97
110
} ) ;
98
111
99
112
it ( 'creates a new MEVN stack webapp based on the GraphQL starter template' , async ( ) => {
113
+ rmDirIfExists ( genPath ) ;
100
114
const { exitCode } = await runPromptWithAnswers (
101
115
[ 'init' , 'my-app' ] ,
102
116
[
103
117
`${ DOWN } ${ DOWN } ${ ENTER } ` , // Choose GraphQL as the starter template
104
- ENTER , // Requires server directory,
118
+ ENTER , // Requires server directory
105
119
ENTER , // Choose npm as the package manager
106
120
] ,
107
121
tempDirPath ,
@@ -118,12 +132,10 @@ describe('mevn init', () => {
118
132
119
133
// Check whether if the respective directory have been generated
120
134
expect ( fs . existsSync ( path . join ( serverPath , 'graphql' ) ) ) . toBeTruthy ( ) ;
121
-
122
- // Delete the generated directory
123
- rmTempDir ( genPath ) ;
124
135
} ) ;
125
136
126
137
it ( 'creates a new MEVN stack webapp based on the PWA starter template' , async ( ) => {
138
+ rmDirIfExists ( genPath ) ;
127
139
const { exitCode } = await runPromptWithAnswers (
128
140
[ 'init' , 'my-app' ] ,
129
141
[
@@ -154,13 +166,10 @@ describe('mevn init', () => {
154
166
expect (
155
167
fs . existsSync ( path . join ( clientPath , 'src' , 'registerServiceWorker.js' ) ) ,
156
168
) . toBeTruthy ( ) ;
157
-
158
- // Delete the generated directory
159
- rmTempDir ( genPath ) ;
160
169
} ) ;
161
170
162
171
it ( 'creates a new MEVN stack webapp based on the Default starter template in current directory' , async ( ) => {
163
- // Create my-app directory
172
+ rmDirIfExists ( genPath ) ;
164
173
fs . mkdirSync ( genPath ) ;
165
174
166
175
const { exitCode } = await runPromptWithAnswers (
@@ -183,6 +192,6 @@ describe('mevn init', () => {
183
192
expect ( fs . existsSync ( path . join ( clientPath , '.gitignore' ) ) ) . toBeTruthy ( ) ;
184
193
185
194
// Check whether if the respective directory have been generated
186
- expect ( fs . existsSync ( path . join ( serverPath ) ) ) . toBeTruthy ( ) ;
195
+ expect ( fs . existsSync ( serverPath ) ) . toBeTruthy ( ) ;
187
196
} ) ;
188
197
} ) ;
0 commit comments