Unverified Commit 53201b17 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

[after] duplicate test pages for edge instead of next.patch (#69477)

`unstable_after` needs to be tested in both nodejs and edge runtimes.
Previously, we set the runtime via `next.patch()`, but this proved
error-prone (e.g. because `sandbox()` call would override this patch,
and not actually run anything in edge...)

This PR makes this more static by splitting the test pages for
`unstable_after` into `/nodejs` and `/edge`, each with a corresponding
`export const runtime = "..."` in its root layout. The pages in `/edge`
mirror the structure of `/nodejs` and reexport components from it (which
makes the tests easier to update than simply duplicating everything).
note that "use client" directives and route segment configs cannot be
reexported and need to be duplicated.
parent ae1efa41
// 'use client' 'use client'
import { unstable_after as after } from 'next/server' import { unstable_after as after } from 'next/server'
import { cliLog } from '../../utils/log' import { cliLog } from '../../utils/log'
......
import { unstable_after as after } from 'next/server' import { unstable_after as after } from 'next/server'
import { cliLog } from '../../utils/log' import { cliLog } from '../../utils/log'
// (patched in tests) export const dynamic = 'error'
// export const dynamic = 'REPLACE_ME'
export default function Index() { export default function Index() {
after(async () => { after(async () => {
cliLog({ source: '[page] /static' }) cliLog({ source: '[page] /invalid-in-dynamic-error' })
}) })
return <div>Page with after()</div> return <div>Page with after()</div>
} }
import { unstable_after as after } from 'next/server'
import { cliLog } from '../../utils/log'
export const dynamic = 'force-static'
export default function Index() {
after(async () => {
cliLog({ source: '[page] /invalid-in-dynamic-force-static' })
})
return <div>Page with after()</div>
}
export default function AppLayout({ children }) {
return (
<html>
<head>
<title>after</title>
</head>
<body>{children}</body>
</html>
)
}
/* eslint-env jest */
import { nextTestSetup } from 'e2e-utils'
import * as Log from './utils/log'
import {
assertHasRedbox,
getRedboxDescription,
getRedboxSource,
} from '../../../lib/next-test-utils'
describe('unstable_after() - invalid usages', () => {
const { next } = nextTestSetup({
files: __dirname,
})
let currentCliOutputIndex = 0
beforeEach(() => {
currentCliOutputIndex = next.cliOutput.length
})
const getLogs = () => {
if (next.cliOutput.length < currentCliOutputIndex) {
// cliOutput shrank since we started the test, so something (like a `sandbox`) reset the logs
currentCliOutputIndex = 0
}
return Log.readCliLogs(next.cliOutput.slice(currentCliOutputIndex))
}
it.each(['error', 'force-static'])(
`errors at compile time with dynamic = "%s"`,
async (dynamicValue) => {
const pathname = '/invalid-in-dynamic-' + dynamicValue
const session = await next.browser(pathname)
await assertHasRedbox(session)
expect(await getRedboxDescription(session)).toContain(
`Route ${pathname} with \`dynamic = "${dynamicValue}"\` couldn't be rendered statically because it used \`unstable_after\``
)
expect(getLogs()).toHaveLength(0)
}
)
it('errors at compile time when used in a client module', async () => {
const session = await next.browser('/invalid-in-client')
await assertHasRedbox(session)
expect(await getRedboxSource(session)).toMatch(
/You're importing a component that needs "?unstable_after"?\. That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component\./
)
expect(getLogs()).toHaveLength(0)
})
})
/** @type {import('next').NextConfig} */
module.exports = {
experimental: {
after: true,
},
}
export function cliLog(/** @type {string | Record<string, any>} */ data) {
console.log('<test-log>' + JSON.stringify(data) + '</test-log>')
}
export function readCliLogs(/** @type {string} */ output) {
return output
.split('\n')
.map((line) => {
const match = line.match(/^<test-log>(?<value>.+?)<\/test-log>$/)
if (!match) {
return null
}
return JSON.parse(match.groups.value)
})
.filter(Boolean)
}
export { default } from '../../../nodejs/[id]/dynamic/page'
export { default } from '../../nodejs/[id]/layout'
export { default } from '../../../nodejs/[id]/setting-cookies/page'
export { default } from '../../../nodejs/[id]/with-action/page'
export {
default,
generateMetadata,
} from '../../../nodejs/[id]/with-metadata/page'
export { default } from '../../nodejs/delay/page'
export const dynamic = 'force-dynamic'
export { default } from '../../../nodejs/interrupted/calls-not-found/page'
import { createPage } from '../../../nodejs/interrupted/calls-redirect/page'
// NOTE: this page is forked from /nodejs
export default createPage('/edge')
export { default } from '../../../nodejs/interrupted/redirect-target/page'
export { default } from '../../../nodejs/interrupted/throws-error/page'
export { default } from '../nodejs/layout'
export const runtime = 'edge'
export { default } from '../../../nodejs/middleware/redirect/page'
export { default } from '../../nodejs/nested-after/page'
export { default } from '../../nodejs/provided-request-context/page'
export { GET } from '../../nodejs/route/route'
export const runtime = 'edge'
export const dynamic = 'force-dynamic'
import { redirect } from 'next/navigation'
import { unstable_after as after } from 'next/server'
import { cliLog } from '../../../utils/log'
export default function Page() {
after(() => {
cliLog({
source: '[page] /interrupted/calls-redirect',
})
})
redirect('/interrupted/redirect-target')
}
import { unstable_after as after } from 'next/server' import { unstable_after as after } from 'next/server'
import { cache } from 'react' import { cache } from 'react'
import { cliLog } from '../../../utils/log' import { cliLog } from '../../../../utils/log'
import { headers } from 'next/headers' import { headers } from 'next/headers'
const thing = cache(() => Symbol('cache me please')) const thing = cache(() => Symbol('cache me please'))
......
import { unstable_after as after } from 'next/server' import { unstable_after as after } from 'next/server'
import { cliLog } from '../../utils/log' import { cliLog } from '../../../utils/log'
export default function Layout({ children }) { export default function Layout({ children }) {
after(async () => { after(async () => {
......
import { unstable_after as after } from 'next/server' import { unstable_after as after } from 'next/server'
import { cache } from 'react' import { cache } from 'react'
import { cliLog } from '../../../utils/log' import { cliLog } from '../../../../utils/log'
import { headers } from 'next/headers' import { headers } from 'next/headers'
const thing = cache(() => Symbol('cache me please')) const thing = cache(() => Symbol('cache me please'))
......
import { unstable_after as after } from 'next/server' import { unstable_after as after } from 'next/server'
import { cliLog } from '../../../utils/log' import { cliLog } from '../../../../utils/log'
export function generateMetadata({ params }) { export function generateMetadata({ params }) {
after(() => { after(() => {
......
import { Suspense } from 'react' import { Suspense } from 'react'
import { unstable_after as after } from 'next/server' import { unstable_after as after } from 'next/server'
import { cliLog } from '../../utils/log' import { cliLog } from '../../../utils/log'
export const dynamic = 'force-dynamic' export const dynamic = 'force-dynamic'
......
import { notFound } from 'next/navigation' import { notFound } from 'next/navigation'
import { unstable_after as after } from 'next/server' import { unstable_after as after } from 'next/server'
import { cliLog } from '../../../utils/log' import { cliLog } from '../../../../utils/log'
export default function Page() { export default function Page() {
after(() => { after(() => {
......
import { unstable_after as after } from 'next/server'
import { redirect } from 'next/navigation'
import { cliLog } from '../../../../utils/log'
// NOTE: this page is forked in /edge
export function createPage(pathPrefix) {
return function Page() {
after(() => {
cliLog({
source: '[page] /interrupted/calls-redirect',
})
})
redirect(pathPrefix + '/interrupted/redirect-target')
}
}
export default createPage('/nodejs')
import { unstable_after as after } from 'next/server' import { unstable_after as after } from 'next/server'
import { cliLog } from '../../../utils/log' import { cliLog } from '../../../../utils/log'
export default function Page() { export default function Page() {
after(() => { after(() => {
......
import { unstable_after as after } from 'next/server' import { unstable_after as after } from 'next/server'
import { cliLog } from '../../../utils/log' import { cliLog } from '../../../../utils/log'
export default function Page() { export default function Page() {
after(() => { after(() => {
......
// (patched in tests) export const runtime = 'nodejs'
// export const runtime = 'REPLACE_ME'
export default function AppLayout({ children }) { export default function AppLayout({ children }) {
return ( return (
......
import { unstable_after as after } from 'next/server' import { unstable_after as after } from 'next/server'
import { cache } from 'react' import { cache } from 'react'
import { cliLog } from '../../utils/log' import { cliLog } from '../../../utils/log'
import { headers } from 'next/headers' import { headers } from 'next/headers'
const thing = cache(() => Symbol('cache me please')) const thing = cache(() => Symbol('cache me please'))
......
import { unstable_after as after } from 'next/server' import { unstable_after as after } from 'next/server'
import { cliLog } from '../../utils/log' import { cliLog } from '../../../utils/log'
export default function Page() { export default function Page() {
after(() => { after(() => {
......
import { unstable_after as after } from 'next/server' import { unstable_after as after } from 'next/server'
import { cliLog } from '../../utils/log' import { cliLog } from '../../../utils/log'
// (patched in tests)
// export const runtime = 'REPLACE_ME'
export const runtime = 'nodejs'
export const dynamic = 'force-dynamic' export const dynamic = 'force-dynamic'
export async function GET() { export async function GET() {
......
...@@ -6,7 +6,12 @@ export function middleware( ...@@ -6,7 +6,12 @@ export function middleware(
/** @type {import ('next/server').NextRequest} */ request /** @type {import ('next/server').NextRequest} */ request
) { ) {
const url = new URL(request.url) const url = new URL(request.url)
if (url.pathname.startsWith('/middleware/redirect-source')) {
const match = url.pathname.match(
/^(?<prefix>\/[^/]+?)\/middleware\/redirect-source/
)
if (match) {
const prefix = match.groups.prefix
const requestId = url.searchParams.get('requestId') const requestId = url.searchParams.get('requestId')
after(() => { after(() => {
cliLog({ cliLog({
...@@ -15,10 +20,12 @@ export function middleware( ...@@ -15,10 +20,12 @@ export function middleware(
cookies: { testCookie: cookies().get('testCookie')?.value }, cookies: { testCookie: cookies().get('testCookie')?.value },
}) })
}) })
return NextResponse.redirect(new URL('/middleware/redirect', request.url)) return NextResponse.redirect(
new URL(prefix + '/middleware/redirect', request.url)
)
} }
} }
export const config = { export const config = {
matcher: '/middleware/:path*', matcher: ['/:prefix/middleware/:path*'],
} }
/* eslint-env jest */ /* eslint-env jest */
import { nextTestSetup, isNextDev } from 'e2e-utils' import { nextTestSetup, isNextDev } from 'e2e-utils'
import { assertHasRedbox, getRedboxSource, retry } from 'next-test-utils' import { assertHasRedbox, getRedboxSource, retry } from 'next-test-utils'
import * as fs from 'fs'
import * as path from 'path'
import * as os from 'os'
import * as Log from './utils/log' import * as Log from './utils/log'
// using unstable_after is a compile-time error in build mode. // using unstable_after is a compile-time error in build mode.
const _describe = isNextDev ? describe : describe.skip const _describe = isNextDev ? describe : describe.skip
_describe('unstable_after() - pages', () => { _describe('unstable_after() - pages', () => {
const logFileDir = fs.mkdtempSync(path.join(os.tmpdir(), 'logs-'))
const logFile = path.join(logFileDir, 'logs.jsonl')
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
env: {
PERSISTENT_LOG_FILE: logFile,
},
}) })
let currentCliOutputIndex = 0 let currentCliOutputIndex = 0
......
...@@ -33,7 +33,7 @@ export function waitForHydration(browser: BrowserInterface): Promise<void> { ...@@ -33,7 +33,7 @@ export function waitForHydration(browser: BrowserInterface): Promise<void> {
export async function sandbox( export async function sandbox(
next: NextInstance, next: NextInstance,
initialFiles?: Map<string, string>, initialFiles?: Map<string, string | ((contents: string) => string)>,
initialUrl: string = '/', initialUrl: string = '/',
webDriverOptions: any = undefined webDriverOptions: any = undefined
) { ) {
......
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