6
6
* JavaScript (default), TypeScript, and appDir.
7
7
*/
8
8
9
+ import path from 'path'
10
+ import fs from 'fs-extra'
9
11
import {
10
12
createNextApp ,
11
13
projectFilesShouldExist ,
@@ -16,13 +18,53 @@ import {
16
18
} from './lib/utils'
17
19
18
20
import { useTempDir } from '../../../test/lib/use-temp-dir'
21
+ import { fetchViaHTTP , findPort , killApp , launchApp } from 'next-test-utils'
22
+ import resolveFrom from 'resolve-from'
23
+ import { getPkgPaths } from '../../../test/lib/create-next-install'
24
+
25
+ const startsWithoutError = async (
26
+ appDir : string ,
27
+ modes = [ 'default' , 'turbo' ]
28
+ ) => {
29
+ for ( const mode of modes ) {
30
+ appDir = await fs . realpath ( appDir )
31
+ const appPort = await findPort ( )
32
+ const app = await launchApp ( appDir , appPort , {
33
+ turbo : mode === 'turbo' ,
34
+ cwd : appDir ,
35
+ nextBin : resolveFrom ( appDir , 'next/dist/bin/next' ) ,
36
+ } )
37
+
38
+ try {
39
+ const res = await fetchViaHTTP ( appPort , '/' )
40
+ expect ( res . status ) . toBe ( 200 )
41
+ expect ( await res . text ( ) ) . toContain ( 'Get started by editing' )
42
+
43
+ const apiRes = await fetchViaHTTP ( appPort , '/api/hello' )
44
+ expect ( apiRes . status ) . toBe ( 200 )
45
+ expect ( await apiRes . json ( ) ) . toEqual ( { name : 'John Doe' } )
46
+ } finally {
47
+ await killApp ( app )
48
+ }
49
+ }
50
+ }
51
+ let testVersion
19
52
20
53
describe ( 'create-next-app templates' , ( ) => {
21
54
if ( ! process . env . NEXT_TEST_CNA ) {
22
55
it ( 'should skip when env is not set' , ( ) => { } )
23
56
return
24
57
}
25
58
59
+ beforeAll ( async ( ) => {
60
+ testVersion = (
61
+ await getPkgPaths ( {
62
+ repoDir : path . join ( __dirname , '../../../' ) ,
63
+ nextSwcVersion : '' ,
64
+ } )
65
+ ) . get ( 'next' )
66
+ } )
67
+
26
68
it ( 'should prompt user to choose if --ts or --js is not provided' , async ( ) => {
27
69
useTempDir ( async ( cwd ) => {
28
70
const projectName = 'choose-ts-js'
@@ -36,9 +78,12 @@ describe('create-next-app templates', () => {
36
78
'--eslint' ,
37
79
'--no-src-dir' ,
38
80
'--no-experimental-app' ,
39
- `--import-alias=" @/*" ` ,
81
+ `--import-alias=@/*` ,
40
82
] ,
41
- { cwd }
83
+ {
84
+ cwd,
85
+ } ,
86
+ testVersion
42
87
)
43
88
/**
44
89
* Wait for the prompt to display.
@@ -47,8 +92,8 @@ describe('create-next-app templates', () => {
47
92
/**
48
93
* Bind the exit listener.
49
94
*/
50
- await new Promise < void > ( ( resolve ) => {
51
- childProcess . on ( 'exit' , ( exitCode ) => {
95
+ await new Promise < void > ( ( resolve , reject ) => {
96
+ childProcess . on ( 'exit' , async ( exitCode ) => {
52
97
expect ( exitCode ) . toBe ( 0 )
53
98
/**
54
99
* Verify it correctly emitted a TS project by looking for tsconfig.
@@ -78,16 +123,19 @@ describe('create-next-app templates', () => {
78
123
'--eslint' ,
79
124
'--no-src-dir' ,
80
125
'--no-experimental-app' ,
81
- `--import-alias=" @/*" ` ,
126
+ `--import-alias=@/*` ,
82
127
] ,
83
128
{
84
129
cwd,
85
- }
130
+ } ,
131
+ testVersion
86
132
)
87
133
const exitCode = await spawnExitPromise ( childProcess )
88
134
89
135
expect ( exitCode ) . toBe ( 0 )
90
136
shouldBeTypescriptProject ( { cwd, projectName, template : 'default' } )
137
+
138
+ await startsWithoutError ( path . join ( cwd , projectName ) )
91
139
} )
92
140
} )
93
141
@@ -101,11 +149,12 @@ describe('create-next-app templates', () => {
101
149
'--eslint' ,
102
150
'--src-dir' ,
103
151
'--no-experimental-app' ,
104
- `--import-alias=" @/*" ` ,
152
+ `--import-alias=@/*` ,
105
153
] ,
106
154
{
107
155
cwd,
108
- }
156
+ } ,
157
+ testVersion
109
158
)
110
159
const exitCode = await spawnExitPromise ( childProcess )
111
160
@@ -116,24 +165,30 @@ describe('create-next-app templates', () => {
116
165
template : 'default' ,
117
166
srcDir : true ,
118
167
} )
168
+ await startsWithoutError ( path . join ( cwd , projectName ) )
119
169
} )
120
170
} )
121
171
122
172
it ( 'should create TS projects with --ts, --typescript with CI=1' , async ( ) => {
123
173
await useTempDir ( async ( cwd ) => {
124
174
const projectName = 'typescript-test'
125
- const childProcess = createNextApp ( [ projectName , '--ts' , '--eslint' ] , {
126
- cwd,
127
- env : {
128
- ...process . env ,
129
- CI : '1' ,
130
- GITHUB_ACTIONS : '1' ,
175
+ const childProcess = createNextApp (
176
+ [ projectName , '--ts' , '--eslint' ] ,
177
+ {
178
+ cwd,
179
+ env : {
180
+ ...process . env ,
181
+ CI : '1' ,
182
+ GITHUB_ACTIONS : '1' ,
183
+ } ,
131
184
} ,
132
- } )
185
+ testVersion
186
+ )
133
187
const exitCode = await spawnExitPromise ( childProcess )
134
188
135
189
expect ( exitCode ) . toBe ( 0 )
136
190
shouldBeTypescriptProject ( { cwd, projectName, template : 'default' } )
191
+ await startsWithoutError ( path . join ( cwd , projectName ) )
137
192
} )
138
193
} )
139
194
@@ -147,16 +202,20 @@ describe('create-next-app templates', () => {
147
202
'--eslint' ,
148
203
'--no-src-dir' ,
149
204
'--no-experimental-app' ,
150
- `--import-alias=" @/*" ` ,
205
+ `--import-alias=@/*` ,
151
206
] ,
152
207
{
153
208
cwd,
154
- }
209
+ } ,
210
+ testVersion
155
211
)
156
212
const exitCode = await spawnExitPromise ( childProcess )
157
213
158
214
expect ( exitCode ) . toBe ( 0 )
159
215
shouldBeJavascriptProject ( { cwd, projectName, template : 'default' } )
216
+ // TODO: enable turbo mode as well once jsconfig paths support
217
+ // is landed
218
+ await startsWithoutError ( path . join ( cwd , projectName ) , [ 'default' ] )
160
219
} )
161
220
} )
162
221
@@ -170,11 +229,12 @@ describe('create-next-app templates', () => {
170
229
'--eslint' ,
171
230
'--src-dir' ,
172
231
'--no-experimental-app' ,
173
- `--import-alias=" @/*" ` ,
232
+ `--import-alias=@/*` ,
174
233
] ,
175
234
{
176
235
cwd,
177
- }
236
+ } ,
237
+ testVersion
178
238
)
179
239
const exitCode = await spawnExitPromise ( childProcess )
180
240
@@ -185,6 +245,9 @@ describe('create-next-app templates', () => {
185
245
template : 'default' ,
186
246
srcDir : true ,
187
247
} )
248
+ // TODO: enable turbo mode as well once jsconfig paths support
249
+ // is landed
250
+ await startsWithoutError ( path . join ( cwd , projectName ) , [ 'default' ] )
188
251
} )
189
252
} )
190
253
} )
@@ -195,6 +258,16 @@ describe('create-next-app --experimental-app-dir', () => {
195
258
return
196
259
}
197
260
261
+ beforeAll ( async ( ) => {
262
+ if ( testVersion ) return
263
+ testVersion = (
264
+ await getPkgPaths ( {
265
+ repoDir : path . join ( __dirname , '../../../' ) ,
266
+ nextSwcVersion : '' ,
267
+ } )
268
+ ) . get ( 'next' )
269
+ } )
270
+
198
271
it ( 'should create TS appDir projects with --ts' , async ( ) => {
199
272
await useTempDir ( async ( cwd ) => {
200
273
const projectName = 'appdir-test'
@@ -205,17 +278,18 @@ describe('create-next-app --experimental-app-dir', () => {
205
278
'--experimental-app' ,
206
279
'--eslint' ,
207
280
'--no-src-dir' ,
208
- '--no-experimental-app' ,
209
- `--import-alias="@/*"` ,
281
+ `--import-alias=@/*` ,
210
282
] ,
211
283
{
212
284
cwd,
213
- }
285
+ } ,
286
+ testVersion
214
287
)
215
288
216
289
const exitCode = await spawnExitPromise ( childProcess )
217
290
expect ( exitCode ) . toBe ( 0 )
218
291
shouldBeTemplateProject ( { cwd, projectName, template : 'app' , mode : 'ts' } )
292
+ await startsWithoutError ( path . join ( cwd , projectName ) )
219
293
} )
220
294
} )
221
295
@@ -229,17 +303,20 @@ describe('create-next-app --experimental-app-dir', () => {
229
303
'--experimental-app' ,
230
304
'--eslint' ,
231
305
'--no-src-dir' ,
232
- '--no-experimental-app' ,
233
- `--import-alias="@/*"` ,
306
+ `--import-alias=@/*` ,
234
307
] ,
235
308
{
236
309
cwd,
237
- }
310
+ } ,
311
+ testVersion
238
312
)
239
313
240
314
const exitCode = await spawnExitPromise ( childProcess )
241
315
expect ( exitCode ) . toBe ( 0 )
242
316
shouldBeTemplateProject ( { cwd, projectName, template : 'app' , mode : 'js' } )
317
+ // TODO: enable turbo mode as well once jsconfig paths support
318
+ // is landed
319
+ await startsWithoutError ( path . join ( cwd , projectName ) , [ 'default' ] )
243
320
} )
244
321
} )
245
322
@@ -253,12 +330,13 @@ describe('create-next-app --experimental-app-dir', () => {
253
330
'--experimental-app' ,
254
331
'--eslint' ,
255
332
'--src-dir' ,
256
- '--import-alias=" @/*" ' ,
333
+ '--import-alias=@/*' ,
257
334
] ,
258
335
{
259
336
cwd,
260
337
stdio : 'inherit' ,
261
- }
338
+ } ,
339
+ testVersion
262
340
)
263
341
264
342
const exitCode = await spawnExitPromise ( childProcess )
@@ -270,6 +348,9 @@ describe('create-next-app --experimental-app-dir', () => {
270
348
mode : 'js' ,
271
349
srcDir : true ,
272
350
} )
351
+ // TODO: enable turbo mode as well once jsconfig paths support
352
+ // is landed
353
+ await startsWithoutError ( path . join ( cwd , projectName ) , [ 'default' ] )
273
354
} )
274
355
} )
275
356
} )
0 commit comments