-
Notifications
You must be signed in to change notification settings - Fork 340
/
edge-functions.test.ts
171 lines (141 loc) · 5.74 KB
/
edge-functions.test.ts
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
import process from 'process'
import execa from 'execa'
import { describe, expect, expectTypeOf, test } from 'vitest'
import { FixtureTestContext, setupFixtureTests } from '../../utils/fixture.js'
import fetch from 'node-fetch'
import { pause } from '../../utils/pause.js'
// Skipping tests on Windows because of an issue with the Deno CLI throwing IO
// errors when running in the CI.
const isWindows = process.platform === 'win32'
const siteInfo = {
account_slug: 'test-account',
id: 'foo',
name: 'site-name',
feature_flags: {
edge_functions_npm_support: true,
},
functions_config: { timeout: 1 },
}
const routes = [
{ path: 'sites/foo', response: siteInfo },
{ path: 'sites/foo/service-instances', response: [] },
{
path: 'accounts',
response: [{ slug: siteInfo.account_slug }],
},
]
const setup = async ({ fixture }) => {
await execa('npm', ['install'], { cwd: fixture.directory })
}
describe.skipIf(isWindows)('edge functions', () => {
setupFixtureTests('dev-server-with-edge-functions', { devServer: true, mockApi: { routes } }, () => {
test.skip<FixtureTestContext>('should run edge functions in correct order', async ({ devServer }) => {
const response = await fetch(`http://localhost:${devServer.port}/ordertest`)
const body = await response.text()
expect(response.status).toBe(200)
expect(body).toMatchSnapshot()
})
test<FixtureTestContext>('should provide context properties', async ({ devServer }) => {
const response = await fetch(`http://localhost:${devServer.port}/context`)
const { deploy, geo, ip, params, requestId, server, site } = await response.json()
expect(geo.city).toEqual('Mock City')
expect(geo.country.code).toEqual('DE')
expect(deploy).toEqual({ id: '0' })
expectTypeOf(ip).toBeString()
expect(params).toEqual({})
expectTypeOf(requestId).toBeString()
expect(server).toEqual({ region: 'local' })
expect(site).toEqual({ id: 'foo', name: 'site-name', url: `http://localhost:${devServer.port}` })
})
test<FixtureTestContext>('should expose URL parameters', async ({ devServer }) => {
const response = await fetch(`http://localhost:${devServer.port}/categories/foo/products/bar`)
const { params } = await response.json()
expect(params).toEqual({
category: 'foo',
product: 'bar',
})
})
test<FixtureTestContext>('should expose URL parameters to edge functions with `cache: "manual"`', async ({
devServer,
}) => {
const response = await fetch(`http://localhost:${devServer.port}/categories-after-cache/foo/products/bar`)
const { params } = await response.json()
expect(params).toEqual({
category: 'foo',
product: 'bar',
})
})
test<FixtureTestContext>('should respect config.methods field', async ({ devServer }) => {
const responseGet = await fetch(`http://localhost:${devServer.port}/products/really-bad-product`, {
method: 'GET',
})
expect(responseGet.status).toBe(404)
const responseDelete = await fetch(`http://localhost:${devServer.port}/products/really-bad-product`, {
method: 'DELETE',
})
expect(await responseDelete.text()).toEqual('Deleted item successfully: really-bad-product')
})
test<FixtureTestContext>('should show an error page when an edge function has an uncaught exception', async ({
devServer,
}) => {
const [plainTextResponse, htmlResponse] = await Promise.all([
fetch(`http://localhost:${devServer.port}/uncaught-exception`, {
method: 'GET',
}),
fetch(`http://localhost:${devServer.port}/uncaught-exception`, {
method: 'GET',
headers: {
Accept: 'text/html',
},
}),
])
expect(plainTextResponse.status).toBe(500)
expect(await plainTextResponse.text()).toContain('ReferenceError: thisWillThrow is not defined')
expect(await htmlResponse.text()).toContain(
'<p>An unhandled error in the function code triggered the following message:</p>',
)
})
test<FixtureTestContext>('should set the `URL`, `SITE_ID`, and `SITE_NAME` environment variables', async ({
devServer,
}) => {
const body = (await fetch(`http://localhost:${devServer.port}/echo-env`).then((res) => res.json())) as Record<
string,
string
>
expect(body.SITE_ID).toBe('foo')
expect(body.SITE_NAME).toBe('site-name')
expect(body.URL).toBe(`http://localhost:${devServer.port}`)
})
})
setupFixtureTests('dev-server-with-edge-functions', { devServer: true, mockApi: { routes } }, () => {
test<FixtureTestContext>('should not remove other edge functions on change', async ({ devServer, fixture }) => {
// we need to wait till file watchers are loaded
await pause(500)
await fixture.builder
.withEdgeFunction({
name: 'new',
handler: async () => new Response('hello'),
config: { path: ['/new'] },
})
.build()
await devServer.waitForLogMatching('Loaded edge function new')
expect(devServer.output).not.toContain('Removed edge function')
})
})
setupFixtureTests(
'dev-server-with-edge-functions-and-npm-modules',
{ devServer: true, mockApi: { routes }, setup },
() => {
test<FixtureTestContext>('should run an edge function that uses the Blobs npm module', async ({ devServer }) => {
const res = await fetch(`http://localhost:${devServer.port}/blobs`, {
method: 'GET',
})
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
data: 'hello world',
metadata: { name: 'Netlify', features: { blobs: true, functions: true } },
})
})
},
)
})