From 4a5d24c7d0dc641eb011452bd0734df6ea44163e Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Thu, 7 Dec 2023 17:57:47 +0100 Subject: [PATCH] chore: prettier --- src/lib/functions/registry.ts | 4 +- .../commands/dev/edge-functions.test.ts | 193 +++++++++--------- 2 files changed, 103 insertions(+), 94 deletions(-) diff --git a/src/lib/functions/registry.ts b/src/lib/functions/registry.ts index ea5f2399a01..c1d2b96c136 100644 --- a/src/lib/functions/registry.ts +++ b/src/lib/functions/registry.ts @@ -469,7 +469,9 @@ export class FunctionsRegistry { * care of registering and unregistering functions as they come and go. */ async scan(relativeDirs: (string | undefined)[]) { - const directories = relativeDirs.filter((dir): dir is string => Boolean(dir)).map((dir) => (isAbsolute(dir) ? dir : join(this.projectRoot, dir))) + const directories = relativeDirs + .filter((dir): dir is string => Boolean(dir)) + .map((dir) => (isAbsolute(dir) ? dir : join(this.projectRoot, dir))) // check after filtering to filter out [undefined] for example if (directories.length === 0) { diff --git a/tests/integration/commands/dev/edge-functions.test.ts b/tests/integration/commands/dev/edge-functions.test.ts index 4b3882a15fe..7d9aeaba473 100644 --- a/tests/integration/commands/dev/edge-functions.test.ts +++ b/tests/integration/commands/dev/edge-functions.test.ts @@ -38,123 +38,130 @@ const setup = async ({ fixture }: FixtureTestContext) => { } const recreateEdgeFunctions = async ({ fixture }: FixtureTestContext) => { - await rename(join(fixture.directory, ".netlify",'_edge-functions'), join(fixture.directory, ".netlify",'edge-functions')) + await rename( + join(fixture.directory, '.netlify', '_edge-functions'), + join(fixture.directory, '.netlify', 'edge-functions'), + ) } describe.skipIf(isWindows)('edge functions', () => { - setupFixtureTests('dev-server-with-edge-functions', { devServer: true, mockApi: { routes }, setupAfterDev: recreateEdgeFunctions }, () => { - test('should run edge functions in correct order', async ({ devServer }) => { - const response = await got(`http://localhost:${devServer.port}/ordertest`, { - throwHttpErrors: false, - retry: { limit: 0 }, + setupFixtureTests( + 'dev-server-with-edge-functions', + { devServer: true, mockApi: { routes }, setupAfterDev: recreateEdgeFunctions }, + () => { + test('should run edge functions in correct order', async ({ devServer }) => { + const response = await got(`http://localhost:${devServer.port}/ordertest`, { + throwHttpErrors: false, + retry: { limit: 0 }, + }) + + expect(response.statusCode).toBe(200) + expect(response.body).toMatchSnapshot() }) - expect(response.statusCode).toBe(200) - expect(response.body).toMatchSnapshot() - }) + test('should provide context properties', async ({ devServer }) => { + const response = await got(`http://localhost:${devServer.port}/context`, { + throwHttpErrors: false, + retry: { limit: 0 }, + }) - test('should provide context properties', async ({ devServer }) => { - const response = await got(`http://localhost:${devServer.port}/context`, { - throwHttpErrors: false, - retry: { limit: 0 }, + const { deploy, geo, ip, params, requestId, server, site } = JSON.parse(response.body) + 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}` }) }) - const { deploy, geo, ip, params, requestId, server, site } = JSON.parse(response.body) - 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('should expose URL parameters', async ({ devServer }) => { + const response = await got(`http://localhost:${devServer.port}/categories/foo/products/bar`, { + throwHttpErrors: false, + retry: { limit: 0 }, + }) - 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 { params } = JSON.parse(response.body) + expect(params).toEqual({ + category: 'foo', + product: 'bar', + }) }) - const { params } = JSON.parse(response.body) - expect(params).toEqual({ - category: 'foo', - product: 'bar', - }) - }) + 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 }, + }) - 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 { params } = JSON.parse(response.body) + expect(params).toEqual({ + category: 'foo', + product: 'bar', + }) }) - const { params } = JSON.parse(response.body) - expect(params).toEqual({ - category: 'foo', - product: 'bar', - }) - }) + test('should respect config.methods field', async ({ devServer }) => { + const responseGet = await got(`http://localhost:${devServer.port}/products/really-bad-product`, { + method: 'GET', + throwHttpErrors: false, + retry: { limit: 0 }, + }) - test('should respect config.methods field', async ({ devServer }) => { - const responseGet = await got(`http://localhost:${devServer.port}/products/really-bad-product`, { - method: 'GET', - throwHttpErrors: false, - retry: { limit: 0 }, - }) + expect(responseGet.statusCode).toBe(404) - expect(responseGet.statusCode).toBe(404) + const responseDelete = await got(`http://localhost:${devServer.port}/products/really-bad-product`, { + method: 'DELETE', + throwHttpErrors: false, + retry: { limit: 0 }, + }) - const responseDelete = await got(`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(responseDelete.body).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 }, + }) - 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') - 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 }, + // 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:

') }) - expect(res2.body).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 - 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 - - expect(body.SITE_ID).toBe('foo') - expect(body.SITE_NAME).toBe('site-name') - expect(body.URL).toBe(`http://localhost:${devServer.port}`) - }) - }) + 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('should not remove other edge functions on change', async ({ devServer, fixture }) => {