From 372196d100a8987137cd7f53ad31b142b87addb1 Mon Sep 17 00:00:00 2001 From: Gal Schlezinger Date: Tue, 12 Jul 2022 13:55:29 +0300 Subject: [PATCH] Add a Node.js dep test --- .../app/node_modules/my-pkg/hello/world.json | 1 + .../app/pages/api/edge.js | 7 +++++ .../index.test.ts | 26 ++++++++++++++++--- 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 test/e2e/edge-compiler-can-import-blob-assets/app/node_modules/my-pkg/hello/world.json diff --git a/test/e2e/edge-compiler-can-import-blob-assets/app/node_modules/my-pkg/hello/world.json b/test/e2e/edge-compiler-can-import-blob-assets/app/node_modules/my-pkg/hello/world.json new file mode 100644 index 0000000000000..394d6c8bc1df3 --- /dev/null +++ b/test/e2e/edge-compiler-can-import-blob-assets/app/node_modules/my-pkg/hello/world.json @@ -0,0 +1 @@ +{ "i am": "a node dependency" } diff --git a/test/e2e/edge-compiler-can-import-blob-assets/app/pages/api/edge.js b/test/e2e/edge-compiler-can-import-blob-assets/app/pages/api/edge.js index d4e8133f1df61..c667c2864a77b 100644 --- a/test/e2e/edge-compiler-can-import-blob-assets/app/pages/api/edge.js +++ b/test/e2e/edge-compiler-can-import-blob-assets/app/pages/api/edge.js @@ -27,6 +27,13 @@ const handlers = new Map([ return fetch(url) }, ], + [ + 'from-node-module', + async () => { + const url = new URL('my-pkg/hello/world.json', import.meta.url) + return fetch(url) + }, + ], ]) const defaultHandler = async () => diff --git a/test/e2e/edge-compiler-can-import-blob-assets/index.test.ts b/test/e2e/edge-compiler-can-import-blob-assets/index.test.ts index 60b15520f4d97..67f0f77062d85 100644 --- a/test/e2e/edge-compiler-can-import-blob-assets/index.test.ts +++ b/test/e2e/edge-compiler-can-import-blob-assets/index.test.ts @@ -4,6 +4,7 @@ import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils' import path from 'path' import { promises as fs } from 'fs' import { readJson } from 'fs-extra' +import type { MiddlewareManifest } from 'next/build/webpack/plugins/middleware-plugin' describe('Edge Compiler can import asset assets', () => { let next: NextInstance @@ -11,7 +12,6 @@ describe('Edge Compiler can import asset assets', () => { beforeAll(async () => { next = await createNext({ files: new FileRef(path.join(__dirname, './app')), - dependencies: {}, }) }) afterAll(() => next.destroy()) @@ -34,13 +34,29 @@ describe('Edge Compiler can import asset assets', () => { expect(buffer.equals(image)).toBeTrue() }) + it('allows to assets from node_modules', async () => { + const response = await fetchViaHTTP(next.url, '/api/edge', { + handler: 'from-node-module', + }) + const json = await response.json() + expect(json).toEqual({ + 'i am': 'a node dependency', + }) + }) + it('extracts all the assets from the bundle', async () => { const manifestPath = path.join( next.testDir, '.next/server/middleware-manifest.json' ) - const manifest = await readJson(manifestPath) - expect(manifest.functions['/api/edge'].assets).toMatchObject([ + const manifest: MiddlewareManifest = await readJson(manifestPath) + const orderedAssets = manifest.functions['/api/edge'].assets.sort( + (a, z) => { + return String(a.name).localeCompare(z.name) + } + ) + + expect(orderedAssets).toMatchObject([ { name: expect.stringMatching(/^text-file\.[0-9a-f]{16}\.txt$/), filePath: expect.stringMatching( @@ -51,6 +67,10 @@ describe('Edge Compiler can import asset assets', () => { name: expect.stringMatching(/^vercel\.[0-9a-f]{16}\.png$/), filePath: expect.stringMatching(/^server\/edge-chunks\/asset_vercel/), }, + { + name: expect.stringMatching(/^world\.[0-9a-f]{16}\.json/), + filePath: expect.stringMatching(/^server\/edge-chunks\/asset_world/), + }, ]) }) })