From d459930e353deba9758e3b185ad15d26b367bc7c Mon Sep 17 00:00:00 2001 From: Angel Mendez Date: Tue, 5 Dec 2023 15:33:25 -0600 Subject: [PATCH] refactor: replace got with node-fetch on edge-functions.test.ts related to #5695 --- .../commands/dev/edge-functions.test.ts | 100 +++++++----------- 1 file changed, 40 insertions(+), 60 deletions(-) diff --git a/tests/integration/commands/dev/edge-functions.test.ts b/tests/integration/commands/dev/edge-functions.test.ts index 39392d3deb2..ea0b338e21b 100644 --- a/tests/integration/commands/dev/edge-functions.test.ts +++ b/tests/integration/commands/dev/edge-functions.test.ts @@ -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 @@ -38,22 +38,17 @@ const setup = async ({ fixture }) => { describe.skipIf(isWindows)('edge functions', () => { setupFixtureTests('dev-server-with-edge-functions', { devServer: true, mockApi: { routes } }, () => { test('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('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' }) @@ -65,12 +60,9 @@ describe.skipIf(isWindows)('edge functions', () => { }) test('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', @@ -80,12 +72,9 @@ describe.skipIf(isWindows)('edge functions', () => { test('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', @@ -93,56 +82,49 @@ describe.skipIf(isWindows)('edge functions', () => { }) test('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('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('

An unhandled error in the function code triggered the following message:

') + 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( + '

An unhandled error in the function code triggered the following message:

', + ) }) test('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 + 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') @@ -174,14 +156,12 @@ describe.skipIf(isWindows)('edge functions', () => { { devServer: true, mockApi: { routes }, setup }, () => { test('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 } }, })