-
Notifications
You must be signed in to change notification settings - Fork 342
/
detect-server.test.js
143 lines (124 loc) · 5.83 KB
/
detect-server.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
const test = require('ava')
const path = require('path')
const getPort = require('get-port')
const { loadDetector, serverSettings, chooseDefaultArgs } = require('./detect-server')
const sitePath = path.join(__dirname, '..', '..', 'tests', 'dummy-site')
process.chdir(sitePath)
test('loadDetector: valid', t => {
const d = loadDetector('create-react-app.js')
t.is(typeof d, 'function')
})
test('loadDetector: invalid', t => {
t.throws(() => {
loadDetector('cry.js')
}, /Failed to load detector/)
})
test('serverSettings: minimal config', async t => {
const env = { ...process.env }
const settings = await serverSettings({ framework: '#auto' }, {}, sitePath, () => {})
t.deepEqual(settings.env, env)
t.is(settings.framework, undefined)
})
test('serverSettings: "#static" as "framework"', async t => {
const settings = await serverSettings({ framework: '#static' }, {}, sitePath, () => {})
t.is(settings.framework, undefined)
})
test('serverSettings: throw if "port" not available', async t => {
const port = await getPort({ port: 1 })
await t.throwsAsync(async () => {
await serverSettings({ framework: '#auto', port }, {}, sitePath, () => {})
}, /Could not acquire required "port"/)
})
test('serverSettings: "command" override npm', async t => {
const env = { ...process.env }
const devConfig = { framework: '#custom', command: 'npm run dev', targetPort: 1234 }
const settings = await serverSettings(devConfig, {}, sitePath, () => {})
t.is(settings.framework, devConfig.framework)
t.is(settings.command, devConfig.command.split(' ')[0])
t.deepEqual(settings.args, devConfig.command.split(' ').slice(1))
t.deepEqual(settings.env, env)
})
test('serverSettings: "command" override yarn', async t => {
const env = { ...process.env }
const devConfig = { framework: '#custom', command: 'yarn dev', targetPort: 1234 }
const settings = await serverSettings(devConfig, {}, sitePath, () => {})
t.is(settings.framework, devConfig.framework)
t.is(settings.command, devConfig.command.split(' ')[0])
t.deepEqual(settings.args, devConfig.command.split(' ').slice(1))
t.deepEqual(settings.env, env)
})
test('serverSettings: custom framework parameters', async t => {
const env = { ...process.env }
const devConfig = { framework: '#custom', command: 'yarn dev', targetPort: 3000, publish: sitePath }
const settings = await serverSettings(devConfig, {}, sitePath, () => {})
t.is(settings.framework, '#custom')
t.is(settings.command, devConfig.command.split(' ')[0])
t.deepEqual(settings.args, devConfig.command.split(' ').slice(1))
t.deepEqual(settings.env, env)
t.is(settings.targetPort, devConfig.proxyPort)
t.is(settings.dist, devConfig.publish)
})
test('serverSettings: set "framework" to "#custom" but no "command"', async t => {
const devConfig = { framework: '#custom', targetPort: 3000, publish: sitePath }
await t.throwsAsync(async () => {
await serverSettings(devConfig, {}, sitePath, () => {})
}, /"command" and "targetPort" properties are required when "framework" is set to "#custom"/)
})
test('serverSettings: set "framework" to "#custom" but no "targetPort"', async t => {
const devConfig = { framework: '#custom', command: "npm run dev", publish: sitePath }
await t.throwsAsync(async () => {
await serverSettings(devConfig, {}, sitePath, () => {})
}, /"command" and "targetPort" properties are required when "framework" is set to "#custom"/)
})
test('serverSettings: set "framework" to "#custom" but no "targetPort" or "command"', async t => {
const devConfig = { framework: '#custom', publish: sitePath }
await t.throwsAsync(async () => {
await serverSettings(devConfig, {}, sitePath, () => {})
}, /"command" and "targetPort" properties are required when "framework" is set to "#custom"/)
})
test('serverSettings: "functions" config', async t => {
const devConfig = { framework: '#auto', functions: path.join(sitePath, 'functions') }
const settings = await serverSettings(devConfig, {}, sitePath, () => {})
t.is(settings.functions, devConfig.functions)
})
test('serverSettings: "dir" flag', async t => {
const devConfig = { framework: '#auto', publish: path.join(sitePath, 'build'), functions: path.join(sitePath, 'functions') }
const flags = { dir: sitePath }
const settings = await serverSettings(devConfig, flags, sitePath, () => {})
t.is(settings.functions, devConfig.functions)
t.is(settings.dist, flags.dir)
t.is(settings.framework, undefined)
t.is(settings.cmd, undefined)
t.is(settings.noCmd, true)
})
test('serverSettings: "dir" flag with "targetPort"', async t => {
const devConfig = { framework: '#auto', targetPort: 1234, functions: path.join(sitePath, 'functions') }
const flags = { dir: sitePath }
await t.throwsAsync(async () => {
await serverSettings(devConfig, flags, sitePath, () => {})
}, /"command" or "targetPort" options cannot be used in conjunction with "dir" flag/)
})
test('serverSettings: when no framework is detected', async t => {
const devConfig = { framework: '#auto', publish: path.join(sitePath, 'build'), functions: path.join(sitePath, 'functions') }
const settings = await serverSettings(devConfig, {}, sitePath, () => {})
t.is(settings.functions, devConfig.functions)
t.is(settings.dist, devConfig.publish)
t.is(settings.framework, undefined)
t.is(settings.cmd, undefined)
t.is(settings.noCmd, true)
})
test('serverSettings: no config', async t => {
const devConfig = { framework: '#auto' }
const settings = await serverSettings(devConfig, {}, sitePath, () => {})
t.is(settings.dist, sitePath)
t.is(settings.framework, undefined)
t.is(settings.cmd, undefined)
t.truthy(settings.port)
t.truthy(settings.proxyPort)
t.is(settings.noCmd, true)
})
test('chooseDefaultArgs', t => {
const possibleArgsArrs = [['run', 'dev'], ['run develop']]
const args = chooseDefaultArgs(possibleArgsArrs)
t.deepEqual(args, possibleArgsArrs[0])
})