Unverified Commit 4f0d76c4 authored by Lee Robinson's avatar Lee Robinson Committed by GitHub

Migrate `create-next-app` and e2e tests to Metadata API. (#45819)

Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
Co-authored-by: 's avatarBalázs Orbán <info@balazsorban.com>
parent 1275fdc0
export async function GET(request) {
return new Response('Hello, Next.js!')
}
export default function Head() {
return (
<>
<title>Create Next App</title>
<meta content="width=device-width, initial-scale=1" name="viewport" />
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</>
)
}
import './globals.css' import './globals.css'
export const metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
}
export default function RootLayout({ children }) { export default function RootLayout({ children }) {
return ( return (
<html lang="en"> <html lang="en">
{/*
<head /> will contain the components returned by the nearest parent
head.js. Find out more at https://beta.nextjs.org/docs/api-reference/file-conventions/head
*/}
<head />
<body>{children}</body> <body>{children}</body>
</html> </html>
) )
......
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
export default function handler(req, res) {
res.status(200).json({ name: 'John Doe' })
}
export async function GET(request: Request) {
return new Response('Hello, Next.js!')
}
export default function Head() {
return (
<>
<title>Create Next App</title>
<meta content="width=device-width, initial-scale=1" name="viewport" />
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</>
)
}
import './globals.css' import './globals.css'
export const metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
}
export default function RootLayout({ export default function RootLayout({
children, children,
}: { }: {
...@@ -7,11 +12,6 @@ export default function RootLayout({ ...@@ -7,11 +12,6 @@ export default function RootLayout({
}) { }) {
return ( return (
<html lang="en"> <html lang="en">
{/*
<head /> will contain the components returned by the nearest parent
head.tsx. Find out more at https://beta.nextjs.org/docs/api-reference/file-conventions/head
*/}
<head />
<body>{children}</body> <body>{children}</body>
</html> </html>
) )
......
/// <reference types="next" /> /// <reference types="next" />
/// <reference types="next/image-types/global" /> /// <reference types="next/image-types/global" />
/// <reference types="next/navigation-types/compat/navigation" /> /// <reference types="next/navigation-types/navigation" />
// NOTE: This file should not be edited // NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information. // see https://nextjs.org/docs/basic-features/typescript for more information.
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next'
type Data = {
name: string
}
export default function handler(
req: NextApiRequest,
res: NextApiResponse<Data>
) {
res.status(200).json({ name: 'John Doe' })
}
import './globals.css' import './globals.css'
export const metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
}
export default function RootLayout({ export default function RootLayout({
children, children,
}: { }: {
...@@ -7,11 +12,6 @@ export default function RootLayout({ ...@@ -7,11 +12,6 @@ export default function RootLayout({
}) { }) {
return ( return (
<html lang="en"> <html lang="en">
<head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</head>
<body>{children}</body> <body>{children}</body>
</html> </html>
) )
......
export default function Head() {
return (
<>
<title></title>
<meta content="width=device-width, initial-scale=1" name="viewport" />
<link rel="icon" href="/favicon.ico" />
</>
)
}
...@@ -102,7 +102,7 @@ describe('app dir - rsc basics', () => { ...@@ -102,7 +102,7 @@ describe('app dir - rsc basics', () => {
// should have only 1 DOCTYPE // should have only 1 DOCTYPE
expect(homeHTML).toMatch(/^<!DOCTYPE html><html/) expect(homeHTML).toMatch(/^<!DOCTYPE html><html/)
// should have default head when there's no head.js provided // should have default metadata when there's nothing additional provided
expect(homeHTML).toContain('<meta charSet="utf-8"/>') expect(homeHTML).toContain('<meta charSet="utf-8"/>')
expect(homeHTML).toContain( expect(homeHTML).toContain(
'<meta name="viewport" content="width=device-width, initial-scale=1"/>' '<meta name="viewport" content="width=device-width, initial-scale=1"/>'
......
...@@ -62,9 +62,8 @@ export const projectSpecification: ProjectSpecification = { ...@@ -62,9 +62,8 @@ export const projectSpecification: ProjectSpecification = {
devDeps: [], devDeps: [],
files: [ files: [
'app/page.js', 'app/page.js',
'app/head.js',
'app/layout.js', 'app/layout.js',
'pages/api/hello.js', 'app/api/hello/route.js',
'jsconfig.json', 'jsconfig.json',
], ],
}, },
...@@ -73,9 +72,8 @@ export const projectSpecification: ProjectSpecification = { ...@@ -73,9 +72,8 @@ export const projectSpecification: ProjectSpecification = {
devDeps: [], devDeps: [],
files: [ files: [
'app/page.tsx', 'app/page.tsx',
'app/head.tsx',
'app/layout.tsx', 'app/layout.tsx',
'pages/api/hello.ts', 'app/api/hello/route.ts',
'tsconfig.json', 'tsconfig.json',
'next-env.d.ts', 'next-env.d.ts',
], ],
......
...@@ -24,7 +24,8 @@ import { getPkgPaths } from '../../../test/lib/create-next-install' ...@@ -24,7 +24,8 @@ import { getPkgPaths } from '../../../test/lib/create-next-install'
const startsWithoutError = async ( const startsWithoutError = async (
appDir: string, appDir: string,
modes = ['default', 'turbo'] modes = ['default', 'turbo'],
usingAppDirectory: boolean = false
) => { ) => {
for (const mode of modes) { for (const mode of modes) {
appDir = await fs.realpath(appDir) appDir = await fs.realpath(appDir)
...@@ -42,7 +43,11 @@ const startsWithoutError = async ( ...@@ -42,7 +43,11 @@ const startsWithoutError = async (
const apiRes = await fetchViaHTTP(appPort, '/api/hello') const apiRes = await fetchViaHTTP(appPort, '/api/hello')
expect(apiRes.status).toBe(200) expect(apiRes.status).toBe(200)
expect(await apiRes.json()).toEqual({ name: 'John Doe' }) if (usingAppDirectory) {
expect(await apiRes.text()).toEqual('Hello, Next.js!')
} else {
expect(await apiRes.json()).toEqual({ name: 'John Doe' })
}
} finally { } finally {
await killApp(app) await killApp(app)
} }
...@@ -336,7 +341,11 @@ describe('create-next-app --experimental-app-dir', () => { ...@@ -336,7 +341,11 @@ describe('create-next-app --experimental-app-dir', () => {
const exitCode = await spawnExitPromise(childProcess) const exitCode = await spawnExitPromise(childProcess)
expect(exitCode).toBe(0) expect(exitCode).toBe(0)
shouldBeTemplateProject({ cwd, projectName, template: 'app', mode: 'ts' }) shouldBeTemplateProject({ cwd, projectName, template: 'app', mode: 'ts' })
await startsWithoutError(path.join(cwd, projectName)) await startsWithoutError(
path.join(cwd, projectName),
['default', 'turbo'],
true
)
}) })
}) })
...@@ -362,10 +371,11 @@ describe('create-next-app --experimental-app-dir', () => { ...@@ -362,10 +371,11 @@ describe('create-next-app --experimental-app-dir', () => {
expect(exitCode).toBe(0) expect(exitCode).toBe(0)
shouldBeTemplateProject({ cwd, projectName, template: 'app', mode: 'js' }) shouldBeTemplateProject({ cwd, projectName, template: 'app', mode: 'js' })
// is landed // is landed
await startsWithoutError(path.join(cwd, projectName), [ await startsWithoutError(
'default', path.join(cwd, projectName),
'turbo', ['default', 'turbo'],
]) true
)
}) })
}) })
...@@ -397,10 +407,11 @@ describe('create-next-app --experimental-app-dir', () => { ...@@ -397,10 +407,11 @@ describe('create-next-app --experimental-app-dir', () => {
mode: 'js', mode: 'js',
srcDir: true, srcDir: true,
}) })
await startsWithoutError(path.join(cwd, projectName), [ await startsWithoutError(
'default', path.join(cwd, projectName),
'turbo', ['default', 'turbo'],
]) true
)
}) })
}) })
}) })
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