1
1
'use strict' ;
2
2
3
3
const execa = require ( 'execa' ) ;
4
- const { mkdirp, writeFileSync, existsSync } = require ( 'fs-extra' ) ;
4
+ const { mkdirp, writeFileSync, existsSync, readdirSync } = require ( 'fs-extra' ) ;
5
5
const { join } = require ( 'path' ) ;
6
6
const { rmSync } = require ( 'fs' ) ;
7
7
8
8
const cli = require . resolve ( 'create-react-app/index.js' ) ;
9
9
10
- jest . setTimeout ( 1000 * 60 * 5 ) ;
10
+ jest . setTimeout ( 1000 * 60 * 10 ) ;
11
11
12
12
const projectName = 'test-app' ;
13
13
const genPath = join ( __dirname , projectName ) ;
14
14
15
15
const generatedFiles = [
16
16
'.gitignore' ,
17
+ 'README.md' ,
18
+ 'node_modules' ,
17
19
'package.json' ,
20
+ 'public' ,
18
21
'src' ,
19
22
'package-lock.json' ,
20
23
] ;
@@ -46,10 +49,17 @@ const run = async (args, options) => {
46
49
const childProcessResult = await result ;
47
50
process . stdout . write ( `ExitCode: ${ childProcessResult . exitCode } \n` ) ;
48
51
process . stdout . write ( '::endgroup::\n' ) ;
49
- return childProcessResult ;
52
+ const files = existsSync ( genPath )
53
+ ? readdirSync ( genPath ) . filter ( f => existsSync ( join ( genPath , f ) ) )
54
+ : null ;
55
+ return {
56
+ ...childProcessResult ,
57
+ files,
58
+ } ;
50
59
} ;
51
60
52
- const genFileExists = f => existsSync ( join ( genPath , f ) ) ;
61
+ const expectAllFiles = ( arr1 , arr2 ) =>
62
+ expect ( [ ...arr1 ] . sort ( ) ) . toEqual ( [ ...arr2 ] . sort ( ) ) ;
53
63
54
64
describe ( 'create-react-app' , ( ) => {
55
65
it ( 'check yarn installation' , async ( ) => {
@@ -60,21 +70,22 @@ describe('create-react-app', () => {
60
70
} ) ;
61
71
62
72
it ( 'asks to supply an argument if none supplied' , async ( ) => {
63
- const { exitCode, stderr } = await run ( [ ] , { reject : false } ) ;
73
+ const { exitCode, stderr, files } = await run ( [ ] , { reject : false } ) ;
64
74
65
75
// Assertions
66
76
expect ( exitCode ) . toBe ( 1 ) ;
67
77
expect ( stderr ) . toContain ( 'Please specify the project directory' ) ;
78
+ expect ( files ) . toBe ( null ) ;
68
79
} ) ;
69
80
70
81
it ( 'creates a project on supplying a name as the argument' , async ( ) => {
71
- const { exitCode } = await run ( [ projectName ] , { cwd : __dirname } ) ;
82
+ const { exitCode, files } = await run ( [ projectName ] , { cwd : __dirname } ) ;
72
83
73
84
// Assert for exit code
74
85
expect ( exitCode ) . toBe ( 0 ) ;
75
86
76
87
// Assert for the generated files
77
- generatedFiles . forEach ( file => expect ( genFileExists ( file ) ) . toBeTruthy ( ) ) ;
88
+ expectAllFiles ( files , generatedFiles ) ;
78
89
} ) ;
79
90
80
91
it ( 'warns about conflicting files in path' , async ( ) => {
@@ -85,7 +96,7 @@ describe('create-react-app', () => {
85
96
const pkgJson = join ( genPath , 'package.json' ) ;
86
97
writeFileSync ( pkgJson , '{ "foo": "bar" }' ) ;
87
98
88
- const { exitCode, stdout } = await run ( [ projectName ] , {
99
+ const { exitCode, stdout, files } = await run ( [ projectName ] , {
89
100
cwd : __dirname ,
90
101
reject : false ,
91
102
} ) ;
@@ -97,57 +108,54 @@ describe('create-react-app', () => {
97
108
expect ( stdout ) . toContain (
98
109
`The directory ${ projectName } contains files that could conflict`
99
110
) ;
111
+
112
+ // Existing file is still there
113
+ expectAllFiles ( files , [ 'package.json' ] ) ;
100
114
} ) ;
101
115
102
116
it ( 'creates a project in the current directory' , async ( ) => {
103
117
// Create temporary directory
104
118
await mkdirp ( genPath ) ;
105
119
106
120
// Create a project in the current directory
107
- const { exitCode } = await run ( [ '.' ] , { cwd : genPath } ) ;
121
+ const { exitCode, files } = await run ( [ '.' ] , { cwd : genPath } ) ;
108
122
109
123
// Assert for exit code
110
124
expect ( exitCode ) . toBe ( 0 ) ;
111
125
112
126
// Assert for the generated files
113
- generatedFiles . forEach ( file => expect ( genFileExists ( file ) ) . toBeTruthy ( ) ) ;
127
+ expectAllFiles ( files , generatedFiles ) ;
114
128
} ) ;
115
129
116
- it (
117
- 'uses yarn as the package manager' ,
118
- async ( ) => {
119
- const { exitCode } = await run ( [ projectName ] , {
120
- cwd : __dirname ,
121
- env : { npm_config_user_agent : 'yarn' } ,
122
- } ) ;
123
-
124
- // Assert for exit code
125
- expect ( exitCode ) . toBe ( 0 ) ;
126
-
127
- // Assert for the generated files
128
- const generatedFilesWithYarn = [
129
- ...generatedFiles . filter ( file => file !== 'package-lock.json' ) ,
130
- 'yarn.lock' ,
131
- ] ;
132
-
133
- generatedFilesWithYarn . forEach ( file =>
134
- expect ( genFileExists ( file ) ) . toBeTruthy ( )
135
- ) ;
136
- } ,
137
- 1000 * 60 * 10
138
- ) ;
139
-
140
- it ( 'creates a project based on the typescript template' , async ( ) => {
141
- const { exitCode } = await run ( [ projectName , '--template' , 'typescript' ] , {
130
+ it ( 'uses yarn as the package manager' , async ( ) => {
131
+ const { exitCode, files } = await run ( [ projectName ] , {
142
132
cwd : __dirname ,
133
+ env : { npm_config_user_agent : 'yarn' } ,
143
134
} ) ;
144
135
145
136
// Assert for exit code
146
137
expect ( exitCode ) . toBe ( 0 ) ;
147
138
148
139
// Assert for the generated files
149
- [ ...generatedFiles , 'tsconfig.json' ] . forEach ( file =>
150
- expect ( genFileExists ( file ) ) . toBeTruthy ( )
140
+ const generatedFilesWithYarn = generatedFiles . map ( file =>
141
+ file === 'package-lock.json' ? 'yarn.lock' : file
142
+ ) ;
143
+
144
+ expectAllFiles ( files , generatedFilesWithYarn ) ;
145
+ } ) ;
146
+
147
+ it ( 'creates a project based on the typescript template' , async ( ) => {
148
+ const { exitCode, files } = await run (
149
+ [ projectName , '--template' , 'typescript' ] ,
150
+ {
151
+ cwd : __dirname ,
152
+ }
151
153
) ;
154
+
155
+ // Assert for exit code
156
+ expect ( exitCode ) . toBe ( 0 ) ;
157
+
158
+ // Assert for the generated files
159
+ expectAllFiles ( files , [ ...generatedFiles , 'tsconfig.json' ] ) ;
152
160
} ) ;
153
161
} ) ;
0 commit comments