forked from netlify/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect-server.test.js
More file actions
179 lines (155 loc) · 7.08 KB
/
Copy pathdetect-server.test.js
File metadata and controls
179 lines (155 loc) · 7.08 KB
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
const test = require('ava')
const path = require('path')
const getPort = require('get-port')
const { loadDetector, serverSettings, chooseDefaultArgs } = require('./detect-server')
const { createSiteBuilder } = require('../../tests/utils/siteBuilder')
test.before(async t => {
const builder = createSiteBuilder({ siteName: 'site-for-detecting-server' })
await builder.buildAsync()
t.context.cwd = process.cwd()
process.chdir(builder.directory)
t.context.builder = builder
t.context.sitePath = builder.directory
})
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 settings = await serverSettings({ framework: '#auto' }, {}, t.context.sitePath, () => {})
t.is(settings.framework, undefined)
})
test('serverSettings: "#static" as "framework"', async t => {
const settings = await serverSettings({ framework: '#static' }, {}, t.context.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 }, {}, t.context.sitePath, () => {})
}, /Could not acquire required "port"/)
})
test('serverSettings: "command" override npm', async t => {
const devConfig = { framework: '#custom', command: 'npm run dev', targetPort: 1234 }
const settings = await serverSettings(devConfig, {}, t.context.sitePath, () => {})
t.is(settings.framework, devConfig.framework)
t.is(settings.command, devConfig.command.split(' ')[0])
t.deepEqual(settings.args, devConfig.command.split(' ').slice(1))
})
test('serverSettings: "command" override yarn', async t => {
const devConfig = { framework: '#custom', command: 'yarn dev', targetPort: 1234 }
const settings = await serverSettings(devConfig, {}, t.context.sitePath, () => {})
t.is(settings.framework, devConfig.framework)
t.is(settings.command, devConfig.command.split(' ')[0])
t.deepEqual(settings.args, devConfig.command.split(' ').slice(1))
})
test('serverSettings: custom framework parameters', async t => {
const devConfig = { framework: '#custom', command: 'yarn dev', targetPort: 3000, publish: t.context.sitePath }
const settings = await serverSettings(devConfig, {}, t.context.sitePath, () => {})
t.is(settings.framework, '#custom')
t.is(settings.command, devConfig.command.split(' ')[0])
t.deepEqual(settings.args, devConfig.command.split(' ').slice(1))
t.is(settings.targetPort, devConfig.frameworkPort)
t.is(settings.dist, devConfig.publish)
})
test('serverSettings: set "framework" to "#custom" but no "command"', async t => {
const devConfig = { framework: '#custom', targetPort: 3000, publish: t.context.sitePath }
await t.throwsAsync(async () => {
await serverSettings(devConfig, {}, t.context.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: t.context.sitePath }
await t.throwsAsync(async () => {
await serverSettings(devConfig, {}, t.context.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: t.context.sitePath }
await t.throwsAsync(async () => {
await serverSettings(devConfig, {}, t.context.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(t.context.sitePath, 'functions') }
const settings = await serverSettings(devConfig, {}, t.context.sitePath, () => {})
t.is(settings.functions, devConfig.functions)
})
test('serverSettings: "dir" flag', async t => {
const devConfig = {
framework: '#auto',
publish: path.join(t.context.sitePath, 'build'),
functions: path.join(t.context.sitePath, 'functions'),
}
const flags = { dir: t.context.sitePath }
const settings = await serverSettings(devConfig, flags, t.context.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 and "command" as config param', async t => {
const devConfig = {
framework: '#auto',
command: 'npm start',
publish: path.join(t.context.sitePath, 'build'),
functions: path.join(t.context.sitePath, 'functions'),
}
const flags = { dir: t.context.sitePath }
const settings = await serverSettings(devConfig, flags, t.context.sitePath, () => {})
t.is(settings.command, undefined)
t.is(settings.noCmd, true)
t.is(settings.dist, flags.dir)
})
test('serverSettings: "dir" and "targetPort" flags', async t => {
const devConfig = { framework: '#auto', functions: path.join(t.context.sitePath, 'functions') }
const flags = { dir: t.context.sitePath, targetPort: 1234 }
await t.throwsAsync(async () => {
await serverSettings(devConfig, flags, t.context.sitePath, () => {})
}, /"targetPort" option cannot be used in conjunction with "dir" flag/)
})
test('serverSettings: "dir" and "command" flags', async t => {
const devConfig = { framework: '#auto', functions: path.join(t.context.sitePath, 'functions') }
const flags = { dir: t.context.sitePath, command: 'ding' }
await t.throwsAsync(async () => {
await serverSettings(devConfig, flags, t.context.sitePath, () => {})
}, /"command" option cannot be used in conjunction with "dir" flag/)
})
test('serverSettings: when no framework is detected', async t => {
const devConfig = {
framework: '#auto',
publish: path.join(t.context.sitePath, 'build'),
functions: path.join(t.context.sitePath, 'functions'),
}
const settings = await serverSettings(devConfig, {}, t.context.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, {}, t.context.sitePath, () => {})
t.is(settings.dist, t.context.sitePath)
t.is(settings.framework, undefined)
t.is(settings.cmd, undefined)
t.truthy(settings.port)
t.truthy(settings.frameworkPort)
t.is(settings.noCmd, true)
})
test('chooseDefaultArgs', t => {
const possibleArgsArrs = [['run', 'dev'], ['run develop']]
const args = chooseDefaultArgs(possibleArgsArrs)
t.deepEqual(args, possibleArgsArrs[0])
})
test.after(async t => {
process.chdir(t.context.cwd)
await t.context.builder.cleanupAsync()
})