Unverified Commit e2a5b095 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

test: merge edge ssr tests (#39924)

- Group edge ssr tests, merge `edge-vs.-non-edge-api-route-priority` and `react-18-streaming-ssr` into 1 e2e test suite
- Add rewrite case to edge ssr in switchable runtime
parent bd82f690
...@@ -1222,7 +1222,8 @@ jobs: ...@@ -1222,7 +1222,8 @@ jobs:
- run: TEST_WASM=true xvfb-run node run-tests.js test/integration/production/test/index.test.js - run: TEST_WASM=true xvfb-run node run-tests.js test/integration/production/test/index.test.js
if: ${{needs.build.outputs.docsChange == 'nope'}} if: ${{needs.build.outputs.docsChange == 'nope'}}
- run: TEST_WASM=true xvfb-run node run-tests.js test/production/react-18-streaming-ssr/index.test.ts # test wasm parsing for runtime in page config
- run: TEST_WASM=true xvfb-run node run-tests.js test/e2e/streaming-ssr/index.test.ts
if: ${{needs.build.outputs.docsChange == 'nope'}} if: ${{needs.build.outputs.docsChange == 'nope'}}
# Build binaries for publishing # Build binaries for publishing
......
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP } from 'next-test-utils'
describe('Edge vs. non-Edge API route priority', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
'pages/api/user/login.js': `
export default async function handler(_, res) {
res.send('from login.js')
}
`,
'pages/api/user/[id].js': `
export const config = {
runtime: 'experimental-edge',
}
export default async function handler() {
return new Response('from [id].js')
}`,
},
dependencies: {},
})
})
afterAll(() => next.destroy())
it('more specific route should match', async () => {
const res = await fetchViaHTTP(next.url, '/api/user/login')
expect(await res.text()).toBe('from login.js')
})
})
...@@ -14,71 +14,7 @@ const react18Deps = { ...@@ -14,71 +14,7 @@ const react18Deps = {
'react-dom': '^18.0.0', 'react-dom': '^18.0.0',
} }
describe('react 18 streaming SSR in minimal mode', () => { const isNextProd = !(global as any).isNextDev && !(global as any).isNextDeploy
let next: NextInstance
beforeAll(async () => {
process.env.NEXT_PRIVATE_MINIMAL_MODE = '1'
next = await createNext({
files: {
'pages/index.js': `
export default function Page() {
return <p>streaming</p>
}
export async function getServerSideProps() {
return { props: {} }
}
`,
},
nextConfig: {
experimental: {
runtime: 'nodejs',
},
webpack(config, { nextRuntime }) {
const path = require('path')
const fs = require('fs')
const runtimeFilePath = path.join(__dirname, 'runtimes.txt')
let runtimeContent = ''
try {
runtimeContent = fs.readFileSync(runtimeFilePath, 'utf8')
runtimeContent += '\n'
} catch (_) {}
runtimeContent += nextRuntime || 'client'
fs.writeFileSync(runtimeFilePath, runtimeContent)
return config
},
},
dependencies: react18Deps,
})
})
afterAll(() => {
delete process.env.NEXT_PRIVATE_MINIMAL_MODE
next.destroy()
})
it('should pass correct nextRuntime values', async () => {
const content = await next.readFile('runtimes.txt')
expect(content.split('\n').sort()).toEqual(['client', 'edge', 'nodejs'])
})
it('should generate html response by streaming correctly', async () => {
const html = await renderViaHTTP(next.url, '/')
expect(html).toContain('streaming')
})
it('should have generated a static 404 page', async () => {
expect(await next.readFile('.next/server/pages/404.html')).toBeTruthy()
const res = await fetchViaHTTP(next.url, '/non-existent')
expect(res.status).toBe(404)
expect(await res.text()).toContain('This page could not be found')
})
})
describe('react 18 streaming SSR with custom next configs', () => { describe('react 18 streaming SSR with custom next configs', () => {
let next: NextInstance let next: NextInstance
...@@ -95,6 +31,13 @@ describe('react 18 streaming SSR with custom next configs', () => { ...@@ -95,6 +31,13 @@ describe('react 18 streaming SSR with custom next configs', () => {
}) })
afterAll(() => next.destroy()) afterAll(() => next.destroy())
it('should match more specific route along with dynamic routes', async () => {
const res1 = await fetchViaHTTP(next.url, '/api/user/login')
const res2 = await fetchViaHTTP(next.url, '/api/user/any')
expect(await res1.text()).toBe('login')
expect(await res2.text()).toBe('[id]')
})
it('should render styled-jsx styles in streaming', async () => { it('should render styled-jsx styles in streaming', async () => {
const html = await renderViaHTTP(next.url, '/') const html = await renderViaHTTP(next.url, '/')
expect(html).toContain('color:blue') expect(html).toContain('color:blue')
...@@ -123,42 +66,115 @@ describe('react 18 streaming SSR with custom next configs', () => { ...@@ -123,42 +66,115 @@ describe('react 18 streaming SSR with custom next configs', () => {
}) })
}) })
describe('react 18 streaming SSR with custom server', () => { if (isNextProd) {
let next describe('react 18 streaming SSR with custom server', () => {
let server let next
let appPort let server
beforeAll(async () => { let appPort
next = await createNext({ beforeAll(async () => {
files: { next = await createNext({
pages: new FileRef(join(__dirname, 'custom-server/pages')), files: {
'server.js': new FileRef(join(__dirname, 'custom-server/server.js')), pages: new FileRef(join(__dirname, 'custom-server/pages')),
}, 'server.js': new FileRef(join(__dirname, 'custom-server/server.js')),
nextConfig: require(join(__dirname, 'custom-server/next.config.js')), },
dependencies: react18Deps, nextConfig: require(join(__dirname, 'custom-server/next.config.js')),
dependencies: react18Deps,
})
await next.stop()
const testServer = join(next.testDir, 'server.js')
appPort = await findPort()
server = await initNextServerScript(
testServer,
/Listening/,
{
...process.env,
PORT: appPort,
},
undefined,
{
cwd: next.testDir,
}
)
})
afterAll(async () => {
await next.destroy()
if (server) await killApp(server)
})
it('should render page correctly under custom server', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toContain('streaming')
}) })
await next.stop()
const testServer = join(next.testDir, 'server.js')
appPort = await findPort()
server = await initNextServerScript(
testServer,
/Listening/,
{
...process.env,
PORT: appPort,
},
undefined,
{
cwd: next.testDir,
}
)
})
afterAll(async () => {
await next.destroy()
if (server) await killApp(server)
}) })
it('should render page correctly under custom server', async () => {
const html = await renderViaHTTP(appPort, '/') describe('react 18 streaming SSR in minimal mode with node runtime', () => {
expect(html).toContain('streaming') let next: NextInstance
beforeAll(async () => {
if (isNextProd) {
process.env.NEXT_PRIVATE_MINIMAL_MODE = '1'
}
next = await createNext({
files: {
'pages/index.js': `
export default function Page() {
return <p>streaming</p>
}
export async function getServerSideProps() {
return { props: {} }
}`,
},
nextConfig: {
experimental: {
runtime: 'nodejs',
},
webpack(config, { nextRuntime }) {
const path = require('path')
const fs = require('fs')
const runtimeFilePath = path.join(__dirname, 'runtimes.txt')
let runtimeContent = ''
try {
runtimeContent = fs.readFileSync(runtimeFilePath, 'utf8')
runtimeContent += '\n'
} catch (_) {}
runtimeContent += nextRuntime || 'client'
fs.writeFileSync(runtimeFilePath, runtimeContent)
return config
},
},
dependencies: react18Deps,
})
})
afterAll(() => {
if (isNextProd) {
delete process.env.NEXT_PRIVATE_MINIMAL_MODE
}
next.destroy()
})
it('should pass correct nextRuntime values', async () => {
const content = await next.readFile('runtimes.txt')
expect(content.split('\n').sort()).toEqual(['client', 'edge', 'nodejs'])
})
it('should generate html response by streaming correctly', async () => {
const html = await renderViaHTTP(next.url, '/')
expect(html).toContain('streaming')
})
if (isNextProd) {
it('should have generated a static 404 page', async () => {
expect(await next.readFile('.next/server/pages/404.html')).toBeTruthy()
const res = await fetchViaHTTP(next.url, '/non-existent')
expect(res.status).toBe(404)
expect(await res.text()).toContain('This page could not be found')
})
}
}) })
}) }
module.exports = { module.exports = {
trailingSlash: true, trailingSlash: true,
experimental: {
runtime: 'experimental-edge',
},
} }
export default async function handler() {
return new Response('[id]')
}
export const config = {
runtime: 'experimental-edge',
}
export default async function handler(_, res) {
res.send('login')
}
...@@ -286,6 +286,10 @@ describe('Switchable runtime', () => { ...@@ -286,6 +286,10 @@ describe('Switchable runtime', () => {
isStatic: false, isStatic: false,
isEdge: true, isEdge: true,
}) })
await testRoute(context.appPort, '/rewrite/edge', {
isStatic: false,
isEdge: true,
})
}) })
// TODO: edge rsc in app dir // TODO: edge rsc in app dir
...@@ -305,6 +309,11 @@ describe('Switchable runtime', () => { ...@@ -305,6 +309,11 @@ describe('Switchable runtime', () => {
text = await response.text() text = await response.text()
expect(text).toMatch(/Returned by Edge API Route .+\/api\/edge/) expect(text).toMatch(/Returned by Edge API Route .+\/api\/edge/)
// Rewrite should also work
response = await fetchViaHTTP(context.appPort, 'rewrite/api/edge')
text = await response.text()
expect(text).toMatch(/Returned by Edge API Route .+\/api\/edge/)
if (!(global as any).isNextDeploy) { if (!(global as any).isNextDeploy) {
const manifest = await readJson( const manifest = await readJson(
join(context.appDir, '.next/server/middleware-manifest.json') join(context.appDir, '.next/server/middleware-manifest.json')
......
/** @type {import('next').NextConfig} */
module.exports = { module.exports = {
reactStrictMode: true, reactStrictMode: true,
experimental: { experimental: {
appDir: true, appDir: true,
serverComponents: true, serverComponents: true,
}, },
async rewrites() {
return {
afterFiles: [
{
source: '/rewrite/edge',
destination: '/edge',
},
{
source: '/rewrite/api/edge',
destination: '/api/edge',
},
],
}
},
} }
...@@ -239,7 +239,7 @@ describe('ServerSide Props Preview Mode', () => { ...@@ -239,7 +239,7 @@ describe('ServerSide Props Preview Mode', () => {
expect(cookies.length).toBe(2) expect(cookies.length).toBe(2)
}) })
/** @type import('next-webdriver').Chain */ /** @type {import('next-webdriver').Chain} */
let browser let browser
it('should start the client-side browser', async () => { it('should start the client-side browser', async () => {
browser = await webdriver( browser = await webdriver(
......
...@@ -300,7 +300,7 @@ describe('Prerender Preview Mode', () => { ...@@ -300,7 +300,7 @@ describe('Prerender Preview Mode', () => {
expect(cookies.length).toBe(2) expect(cookies.length).toBe(2)
}) })
/** @type import('next-webdriver').Chain */ /** @type {import('next-webdriver').Chain} */
let browser let browser
it('should start the client-side browser', async () => { it('should start the client-side browser', async () => {
browser = await webdriver( browser = await webdriver(
......
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