Unverified Commit 7861621f authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

test: Refactor edge-runtime-dynamic-code test (#83390)

This test is about `eval`
But it also used a non-static middleware `matchers` config, which is going to break once we are more strict about this and throw a build error.
parent c2d3e654
export const useCases = {
eval: 'using-eval',
noEval: 'not-using-eval',
wasmCompile: 'using-webassembly-compile',
wasmInstanciate: 'using-webassembly-instantiate',
wasmBufferInstanciate: 'using-webassembly-instantiate-with-buffer',
}
export async function usingEval() { export async function usingEval() {
// eslint-disable-next-line no-eval // eslint-disable-next-line no-eval
return { value: eval('100') } return { value: eval('100') }
......
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { useCases, notUsingEval, usingEval } from './lib/utils' import { notUsingEval, usingEval } from './lib/utils'
import { import {
usingWebAssemblyCompile, usingWebAssemblyCompile,
usingWebAssemblyInstantiate, usingWebAssemblyInstantiate,
...@@ -7,31 +7,33 @@ import { ...@@ -7,31 +7,33 @@ import {
} from './lib/wasm' } from './lib/wasm'
export async function middleware(request) { export async function middleware(request) {
if (request.nextUrl.pathname === `/${useCases.eval}`) { if (request.nextUrl.pathname === '/using-eval') {
return new Response(null, { return new Response(null, {
headers: { data: JSON.stringify(await usingEval()) }, headers: { data: JSON.stringify(await usingEval()) },
}) })
} }
if (request.nextUrl.pathname === `/${useCases.noEval}`) { if (request.nextUrl.pathname === '/not-using-eval') {
return new Response(null, { return new Response(null, {
headers: { data: JSON.stringify(await notUsingEval()) }, headers: { data: JSON.stringify(await notUsingEval()) },
}) })
} }
if (request.nextUrl.pathname === `/${useCases.wasmCompile}`) { if (request.nextUrl.pathname === '/using-webassembly-compile') {
return new Response(null, { return new Response(null, {
headers: { data: JSON.stringify(await usingWebAssemblyCompile(9)) }, headers: { data: JSON.stringify(await usingWebAssemblyCompile(9)) },
}) })
} }
if (request.nextUrl.pathname === `/${useCases.wasmInstanciate}`) { if (request.nextUrl.pathname === '/using-webassembly-instantiate') {
return new Response(null, { return new Response(null, {
headers: { data: JSON.stringify(await usingWebAssemblyInstantiate(9)) }, headers: { data: JSON.stringify(await usingWebAssemblyInstantiate(9)) },
}) })
} }
if (request.nextUrl.pathname === `/${useCases.wasmBufferInstanciate}`) { if (
request.nextUrl.pathname === '/using-webassembly-instantiate-with-buffer'
) {
return new Response(null, { return new Response(null, {
headers: { headers: {
data: JSON.stringify(await usingWebAssemblyInstantiateWithBuffer(9)), data: JSON.stringify(await usingWebAssemblyInstantiateWithBuffer(9)),
...@@ -43,5 +45,11 @@ export async function middleware(request) { ...@@ -43,5 +45,11 @@ export async function middleware(request) {
} }
export const config = { export const config = {
matcher: Object.values(useCases).map((route) => `/${route}`), matcher: [
'/using-eval',
'/not-using-eval',
'/using-webassembly-compile',
'/using-webassembly-instantiate',
'/using-webassembly-instantiate-with-buffer',
],
} }
import { useCases, notUsingEval, usingEval } from '../../lib/utils' import { notUsingEval, usingEval } from '../../lib/utils'
import { import {
usingWebAssemblyCompile, usingWebAssemblyCompile,
usingWebAssemblyInstantiate, usingWebAssemblyInstantiate,
...@@ -8,19 +8,27 @@ import { ...@@ -8,19 +8,27 @@ import {
export default async function handler(request) { export default async function handler(request) {
const useCase = request.nextUrl.searchParams.get('case') const useCase = request.nextUrl.searchParams.get('case')
return Response.json( if (useCase === 'using-eval') {
useCase === useCases.eval return Response.json(await usingEval())
? await usingEval() }
: useCase === useCases.noEval
? await notUsingEval() if (useCase === 'not-using-eval') {
: useCase === useCases.wasmCompile return Response.json(await notUsingEval())
? await usingWebAssemblyCompile(9) }
: useCase === useCases.wasmInstanciate
? await usingWebAssemblyInstantiate(9) if (useCase === 'using-webassembly-compile') {
: useCase === useCases.wasmBufferInstanciate return Response.json(await usingWebAssemblyCompile(9))
? await usingWebAssemblyInstantiateWithBuffer(9) }
: { ok: true }
) if (useCase === 'using-webassembly-instantiate') {
return Response.json(await usingWebAssemblyInstantiate(9))
}
if (useCase === 'using-webassembly-instantiate-with-buffer') {
return Response.json(await usingWebAssemblyInstantiateWithBuffer(9))
}
return Response.json({ ok: true })
} }
export const config = { runtime: 'edge' } export const config = { runtime: 'edge' }
...@@ -42,7 +42,7 @@ describe('Page using eval in development mode', () => { ...@@ -42,7 +42,7 @@ describe('Page using eval in development mode', () => {
beforeEach(() => (output = '')) beforeEach(() => (output = ''))
afterAll(() => killApp(context.app)) afterAll(() => killApp(context.app))
it('does issue dynamic code evaluation warnings', async () => { it('does not issue dynamic code evaluation warnings', async () => {
const html = await renderViaHTTP(context.appPort, '/') const html = await renderViaHTTP(context.appPort, '/')
expect(html).toMatch(/>.*?100.*?and.*?100.*?<\//) expect(html).toMatch(/>.*?100.*?and.*?100.*?<\//)
await waitFor(500) await waitFor(500)
...@@ -108,10 +108,10 @@ describe.each([ ...@@ -108,10 +108,10 @@ describe.each([
isTurbopack isTurbopack
? '' + ? '' +
// TODO(veil): Turbopack duplicates project path // TODO(veil): Turbopack duplicates project path
'\n at usingEval (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/lib/utils.js:11:17)' + '\n at usingEval (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/lib/utils.js:3:17)' +
'\n at middleware (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/middleware.js:12:54)' + '\n at middleware (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/middleware.js:12:54)' +
'\n 9 | export async function usingEval() {' '\n 1 | export async function usingEval() {'
: '\n at usingEval (../../test/integration/edge-runtime-dynamic-code/lib/utils.js:11:19)' + : '\n at usingEval (../../test/integration/edge-runtime-dynamic-code/lib/utils.js:3:19)' +
'\n at middleware (../../test/integration/edge-runtime-dynamic-code/middleware.js:12:54)' + '\n at middleware (../../test/integration/edge-runtime-dynamic-code/middleware.js:12:54)' +
// Next.js internal frame. Feel free to adjust. // Next.js internal frame. Feel free to adjust.
// Not ignore-listed because we're not in an isolated app and Next.js is symlinked so it's not in node_modules // Not ignore-listed because we're not in an isolated app and Next.js is symlinked so it's not in node_modules
...@@ -122,12 +122,12 @@ describe.each([ ...@@ -122,12 +122,12 @@ describe.each([
isTurbopack isTurbopack
? '' + ? '' +
// TODO(veil): Turbopack duplicates project path // TODO(veil): Turbopack duplicates project path
'\n at usingEval (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/lib/utils.js:11:17)' + '\n at usingEval (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/lib/utils.js:3:17)' +
'\n at handler (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/pages/api/route.js:13:24)' + '\n at handler (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/pages/api/route.js:12:41)' +
'\n 9 | export async function usingEval() {' '\n 1 | export async function usingEval() {'
: '\n at usingEval (../../test/integration/edge-runtime-dynamic-code/lib/utils.js:11:19)' + : '\n at usingEval (../../test/integration/edge-runtime-dynamic-code/lib/utils.js:3:19)' +
'\n at handler (../../test/integration/edge-runtime-dynamic-code/pages/api/route.js:13:24)' + '\n at handler (../../test/integration/edge-runtime-dynamic-code/pages/api/route.js:12:41)' +
'\n 9 | export async function usingEval() {' '\n 1 | export async function usingEval() {'
) )
} }
...@@ -135,14 +135,14 @@ describe.each([ ...@@ -135,14 +135,14 @@ describe.each([
if (isTurbopack) { if (isTurbopack) {
expect(output).toContain( expect(output).toContain(
'' + '' +
"\n> 11 | return { value: eval('100') }" + "\n> 3 | return { value: eval('100') }" +
'\n | ^' '\n | ^'
) )
} else { } else {
expect(output).toContain( expect(output).toContain(
'' + '' +
"\n> 11 | return { value: eval('100') }" + "\n> 3 | return { value: eval('100') }" +
'\n | ^' '\n | ^'
) )
} }
}) })
...@@ -189,14 +189,12 @@ describe.each([ ...@@ -189,14 +189,12 @@ describe.each([
? '' + ? '' +
// TODO(veil): Turbopack duplicates project path // TODO(veil): Turbopack duplicates project path
'\n at usingWebAssemblyCompile (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/lib/wasm.js:22:18)' + '\n at usingWebAssemblyCompile (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/lib/wasm.js:22:18)' +
'\n at handler (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/pages/api/route.js:17:42)' + '\n at handler (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/pages/api/route.js:20:55)' +
'\n 20 |' '\n 20 |'
: '' + : '' +
'\n at usingWebAssemblyCompile (../../test/integration/edge-runtime-dynamic-code/lib/wasm.js:22:24)' + '\n at usingWebAssemblyCompile (../../test/integration/edge-runtime-dynamic-code/lib/wasm.js:22:24)' +
'\n at handler (../../test/integration/edge-runtime-dynamic-code/pages/api/route.js:17:42)' + '\n at handler (../../test/integration/edge-runtime-dynamic-code/pages/api/route.js:20:55)' +
// Next.js internal frame. Feel free to adjust. '\n 20 |'
// Not ignore-listed because we're not in an isolated app and Next.js is symlinked so it's not in node_modules
'\n at'
) )
// Turbopack produces incorrect mappings in the sourcemap. // Turbopack produces incorrect mappings in the sourcemap.
...@@ -230,11 +228,11 @@ describe.each([ ...@@ -230,11 +228,11 @@ describe.each([
isTurbopack isTurbopack
? '' + ? '' +
'\n at async usingWebAssemblyInstantiateWithBuffer (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/lib/wasm.js:28:24)' + '\n at async usingWebAssemblyInstantiateWithBuffer (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/lib/wasm.js:28:24)' +
'\n at async middleware (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/middleware.js:37:30)' + '\n at async middleware (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/middleware.js:39:30)' +
'\n 26 |\n' '\n 26 |\n'
: '' + : '' +
'\n at async usingWebAssemblyInstantiateWithBuffer (../../test/integration/edge-runtime-dynamic-code/lib/wasm.js:28:24)' + '\n at async usingWebAssemblyInstantiateWithBuffer (../../test/integration/edge-runtime-dynamic-code/lib/wasm.js:28:24)' +
'\n at async middleware (../../test/integration/edge-runtime-dynamic-code/middleware.js:37:30)' + '\n at async middleware (../../test/integration/edge-runtime-dynamic-code/middleware.js:39:30)' +
// Next.js internal frame. Feel free to adjust. // Next.js internal frame. Feel free to adjust.
// TODO(veil): https://linear.app/vercel/issue/NDX-464 // TODO(veil): https://linear.app/vercel/issue/NDX-464
'\n at ' '\n at '
...@@ -250,14 +248,12 @@ describe.each([ ...@@ -250,14 +248,12 @@ describe.each([
? '' + ? '' +
// TODO(veil): Turbopack duplicates project path // TODO(veil): Turbopack duplicates project path
'\n at async usingWebAssemblyInstantiateWithBuffer (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/lib/wasm.js:28:24)' + '\n at async usingWebAssemblyInstantiateWithBuffer (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/lib/wasm.js:28:24)' +
'\n at async handler (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/pages/api/route.js:21:17)' + '\n at async handler (../../test/integration/edge-runtime-dynamic-code/test/integration/edge-runtime-dynamic-code/pages/api/route.js:28:26)' +
'\n 26 |' '\n 26 |'
: '' + : '' +
'\n at async usingWebAssemblyInstantiateWithBuffer (../../test/integration/edge-runtime-dynamic-code/lib/wasm.js:28:24)' + '\n at async usingWebAssemblyInstantiateWithBuffer (../../test/integration/edge-runtime-dynamic-code/lib/wasm.js:28:24)' +
'\n at async handler (../../test/integration/edge-runtime-dynamic-code/pages/api/route.js:21:17)' + '\n at async handler (../../test/integration/edge-runtime-dynamic-code/pages/api/route.js:28:26)' +
// Next.js internal frame. Feel free to adjust. '\n 26 |'
// Not ignore-listed because we're not in an isolated app and Next.js is symlinked so it's not in node_modules
'\n at'
) )
// TODO(veil): Inconsistent cursor position // TODO(veil): Inconsistent cursor position
expect(stripAnsi(output)).toContain( expect(stripAnsi(output)).toContain(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment