Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed May 27, 2024
1 parent c3ab20c commit 7b6b937
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ export default async function getBaseWebpackConfig(
loggedIgnoredCompilerOptions = true
}

const shouldIncludeExternalDirs = config.experimental.externalDir // || !!config.transpilePackages
const shouldIncludeExternalDirs = config.experimental.externalDir
const codeCondition = {
test: { or: [/\.(tsx|ts|js|cjs|mjs|jsx)$/, /__barrel_optimize__/] },
...(shouldIncludeExternalDirs
Expand Down
10 changes: 7 additions & 3 deletions test/e2e/module-layer/middleware.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import 'server-only'
import React from 'react'
import * as React from 'react'
import { NextResponse } from 'next/server'
// import './lib/mixed-lib'

export function middleware(request) {
if (React.useState) {
// To avoid webpack ESM exports checking warning
const ReactObject = Object(React)
if (ReactObject.useState) {
throw new Error('React.useState should not be defined in server layer')
}

if (request.nextUrl.pathname === '/react-version') {
return new Response(React.version)
return Response.json({
React: Object.keys(ReactObject),
})
}

return NextResponse.next()
Expand Down
45 changes: 36 additions & 9 deletions test/e2e/module-layer/module-layer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ describe('module layer', () => {
'/app/route',
'/app/route-edge',
// pages/api
'/api/hello',
'/api/hello-edge',
'/api/default',
'/api/default-edge',
'/api/server-only',
'/api/server-only-edge',
'/api/mixed',
'/api/react-edge',
]

for (const route of routes) {
Expand All @@ -35,9 +36,33 @@ describe('module layer', () => {
})
}

it('should render installed react version for middleware', async () => {
const text = await next.fetch('/react-version').then((res) => res.text())
expect(text).toContain('19.0.0-rc')
it('should render installed react-server condition for middleware', async () => {
const json = await next.fetch('/react-version').then((res) => res.json())
expect(json.React).toContain('version') // basic react-server export
expect(json.React).not.toContain('useEffect') // no client api export
})

// This is for backward compatibility, don't change react usage in existing pages/api
it('should contain client react exports for pages api', async () => {
async function verifyReactExports(route, isEdge) {
const json = await next.fetch(route).then((res) => res.json())
// contain all react-server and default condition exports
expect(json.React).toContain('version')
expect(json.React).toContain('useEffect')

// contain react-dom-server default condition exports
expect(json.ReactDomServer).toContain('version')
expect(json.ReactDomServer).toContain('renderToString')
expect(json.ReactDomServer).toContain('renderToStaticMarkup')
expect(json.ReactDomServer).toContain(
isEdge ? 'renderToReadableStream' : 'renderToPipeableStream'
)
}

await verifyReactExports('/api/default', false)
await verifyReactExports('/api/default-edge', true)
await verifyReactExports('/api/server-only', false)
await verifyReactExports('/api/server-only-edge', true)
})

if (isNextStart) {
Expand All @@ -50,7 +75,8 @@ describe('module layer', () => {
)
expect(functionsManifest.functions).toContainKeys([
'/app/route-edge',
'/api/hello-edge',
'/api/default-edge',
'/api/server-only-edge',
'/app/client-edge',
'/app/server-edge',
])
Expand All @@ -62,9 +88,10 @@ describe('module layer', () => {
)
expect(middlewareManifest.middleware).toBeTruthy()
expect(pagesManifest).toContainKeys([
'/api/hello-edge',
'/api/default-edge',
'/pages-ssr',
'/api/hello',
'/api/default',
'/api/server-only',
])
})
}
Expand Down
11 changes: 11 additions & 0 deletions test/e2e/module-layer/pages/api/default-edge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as ReactDomServer from 'react-dom/server'
import * as React from 'react'

export default async (_req) => {
return Response.json({
React: Object.keys(Object(React)),
ReactDomServer: Object.keys(Object(ReactDomServer)),
})
}

export const runtime = 'edge'
9 changes: 9 additions & 0 deletions test/e2e/module-layer/pages/api/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as ReactDomServer from 'react-dom/server'
import * as React from 'react'

export default async (_req, res) => {
return res.json({
React: Object.keys(Object(React)),
ReactDomServer: Object.keys(Object(ReactDomServer)),
})
}
7 changes: 0 additions & 7 deletions test/e2e/module-layer/pages/api/hello-edge.js

This file was deleted.

5 changes: 0 additions & 5 deletions test/e2e/module-layer/pages/api/hello.js

This file was deleted.

23 changes: 0 additions & 23 deletions test/e2e/module-layer/pages/api/react-edge.js

This file was deleted.

12 changes: 12 additions & 0 deletions test/e2e/module-layer/pages/api/server-only-edge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'server-only'
import * as ReactDomServer from 'react-dom/server'
import * as React from 'react'

export default async (_req) => {
return Response.json({
React: Object.keys(Object(React)),
ReactDomServer: Object.keys(Object(ReactDomServer)),
})
}

export const runtime = 'edge'
10 changes: 10 additions & 0 deletions test/e2e/module-layer/pages/api/server-only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'server-only'
import * as ReactDomServer from 'react-dom/server'
import * as React from 'react'

export default async (_req, res) => {
return res.json({
React: Object.keys(Object(React)),
ReactDomServer: Object.keys(Object(ReactDomServer)),
})
}

0 comments on commit 7b6b937

Please sign in to comment.