-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-helpers.js
274 lines (222 loc) · 7.83 KB
/
test-helpers.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
const fetch = require(`node-fetch`)
const execa = require(`execa`)
const fs = require(`fs-extra`)
const path = require(`path`)
const FormData = require("form-data")
export function runTests(env, host) {
describe(env, () => {
describe(`routing`, () => {
test(`top-level API`, async () => {
const result = await fetch(`${host}/api/top-level`).then(res =>
res.text()
)
expect(result).toMatchSnapshot()
})
test(`secondary-level API`, async () => {
const result = await fetch(
`${host}/api/a-directory/function`
).then(res => res.text())
expect(result).toMatchSnapshot()
})
test(`secondary-level API with index.js`, async () => {
const result = await fetch(`${host}/api/a-directory`).then(res =>
res.text()
)
expect(result).toMatchSnapshot()
})
test(`secondary-level API`, async () => {
const result = await fetch(`${host}/api/dir/function`).then(res =>
res.text()
)
expect(result).toMatchSnapshot()
})
test(`routes with special characters`, async () => {
const routes = [
`${host}/api/I-Am-Capitalized`,
`${host}/api/some whitespace`,
`${host}/api/with-äöü-umlaut`,
`${host}/api/some-àè-french`,
encodeURI(`${host}/api/some-אודות`),
]
for (const route of routes) {
const result = await fetch(route).then(res => res.text())
expect(result).toMatchSnapshot()
}
})
test(`dynamic routes`, async () => {
const routes = [
`${host}/api/users/23/additional`,
`${host}/api/dir/super`,
]
for (const route of routes) {
const result = await fetch(route).then(res => res.json())
expect(result).toMatchSnapshot()
}
})
})
describe(`environment variables`, () => {
test(`can use inside functions`, async () => {
const result = await fetch(`${host}/api/env-variables`).then(res =>
res.text()
)
expect(result).toEqual(`word`)
})
})
describe(`typescript`, () => {
test(`typescript functions work`, async () => {
const result = await fetch(`${host}/api/i-am-typescript`).then(res =>
res.text()
)
expect(result).toMatchSnapshot()
})
})
describe(`function errors don't crash the server`, () => {
// This test mainly just shows that the server doesn't crash.
test(`normal`, async () => {
const result = await fetch(`${host}/api/error-send-function-twice`)
expect(result.status).toEqual(200)
})
})
describe(`response formats`, () => {
test(`returns json correctly`, async () => {
const res = await fetch(`${host}/api/i-am-json`)
const result = await res.json()
const { date, ...headers } = Object.fromEntries(res.headers)
expect(result).toMatchSnapshot()
expect(headers).toMatchSnapshot()
})
test(`returns text correctly`, async () => {
const res = await fetch(`${host}/api/i-am-typescript`)
const result = await res.text()
const { date, ...headers } = Object.fromEntries(res.headers)
expect(result).toMatchSnapshot()
expect(headers).toMatchSnapshot()
})
})
describe(`functions can send custom statuses`, () => {
test(`can return 200 status`, async () => {
const res = await fetch(`${host}/api/status`)
expect(res.status).toEqual(200)
})
test(`can return 404 status`, async () => {
const res = await fetch(`${host}/api/status?code=404`)
expect(res.status).toEqual(404)
})
test(`can return 500 status`, async () => {
const res = await fetch(`${host}/api/status?code=500`)
expect(res.status).toEqual(500)
})
})
describe(`functions can parse different ways of sending data`, () => {
test(`query string`, async () => {
const result = await fetch(
`${host}/api/parser?amIReal=true`
).then(res => res.json())
expect(result).toMatchSnapshot()
})
test(`form parameters`, async () => {
const { URLSearchParams } = require("url")
const params = new URLSearchParams()
params.append("a", `form parameters`)
const result = await fetch(`${host}/api/parser`, {
method: `POST`,
body: params,
}).then(res => res.json())
expect(result).toMatchSnapshot()
})
test(`form data`, async () => {
const FormData = require("form-data")
const form = new FormData()
form.append("a", `form-data`)
const result = await fetch(`${host}/api/parser`, {
method: `POST`,
body: form,
}).then(res => res.json())
expect(result).toMatchSnapshot()
})
test(`json body`, async () => {
const body = { a: `json` }
const result = await fetch(`${host}/api/parser`, {
method: `POST`,
body: JSON.stringify(body),
headers: { "Content-Type": "application/json" },
}).then(res => res.json())
expect(result).toMatchSnapshot()
})
// TODO enable when functions support uploading files.
// test(`file in multipart/form`, async () => {
// const { readFileSync } = require("fs")
// const file = readFileSync(path.join(__dirname, "./fixtures/test.txt"))
// const form = new FormData()
// form.append("file", file)
// const result = await fetch(`${host}/api/parser`, {
// method: `POST`,
// body: form,
// }).then(res => res.json())
// console.log({ result })
// expect(result).toMatchSnapshot()
// })
// test(`stream a file`, async () => {
// const { createReadStream } = require("fs")
// const stream = createReadStream(path.join(__dirname, "./fixtures/test.txt"))
// const res = await fetch(`${host}/api/parser`, {
// method: `POST`,
// body: stream,
// })
// console.log(res)
// expect(result).toMatchSnapshot()
// })
})
describe(`functions get parsed cookies`, () => {
test(`cookie`, async () => {
const result = await fetch(`${host}/api/cookie-me`, {
headers: { cookie: `foo=blue;` },
}).then(res => res.json())
expect(result).toMatchSnapshot()
})
})
describe(`functions can redirect`, () => {
test(`normal`, async () => {
const result = await fetch(`${host}/api/redirect-me`)
expect(result.url).toEqual(host + `/`)
})
})
describe(`functions can have custom middleware`, () => {
test(`normal`, async () => {
const result = await fetch(`${host}/api/cors`)
const headers = Object.fromEntries(result.headers)
expect(headers[`access-control-allow-origin`]).toEqual(`*`)
})
})
// TODO figure out why this gets into endless loops
// describe.only(`hot reloading`, () => {
// const fixturesDir = path.join(__dirname, `fixtures`)
// const apiDir = path.join(__dirname, `../src/api`)
// beforeAll(() => {
// try {
// fs.unlinkSync(path.join(apiDir, `function-a.js`))
// } catch (e) {
// // Ignore as this should mostly error with file not found.
// // We delete to be sure it's not there.
// }
// })
// afterAll(() => {
// fs.unlinkSync(path.join(apiDir, `function-a.js`))
// })
// test(`new function`, cb => {
// fs.copySync(
// path.join(fixturesDir, `function-a.js`),
// path.join(apiDir, `function-a.js`)
// )
// setTimeout(async () => {
// const result = await fetch(
// `${host}/api/function-a`
// ).then(res => res.text())
// console.log(result)
// expect(result).toMatchSnapshot()
// cb()
// }, 400)
// })
// })
})
}