Fix filesystempublicroutes test for Turbopack (#61132)

## What?

`exportPathMap` didn't work when Turbopack was enabled because the
`serializeNextConfig` function mutates the original values, overriding
`exportPathMap`.

This PR changes the serialization to copy the object and mutate only the
copied object.

Also refactored the test that was checking `_next`, the better way to
test that is to have the page render something dynamically, which is
what is added in this PR.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-2225
This commit is contained in:
Tim Neutkens 2024-01-25 14:44:50 +01:00 committed by GitHub
parent fdd15869ac
commit 9cc7167e1b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 34 additions and 31 deletions

View file

@ -1056,7 +1056,8 @@ function bindingToApi(binding: any, _wasm: boolean) {
nextConfig: NextConfigComplete, nextConfig: NextConfigComplete,
projectPath: string projectPath: string
): Promise<string> { ): Promise<string> {
let nextConfigSerializable = nextConfig as any // Avoid mutating the existing `nextConfig` object.
let nextConfigSerializable = { ...(nextConfig as any) }
nextConfigSerializable.generateBuildId = nextConfigSerializable.generateBuildId =
await nextConfig.generateBuildId?.() await nextConfig.generateBuildId?.()
@ -1091,9 +1092,12 @@ function bindingToApi(binding: any, _wasm: boolean) {
: undefined : undefined
// loaderFile is an absolute path, we need it to be relative for turbopack. // loaderFile is an absolute path, we need it to be relative for turbopack.
if (nextConfig.images.loaderFile) { if (nextConfigSerializable.images.loaderFile) {
nextConfig.images.loaderFile = nextConfigSerializable.images = {
'./' + path.relative(projectPath, nextConfig.images.loaderFile) ...nextConfig.images,
loaderFile:
'./' + path.relative(projectPath, nextConfig.images.loaderFile),
}
} }
return JSON.stringify(nextConfigSerializable, null, 2) return JSON.stringify(nextConfigSerializable, null, 2)

View file

@ -1 +1,14 @@
export default () => <div>exportpathmap was here</div> import { useEffect } from 'react'
import { useState } from 'react'
export default function ExportPathMapRoute() {
const [mounted, setMounted] = useState(false)
useEffect(() => {
setMounted(true)
}, [])
return (
<div>
<h1>exportpathmap was here</h1>
{mounted ? <div id="page-was-loaded">Hello World</div> : null}
</div>
)
}

View file

@ -2,12 +2,8 @@
import { join } from 'path' import { join } from 'path'
import getPort from 'get-port' import getPort from 'get-port'
import { import { fetchViaHTTP, initNextServerScript, killApp } from 'next-test-utils'
fetchViaHTTP, import webdriver from 'next-webdriver'
initNextServerScript,
killApp,
getPageFileFromBuildManifest,
} from 'next-test-utils'
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
let appPort let appPort
@ -40,23 +36,14 @@ describe('FileSystemPublicRoutes', () => {
const res = await fetch('/exportpathmap-route') const res = await fetch('/exportpathmap-route')
expect(res.status).toBe(200) expect(res.status).toBe(200)
const body = await res.text() const body = await res.text()
expect(body).toMatch( expect(body).toMatch(/exportpathmap was here/)
process.env.TURBOPACK ? /turbopack/ : /exportpathmap was here/
)
}) })
it('should still handle /_next routes', async () => { it('should serve JavaScript files correctly', async () => {
await fetch('/exportpathmap-route') // make sure it's built const browser = await webdriver(context.appPort, '/exportpathmap-route')
const pageFile = getPageFileFromBuildManifest(
appDir, const text = await browser.waitForElementByCss('#page-was-loaded').text()
'/exportpathmap-route' expect(text).toBe('Hello World')
)
const res = await fetch(join('/_next', pageFile))
expect(res.status).toBe(200)
const body = await res.text()
expect(body).toMatch(
process.env.TURBOPACK ? /turbopack/ : /exportpathmap was here/
)
}) })
it('should route to public folder files', async () => { it('should route to public folder files', async () => {

View file

@ -10749,13 +10749,12 @@
}, },
"test/integration/filesystempublicroutes/test/index.test.js": { "test/integration/filesystempublicroutes/test/index.test.js": {
"passed": [ "passed": [
"FileSystemPublicRoutes should route to public folder files",
"FileSystemPublicRoutes should still handle /_next routes"
],
"failed": [
"FileSystemPublicRoutes should not route to the index page", "FileSystemPublicRoutes should not route to the index page",
"FileSystemPublicRoutes should route to exportPathMap defined routes in development" "FileSystemPublicRoutes should route to exportPathMap defined routes in development",
"FileSystemPublicRoutes should route to public folder files",
"FileSystemPublicRoutes should serve JavaScript files correctly"
], ],
"failed": [],
"pending": [], "pending": [],
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": false