Unverified Commit 47a52c0d authored by Sam Poder's avatar Sam Poder Committed by GitHub

[turbopack / next.js] Add an end-to-end test for new root detection (#96544)

Follow-up PR to https://github.com/vercel/next.js/pull/96159, adding an
end to end test that specifically tests the new stuff in that PR!
parent 8e878d48
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>hello world</p>
}
import { dirname, join } from 'path'
import { FileRef, nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import { packageJson, packageLock } from './test-utils'
// <isolation root>/ a lockfile that belongs to an unrelated project
// └── repo/ a Git repository, and the expected root
// ├── .git/
// ├── package-lock.json
// └── app/ the Next.js app
describe('root-detection - git repository boundary', () => {
const { next, skipped, isTurbopack } = nextTestSetup({
files: {
app: new FileRef(join(__dirname, 'app')),
// a `.git` directory makes the parent directory a repository root.
'../.git/HEAD': 'ref: refs/heads/main\n',
'../package.json': packageJson('repository'),
'../package-lock.json': packageLock('repository'),
'../../package.json': packageJson('above-repository'),
'../../package-lock.json': packageLock('above-repository'),
},
// So that the files written above don't leave the isolated testDir
subDir: 'repo/app',
skipDeployment: true,
// The workspace file would stop the search before the Git boundary does,
// so the test wouldn't be exercising the boundary.
deleteWorkspaceFile: true,
})
if (skipped) {
return
}
it('should not select a root above the repository', async () => {
const repoDir = dirname(next.testDir)
const aboveRepoDir = dirname(repoDir)
const config = isTurbopack ? 'turbopack.root' : 'outputFileTracingRoot'
await retry(async () => {
expect(next.cliOutput).toContain(
`Next.js ignored package-lock.json in ${aboveRepoDir} because it is outside the current Git repository (${repoDir}).`
)
expect(next.cliOutput).toContain(
`To use this directory, set \`${config}\` in your Next.js config.`
)
// the lockfile inside the repository is still used, so the root is the
// repository rather than the app directory.
expect(next.cliOutput).toContain(
`We detected multiple lockfiles and selected the directory of ${join(
repoDir,
'package-lock.json'
)} as the root directory.`
)
})
})
it('should serve the app', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('hello world')
})
})
import { dirname, join } from 'path'
import { FileRef, nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import {
gitRepositoryBoundaryWarning,
packageJson,
packageLock,
} from './test-utils'
// <isolation root>/ a lockfile that belongs to the checkout
// ├── repo/.git/worktrees/worktree/ the Git directory of the worktree
// └── worktree/ a linked worktree, and the expected root
// ├── .git a file pointing at the Git directory
// ├── package-lock.json
// └── app/ the Next.js app
describe('root-detection - git worktree boundary', () => {
const { next, skipped } = nextTestSetup({
files: {
app: new FileRef(join(__dirname, 'app')),
'../.git': 'gitdir: ../repo/.git/worktrees/worktree\n',
// the Git directory of a linked worktree holds a `commondir` file, which
// is what tells a worktree apart from a submodule.
'../../repo/.git/worktrees/worktree/commondir': '../..\n',
'../package.json': packageJson('worktree'),
'../package-lock.json': packageLock('worktree'),
'../../package.json': packageJson('above-worktree'),
'../../package-lock.json': packageLock('above-worktree'),
},
// So that the files written above don't leave the isolated testDir
subDir: 'worktree/app',
skipDeployment: true,
// The workspace file would stop the search before the worktree boundary
// does, so the test wouldn't be exercising the boundary.
deleteWorkspaceFile: true,
})
if (skipped) {
return
}
it('should not select a root above the worktree', async () => {
const worktreeDir = dirname(next.testDir)
await retry(async () => {
// the lockfile inside the worktree is used, the one above it is not.
expect(next.cliOutput).toContain(
`We detected multiple lockfiles and selected the directory of ${join(
worktreeDir,
'package-lock.json'
)} as the root directory.`
)
})
// a worktree is a checkout of its repository, so stopping at one is
// expected and is not reported as a boundary.
expect(next.cliOutput).not.toMatch(gitRepositoryBoundaryWarning)
})
it('should serve the app', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('hello world')
})
})
import { dirname, join } from 'path'
import { FileRef, isNextDev, nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import {
multipleLockfilesWarning,
packageJson,
packageLock,
} from './test-utils'
// Run the Next.js binary directly. These tests point `HOME` at a directory
// inside the isolated test directory, where `pnpm` would not find its cache.
const nextBin = 'node node_modules/next/dist/bin/next'
// Both cases share this layout and only differ in the home directory that the
// Next.js process sees:
//
// <isolation root>/ a lockfile
// └── project/ a lockfile
// └── app/ the Next.js app
const cases: Array<{
name: string
getHome: (testDir: string) => string
// the outermost directory whose lockfile is still used as the root.
getRoot: (testDir: string) => string
// the lockfile named by the multiple lockfiles warning, if it is expected.
getRootLockFile: (testDir: string) => string | null
}> = [
{
name: 'above the project',
getHome: (testDir) => dirname(dirname(testDir)),
getRoot: (testDir) => dirname(testDir),
getRootLockFile: (testDir) => join(dirname(testDir), 'package-lock.json'),
},
{
// the app directory is always usable as the root, even when it is the home
// directory itself.
name: 'at the app',
getHome: (testDir) => testDir,
getRoot: (testDir) => testDir,
getRootLockFile: () => null,
},
]
describe.each(cases)(
'root-detection - home directory boundary ($name)',
({ getHome, getRoot, getRootLockFile }) => {
const { next, skipped } = nextTestSetup({
files: {
app: new FileRef(join(__dirname, 'app')),
'../package.json': packageJson('project'),
'../package-lock.json': packageLock('project'),
'../../package.json': packageJson('above-project'),
'../../package-lock.json': packageLock('above-project'),
},
// So that the files written above don't leave the isolated testDir
subDir: 'project/app',
skipDeployment: true,
// The workspace file would stop the search before the home directory
// boundary does, so the test wouldn't be exercising the boundary.
deleteWorkspaceFile: true,
// The home directory is only known once the test directory exists.
skipStart: true,
buildCommand: `${nextBin} build`,
startCommand: `${nextBin} ${isNextDev ? 'dev' : 'start'}`,
})
if (skipped) {
return
}
beforeAll(async () => {
const home = getHome(next.testDir)
next.env.HOME = home
// `os.homedir()` reads `USERPROFILE` on Windows.
next.env.USERPROFILE = home
await next.start()
})
it('should not select the home directory as the root', async () => {
const home = getHome(next.testDir)
const root = getRoot(next.testDir)
const rootLockFile = getRootLockFile(next.testDir)
await retry(async () => {
expect(next.cliOutput).toContain(
`Next.js ignored package-lock.json in ${dirname(
root
)} because it would include your home directory (${home}).`
)
})
if (rootLockFile) {
expect(next.cliOutput).toContain(
`We detected multiple lockfiles and selected the directory of ${rootLockFile} as the root directory.`
)
} else {
expect(next.cliOutput).not.toMatch(multipleLockfilesWarning)
}
})
it('should serve the app', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('hello world')
})
}
)
import { join } from 'path'
import { FileRef, nextTestSetup } from 'e2e-utils'
import {
gitRepositoryBoundaryWarning,
homeDirectoryBoundaryWarning,
multipleLockfilesWarning,
packageJson,
packageLock,
} from './test-utils'
// npm, Yarn, and Bun declare a workspace root with a `workspaces` field:
//
// <isolation root>/ the workspace root, and the expected root
// ├── package.json declares `workspaces`
// ├── package-lock.json
// ├── shared/utils.ts imported by the app
// └── test/ the Next.js app, with a lockfile of its own
describe('root-detection - package.json workspaces', () => {
const { next, skipped } = nextTestSetup({
files: {
app: new FileRef(join(__dirname, 'workspace-app')),
'../package.json': packageJson('workspace-root', {
private: true,
workspaces: ['test'],
}),
'../package-lock.json': packageLock('workspace-root'),
'../shared/utils.ts': `export const message = 'hello world'\n`,
},
// So that the files written above don't leave the isolated testDir
subDir: 'test',
skipDeployment: true,
// The workspace file would make the app directory a workspace root of its
// own, so the test wouldn't be exercising the `workspaces` field.
deleteWorkspaceFile: true,
})
if (skipped) {
return
}
it('should select the workspace root as the root', async () => {
// the app imports a file from the workspace root, which is only reachable
// when the root includes it.
const $ = await next.render$('/')
expect($('p').text()).toBe('hello world')
})
it('should not warn about the lockfiles below the workspace root', async () => {
// the `workspaces` field settles where the root is, so neither lockfile is
// ambiguous.
expect(next.cliOutput).not.toMatch(multipleLockfilesWarning)
expect(next.cliOutput).not.toMatch(gitRepositoryBoundaryWarning)
expect(next.cliOutput).not.toMatch(homeDirectoryBoundaryWarning)
})
})
/**
* An npm lockfile, which makes Next.js treat the directory containing it as the
* root of a project.
*/
export function packageLock(name: string): string {
return JSON.stringify({
name,
version: '1.0.0',
lockfileVersion: 3,
packages: { '': { name, version: '1.0.0' } },
})
}
export function packageJson(name: string, fields: object = {}): string {
return JSON.stringify({ name, version: '1.0.0', ...fields })
}
export const multipleLockfilesWarning =
/We detected multiple lockfiles and selected the directory of .+ as the root directory\./
export const gitRepositoryBoundaryWarning =
/because it is outside the current Git repository/
export const homeDirectoryBoundaryWarning =
/because it would include your home directory/
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
// `shared/utils` is written to the workspace root, above the app directory, so
// this import only resolves when the workspace root is the inferred root.
import { message } from '../../shared/utils'
export default function Page() {
return <p>{message}</p>
}
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