Skip to content

Commit

Permalink
refactor: replace got with node-fetch on edge-functions.test.ts (#6248)
Browse files Browse the repository at this point in the history
related to #5695

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
hereje and kodiakhq[bot] authored Dec 14, 2023
1 parent 8834fbc commit a5740db
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 @@ -4,7 +4,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 @@ -38,22 +38,17 @@ const setup = async ({ fixture }) => {
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 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 @@ -65,12 +60,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 @@ -80,69 +72,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 @@ -174,14 +156,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

1 comment on commit a5740db

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,399
  • Package size: 405 MB
  • Number of ts-expect-error directives: 1,292

Please sign in to comment.