Skip to content

Commit

Permalink
Merge branch 'main' into fix-ef-internal-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt committed Dec 14, 2023
2 parents 7d5ead8 + a5740db commit 96ba1d9
Showing 1 changed file with 40 additions and 60 deletions.
100 changes: 40 additions & 60 deletions tests/integration/commands/dev/edge-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import execa from 'execa'
import { describe, expect, expectTypeOf, test } from 'vitest'

import { FixtureTestContext, setupFixtureTests } from '../../utils/fixture.js'
import got from '../../utils/got.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
Expand Down Expand Up @@ -50,22 +50,17 @@ describe.skipIf(isWindows)('edge functions', () => {
{ devServer: true, mockApi: { routes }, setupAfterDev: recreateEdgeFunctions },
() => {
test<FixtureTestContext>('should run edge functions in correct order', async ({ devServer }) => {
const response = await got(`http://localhost:${devServer.port}/ordertest`, {
throwHttpErrors: false,
retry: { limit: 0 },
})
const response = await fetch(`http://localhost:${devServer.port}/ordertest`)
const body = await response.text()

expect(response.statusCode).toBe(200)
expect(response.body).toMatchSnapshot()
expect(response.status).toBe(200)
expect(body).toMatchSnapshot()
})

test<FixtureTestContext>('should provide context properties', async ({ devServer }) => {
const response = await got(`http://localhost:${devServer.port}/context`, {
throwHttpErrors: false,
retry: { limit: 0 },
})
const response = await fetch(`http://localhost:${devServer.port}/context`)

const { deploy, geo, ip, params, requestId, server, site } = JSON.parse(response.body)
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' })
Expand All @@ -77,12 +72,9 @@ describe.skipIf(isWindows)('edge functions', () => {
})

test<FixtureTestContext>('should expose URL parameters', async ({ devServer }) => {
const response = await got(`http://localhost:${devServer.port}/categories/foo/products/bar`, {
throwHttpErrors: false,
retry: { limit: 0 },
})
const response = await fetch(`http://localhost:${devServer.port}/categories/foo/products/bar`)

const { params } = JSON.parse(response.body)
const { params } = await response.json()
expect(params).toEqual({
category: 'foo',
product: 'bar',
Expand All @@ -92,69 +84,59 @@ describe.skipIf(isWindows)('edge functions', () => {
test<FixtureTestContext>('should expose URL parameters to edge functions with `cache: "manual"`', async ({
devServer,
}) => {
const response = await got(`http://localhost:${devServer.port}/categories-after-cache/foo/products/bar`, {
throwHttpErrors: false,
retry: { limit: 0 },
})
const response = await fetch(`http://localhost:${devServer.port}/categories-after-cache/foo/products/bar`)

const { params } = JSON.parse(response.body)
const { params } = await response.json()
expect(params).toEqual({
category: 'foo',
product: 'bar',
})
})

test<FixtureTestContext>('should respect config.methods field', async ({ devServer }) => {
const responseGet = await got(`http://localhost:${devServer.port}/products/really-bad-product`, {
const responseGet = await fetch(`http://localhost:${devServer.port}/products/really-bad-product`, {
method: 'GET',
throwHttpErrors: false,
retry: { limit: 0 },
})

expect(responseGet.statusCode).toBe(404)
expect(responseGet.status).toBe(404)

const responseDelete = await got(`http://localhost:${devServer.port}/products/really-bad-product`, {
const responseDelete = await fetch(`http://localhost:${devServer.port}/products/really-bad-product`, {
method: 'DELETE',
throwHttpErrors: false,
retry: { limit: 0 },
})

expect(responseDelete.body).toEqual('Deleted item successfully: really-bad-product')
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,
}) => {
// Request #1: Plain text
const res1 = await got(`http://localhost:${devServer.port}/uncaught-exception`, {
method: 'GET',
throwHttpErrors: false,
retry: { limit: 0 },
})

expect(res1.statusCode).toBe(500)
expect(res1.body).toContain('ReferenceError: thisWillThrow is not defined')

// Request #2: HTML
const res2 = await got(`http://localhost:${devServer.port}/uncaught-exception`, {
method: 'GET',
headers: {
Accept: 'text/html',
},
throwHttpErrors: false,
retry: { limit: 0 },
})

expect(res2.body).toContain('<p>An unhandled error in the function code triggered the following message:</p>')
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 got(`http://localhost:${devServer.port}/echo-env`, {
throwHttpErrors: false,
retry: { limit: 0 },
}).json()) as Record<string, string>
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')
Expand Down Expand Up @@ -187,14 +169,12 @@ describe.skipIf(isWindows)('edge functions', () => {
{ devServer: true, mockApi: { routes }, setup },
() => {
test<FixtureTestContext>('should run an edge function that uses the Blobs npm module', async ({ devServer }) => {
const res = await got(`http://localhost:${devServer.port}/blobs`, {
const res = await fetch(`http://localhost:${devServer.port}/blobs`, {
method: 'GET',
throwHttpErrors: false,
retry: { limit: 0 },
})

expect(res.statusCode).toBe(200)
expect(JSON.parse(res.body)).toEqual({
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
data: 'hello world',
metadata: { name: 'Netlify', features: { blobs: true, functions: true } },
})
Expand Down

0 comments on commit 96ba1d9

Please sign in to comment.