-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathframework-detection.test.js
408 lines (352 loc) · 15 KB
/
framework-detection.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import execa from 'execa'
import fetch from 'node-fetch'
import { describe, test } from 'vitest'
import { cliPath } from './utils/cli-path.js'
import { getExecaOptions, withDevServer } from './utils/dev-server.ts'
import { DOWN, answerWithValue, handleQuestions } from './utils/handle-questions.js'
import { withSiteBuilder } from './utils/site-builder.ts'
import { normalize } from './utils/snapshots.js'
const content = 'Hello World!'
describe.concurrent('frameworks/framework-detection', () => {
test('should default to process.cwd() and static server', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder
.withContentFile({
path: 'index.html',
content,
})
.build()
await withDevServer({ cwd: builder.directory }, async ({ output, url }) => {
const response = await fetch(url)
const responseContent = await response.text()
t.expect(responseContent).toEqual(content)
t.expect(normalize(output, { duration: true, filePath: true })).toMatchSnapshot()
})
})
})
test('should use static server when --dir flag is passed', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder
.withContentFile({
path: 'public/index.html',
content,
})
.build()
await withDevServer({ cwd: builder.directory, args: ['--dir', 'public'] }, async ({ output, url }) => {
const response = await fetch(url)
const responseContent = await response.text()
t.expect(responseContent).toEqual(content)
t.expect(normalize(output, { duration: true, filePath: true })).toMatchSnapshot()
})
})
})
test('should use static server when framework is set to #static', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder
.withContentFile({
path: 'index.html',
content,
})
.withNetlifyToml({ config: { dev: { framework: '#static' } } })
.build()
await withDevServer({ cwd: builder.directory }, async ({ output, url }) => {
const response = await fetch(url)
const responseContent = await response.text()
t.expect(responseContent).toEqual(content)
t.expect(normalize(output, { duration: true, filePath: true })).toMatchSnapshot()
})
})
})
test('should warn if using static server and `targetPort` is configured', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder
.withContentFile({
path: 'public/index.html',
content,
})
.build()
await withDevServer(
{ cwd: builder.directory, args: ['--dir', 'public', '--target-port', '3000'] },
async ({ output, url }) => {
const response = await fetch(url)
const responseContent = await response.text()
t.expect(responseContent).toEqual(content)
t.expect(normalize(output, { duration: true, filePath: true })).toMatchSnapshot()
},
)
})
})
test('should run `command` when both `command` and `targetPort` are configured', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder.withNetlifyToml({ config: { build: { publish: 'public' } } }).build()
// a failure is expected since we use `echo hello` instead of starting a server
const error = await withDevServer(
{ cwd: builder.directory, args: ['--command', 'echo hello', '--target-port', '3000'] },
() => {},
true,
).catch((error_) => error_)
t.expect(normalize(error.stdout, { duration: true, filePath: true })).toMatchSnapshot()
})
})
test('should force a specific framework when configured', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder.withNetlifyToml({ config: { dev: { framework: 'create-react-app' } } }).build()
// a failure is expected since this is not a true create-react-app project
const error = await withDevServer({ cwd: builder.directory }, () => {}, true).catch((error_) => error_)
t.expect(normalize(error.stdout, { duration: true, filePath: true })).toMatchSnapshot()
})
})
test('should throw when forcing a non supported framework', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder.withNetlifyToml({ config: { dev: { framework: 'to-infinity-and-beyond-js' } } }).build()
const error = await withDevServer({ cwd: builder.directory }, () => {}, true).catch((error_) => error_)
t.expect(normalize(error.stdout, { duration: true, filePath: true })).toMatchSnapshot()
})
})
test('should detect a known framework', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder
.withPackageJson({
packageJson: { dependencies: { 'react-scripts': '1.0.0' }, scripts: { start: 'react-scripts start' } },
})
.build()
// a failure is expected since this is not a true create-react-app project
const error = await withDevServer({ cwd: builder.directory }, () => {}, true).catch((error_) => error_)
t.expect(normalize(error.stdout, { duration: true, filePath: true })).toMatchSnapshot()
})
})
test('should throw if framework=#custom but command is missing', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder.withNetlifyToml({ config: { dev: { framework: '#custom' } } }).build()
const error = await withDevServer(
{ cwd: builder.directory, args: ['--target-port', '3000'] },
() => {},
true,
).catch((error_) => error_)
t.expect(normalize(error.stdout, { duration: true, filePath: true })).toMatchSnapshot()
})
})
test('should throw if framework=#custom but targetPort is missing', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder.withNetlifyToml({ config: { dev: { framework: '#custom' } } }).build()
const error = await withDevServer(
{ cwd: builder.directory, args: ['--command', 'echo hello'] },
() => {},
true,
).catch((error_) => error_)
t.expect(normalize(error.stdout, { duration: true, filePath: true })).toMatchSnapshot()
})
})
test('should start custom command if framework=#custom, command and targetPort are configured', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder.withNetlifyToml({ config: { dev: { framework: '#custom', publish: 'public' } } }).build()
const error = await withDevServer(
{ cwd: builder.directory, args: ['--command', 'echo hello', '--target-port', '3000'] },
() => {},
true,
).catch((error_) => error_)
t.expect(normalize(error.stdout, { duration: true, filePath: true })).toMatchSnapshot()
})
})
test(`should print specific error when command doesn't exist`, async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder.build()
const error = await withDevServer(
{
cwd: builder.directory,
args: [
'--command',
'oops-i-did-it-again forgot-to-use-a-valid-command',
'--target-port',
'3000',
'--framework',
'#custom',
],
},
() => {},
true,
).catch((error_) => error_)
t.expect(normalize(error.stdout, { duration: true, filePath: true })).toMatchSnapshot()
})
})
test('should prompt when multiple frameworks are detected', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder
.withPackageJson({
packageJson: {
dependencies: { 'react-scripts': '1.0.0', gatsby: '^3.0.0' },
scripts: { start: 'react-scripts start', develop: 'gatsby develop' },
},
})
.withContentFile({ path: 'gatsby-config.js', content: '' })
.build()
// a failure is expected since this is not a true framework project
const asyncErrorBlock = async () => {
const childProcess = execa(
cliPath,
['dev', '--offline'],
getExecaOptions({ cwd: builder.directory, env: { CI: 'false' } }),
)
handleQuestions(childProcess, [
{
question: 'Multiple possible dev commands found',
answer: answerWithValue(DOWN),
},
])
await childProcess
}
const error = await asyncErrorBlock().catch((error_) => error_)
t.expect(normalize(error.stdout, { duration: true, filePath: true })).toMatchSnapshot()
})
})
test('should fail in CI when multiple frameworks are detected', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder
.withPackageJson({
packageJson: {
dependencies: { 'react-scripts': '1.0.0', gatsby: '^3.0.0' },
scripts: { start: 'react-scripts start', develop: 'gatsby develop' },
},
})
.withContentFile({ path: 'gatsby-config.js', content: '' })
.build()
// a failure is expected since this is not a true framework project
const asyncErrorBlock = async () => {
const childProcess = execa(
cliPath,
['dev', '--offline'],
getExecaOptions({ cwd: builder.directory, env: { CI: true } }),
)
await childProcess
}
const error = await asyncErrorBlock().catch((error_) => error_)
t.expect(
normalize(error.stdout, { duration: true, filePath: true }).includes(
'Detected commands for: Gatsby, Create React App. Update your settings to specify which to use. Refer to https://ntl.fyi/dev-monorepo for more information.',
),
)
t.expect(error.exitCode).toBe(1)
})
})
test('should not run framework detection if command and targetPort are configured', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder.withContentFile({ path: 'config.toml', content: '' }).build()
// a failure is expected since the command exits early
const error = await withDevServer(
{ cwd: builder.directory, args: ['--command', 'echo hello', '--target-port', '3000'] },
() => {},
true,
).catch((error_) => error_)
t.expect(normalize(error.stdout, { duration: true, filePath: true })).toMatchSnapshot()
})
})
test('should filter frameworks with no dev command', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder
.withContentFile({
path: 'index.html',
content,
})
.withPackageJson({
packageJson: { dependencies: { gulp: '1.0.0' } },
})
.build()
await withDevServer({ cwd: builder.directory }, async ({ output, url }) => {
const response = await fetch(url)
const responseContent = await response.text()
t.expect(responseContent).toEqual(content)
t.expect(normalize(output, { duration: true, filePath: true })).toMatchSnapshot()
})
})
})
test('should pass framework-info env to framework sub process', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder
.withPackageJson({
packageJson: {
dependencies: { '@redwoodjs/core': '^2.0.0' },
scripts: { dev: 'node -p process.env.NODE_VERSION' },
},
})
.build()
// a failure is expected since this is not a true Gatsby project
const error = await withDevServer({ cwd: builder.directory }, () => {}, true).catch((error_) => error_)
t.expect(normalize(error.stdout, { duration: true, filePath: true })).toMatchSnapshot()
})
})
test('should start static service for frameworks without port, forced framework', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder.withNetlifyToml({ config: { dev: { framework: 'remix' } } }).build()
// a failure is expected since this is not a true remix project
const error = await withDevServer({ cwd: builder.directory }, () => {}, true).catch((error_) => error_)
t.expect(error.stdout.includes(`Failed running command: remix watch. Please verify 'remix' exists`)).toBe(true)
})
})
test('should start static service for frameworks without port, detected framework', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder
.withPackageJson({
packageJson: {
dependencies: { remix: '^1.0.0', '@remix-run/netlify': '^1.0.0' },
scripts: {},
},
})
.withContentFile({ path: 'remix.config.js', content: '' })
.build()
// a failure is expected since this is not a true remix project
const error = await withDevServer({ cwd: builder.directory }, () => {}, true).catch((error_) => error_)
t.expect(error.stdout.includes(`Failed running command: remix watch. Please verify 'remix' exists`)).toBe(true)
})
})
test('should run and serve a production build when using the `serve` command', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder
.withNetlifyToml({
config: {
build: { publish: 'public' },
context: {
dev: { environment: { CONTEXT_CHECK: 'DEV' } },
production: { environment: { CONTEXT_CHECK: 'PRODUCTION' } },
},
functions: { directory: 'functions' },
plugins: [{ package: './plugins/frameworker' }],
},
})
.withBuildPlugin({
name: 'frameworker',
plugin: {
onPreBuild: async ({ netlifyConfig }) => {
// eslint-disable-next-line no-undef, @typescript-eslint/no-require-imports
const { mkdir, writeFile } = require('fs/promises')
const generatedFunctionsDir = 'new_functions'
netlifyConfig.functions.directory = generatedFunctionsDir
netlifyConfig.redirects.push({
from: '/hello',
to: '/.netlify/functions/hello',
})
await mkdir(generatedFunctionsDir)
await writeFile(
`${generatedFunctionsDir}/hello.js`,
`const { CONTEXT_CHECK, NETLIFY_DEV } = process.env; exports.handler = async () => ({ statusCode: 200, body: JSON.stringify({ CONTEXT_CHECK, NETLIFY_DEV }) })`,
)
},
},
})
.build()
await withDevServer(
{ cwd: builder.directory, context: null, debug: true, serve: true },
async ({ output, url }) => {
const response = await fetch(`${url}/hello`)
const responseJson = await response.json()
t.expect(responseJson).toStrictEqual({ CONTEXT_CHECK: 'PRODUCTION' })
const normalizedText = normalize(output, { duration: true, filePath: true })
t.expect(
normalizedText.includes(
`Changes will not be hot-reloaded, so if you need to rebuild your site you must exit and run 'netlify serve' again`,
),
).toEqual(true)
},
)
})
})
})