Files
next.js/test/integration/create-next-app/examples.test.ts
Arian Tron 61f56f997c
Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
first commit
2026-03-10 19:37:31 +03:30

310 lines
7.0 KiB
TypeScript

import {
EXAMPLE_PATH,
EXAMPLE_REPO,
FULL_EXAMPLE_PATH,
projectFilesShouldExist,
projectFilesShouldNotExist,
shouldBeTemplateProject,
run,
useTempDir,
} from './utils'
describe('create-next-app --example', () => {
let nextTgzFilename: string
beforeAll(() => {
if (!process.env.NEXT_TEST_PKG_PATHS) {
throw new Error('This test needs to be run with `node run-tests.js`.')
}
const pkgPaths = new Map<string, string>(
JSON.parse(process.env.NEXT_TEST_PKG_PATHS)
)
nextTgzFilename = pkgPaths.get('next')
})
it('should create on valid Next.js example name', async () => {
await useTempDir(async (cwd) => {
const projectName = 'valid-example'
const res = await run(
[
projectName,
'--example',
'basic-css',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
{
cwd,
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files: [
'.gitignore',
'package.json',
'app/page.tsx',
'app/layout.tsx',
'node_modules/next',
],
})
})
})
it('should create with GitHub URL', async () => {
await useTempDir(async (cwd) => {
const projectName = 'github-url'
const res = await run(
[
projectName,
'--example',
FULL_EXAMPLE_PATH,
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
{
cwd,
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files: [
'.gitignore',
'package.json',
'app/page.tsx',
'app/layout.tsx',
'node_modules/next',
],
})
})
})
it('should create with GitHub URL trailing slash', async () => {
await useTempDir(async (cwd) => {
const projectName = 'github-url-trailing-slash'
const res = await run(
[
projectName,
'--example',
// since vercel/examples is not a template repo, we use the following
// GH#39665
'https://github.com/vercel/nextjs-portfolio-starter/',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
{
cwd,
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files: [
'.gitignore',
'package.json',
'pages/index.mdx',
'node_modules/next',
],
})
})
})
it('should create with GitHub URL and --example-path', async () => {
await useTempDir(async (cwd) => {
const projectName = 'github-url-and-example-path'
const res = await run(
[
projectName,
'--js',
'--no-tailwind',
'--eslint',
'--example',
EXAMPLE_REPO,
'--example-path',
EXAMPLE_PATH,
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
{
cwd,
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files: [
'.gitignore',
'package.json',
'app/page.tsx',
'app/layout.tsx',
'node_modules/next',
],
})
})
})
it('should use --example-path over the GitHub URL', async () => {
await useTempDir(async (cwd) => {
const projectName = 'example-path-over-github-url'
const res = await run(
[
projectName,
'--js',
'--no-tailwind',
'--eslint',
'--example',
FULL_EXAMPLE_PATH,
'--example-path',
EXAMPLE_PATH,
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
{
cwd,
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files: [
'.gitignore',
'package.json',
'app/page.tsx',
'app/layout.tsx',
'node_modules/next',
],
})
})
})
// TODO: investigate why this test stalls on yarn install when
// stdin is piped instead of inherited on windows
if (process.platform !== 'win32') {
it('should fall back to default template if failed to download', async () => {
await useTempDir(async (cwd) => {
const projectName = 'fallback-to-default'
const res = await run(
[
projectName,
'--js',
'--no-tailwind',
'--eslint',
'--app',
'--example',
'__internal-testing-retry',
'--import-alias=@/*',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
{
cwd,
input: '\n', // 'Yes' to retry
stdio: 'pipe',
}
)
expect(res.exitCode).toBe(0)
shouldBeTemplateProject({
cwd,
projectName,
template: 'app',
mode: 'js',
})
})
})
}
it('should create if --example value is default', async () => {
await useTempDir(async (cwd) => {
const projectName = 'example-default'
const res = await run(
[
projectName,
'--js',
'--no-tailwind',
'--eslint',
'--example',
'default',
'--import-alias=@/*',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
{
cwd,
}
)
expect(res.exitCode).toBe(0)
shouldBeTemplateProject({
cwd,
projectName,
template: 'default',
mode: 'js',
})
})
})
it('should not create if --example flag value is invalid', async () => {
await useTempDir(async (cwd) => {
const projectName = 'invalid-example'
const res = await run(
[
projectName,
'--example',
'not a real example',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
{
cwd,
reject: false,
}
)
expect(res.exitCode).toBe(1)
projectFilesShouldNotExist({
cwd,
projectName,
files: ['package.json'],
})
})
})
it('should not create if --example flag value is absent', async () => {
await useTempDir(async (cwd) => {
const projectName = 'no-example'
const res = await run(
[
projectName,
'--ts',
'--app',
'--eslint',
'--no-src-dir',
'--no-tailwind',
'--example',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
{
cwd,
reject: false,
}
)
expect(res.exitCode).toBe(1)
})
})
})