Unverified Commit e8349a52 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

test(cna): split and refactor (#63921)

> Note: Did not add additional tests or make many changes to the utils,
possible refactoring on the following PR.

This PR split the legacy tests into four sections to improve the
maintenance and concurrency of CNA tests:

- `prompts`: target prompt interactions. `Y/n`
- `examples`:  target `--example` and `--example-path` flags.
- `templates`: target the flag values such as `--app`, `--eslint`, etc.
- `package-manager`: target package managers: npm, pnpm, yarn, bun

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent d93e68fb
......@@ -118,7 +118,6 @@
"abort-controller": "3.0.0",
"alex": "9.1.0",
"amphtml-validator": "1.0.35",
"ansi-escapes": "4.3.2",
"async-sema": "3.0.1",
"browserslist": "4.22.2",
"buffer": "5.6.0",
......
......@@ -197,9 +197,6 @@ importers:
amphtml-validator:
specifier: 1.0.35
version: 1.0.35
ansi-escapes:
specifier: 4.3.2
version: 4.3.2
async-sema:
specifier: 3.0.1
version: 3.0.1
......@@ -8505,7 +8502,7 @@ packages:
engines: {node: '>= 8.0.0'}
dependencies:
find-babel-config: 1.2.0
glob: 7.1.7
glob: 7.1.6
pkg-up: 3.1.0
reselect: 4.1.8
resolve: 1.22.4
......@@ -12414,7 +12411,7 @@ packages:
engines: {node: '>= 10.17.0'}
hasBin: true
dependencies:
debug: 4.1.1
debug: 4.3.4
get-stream: 5.1.0
yauzl: 2.10.0
optionalDependencies:
......@@ -13973,7 +13970,7 @@ packages:
engines: {node: '>= 6'}
dependencies:
agent-base: 6.0.2
debug: 4.1.1
debug: 4.3.4
transitivePeerDependencies:
- supports-color
dev: true
......
import {
EXAMPLE_PATH,
EXAMPLE_REPO,
FULL_EXAMPLE_PATH,
projectFilesShouldExist,
projectFilesShouldNotExist,
shouldBeTemplateProject,
run,
useTempDir,
} from './utils'
describe('create-next-app --example', () => {
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'], {
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], {
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/',
],
{
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,
],
{
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,
],
{
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=@/*',
],
{
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=@/*',
],
{
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'], {
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',
],
{
cwd,
reject: false,
}
)
expect(res.exitCode).toBe(1)
})
})
})
import {
command,
DEFAULT_FILES,
FULL_EXAMPLE_PATH,
projectFilesShouldExist,
run,
useTempDir,
} from '../utils'
const lockFile = 'bun.lockb'
const files = [...DEFAULT_FILES, lockFile]
beforeEach(async () => {
await command('bun', ['--version'])
// install bun if not available
.catch(() => command('npm', ['i', '-g', 'bun']))
})
describe('create-next-app with package manager bun', () => {
it('should use bun for --use-bun flag', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-bun'
const res = await run(
[
projectName,
'--ts',
'--app',
'--use-bun',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
it('should use bun when user-agent is bun', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-bun'
const res = await run(
[
projectName,
'--ts',
'--app',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
env: { npm_config_user_agent: 'bun' },
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
it('should use bun for --use-bun flag with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-bun-with-example'
const res = await run(
[projectName, '--use-bun', '--example', FULL_EXAMPLE_PATH],
{ cwd }
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
it('should use bun when user-agent is bun with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-bun-with-example'
const res = await run([projectName, '--example', FULL_EXAMPLE_PATH], {
cwd,
env: { npm_config_user_agent: 'bun' },
})
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
import {
DEFAULT_FILES,
FULL_EXAMPLE_PATH,
projectFilesShouldExist,
run,
useTempDir,
} from '../utils'
const lockFile = 'package-lock.json'
const files = [...DEFAULT_FILES, lockFile]
describe('create-next-app with package manager npm', () => {
it('should use npm for --use-npm flag', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-npm'
const res = await run(
[
projectName,
'--ts',
'--app',
'--use-npm',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
it('should use npm when user-agent is npm', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-npm'
const res = await run(
[
projectName,
'--ts',
'--app',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
env: { npm_config_user_agent: 'npm' },
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
it('should use npm for --use-npm flag with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-npm-with-example'
const res = await run(
[projectName, '--use-npm', '--example', FULL_EXAMPLE_PATH],
{ cwd }
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
it('should use npm when user-agent is npm with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-npm-with-example'
const res = await run([projectName, '--example', FULL_EXAMPLE_PATH], {
cwd,
env: { npm_config_user_agent: 'npm' },
})
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
import {
command,
DEFAULT_FILES,
FULL_EXAMPLE_PATH,
projectFilesShouldExist,
run,
useTempDir,
} from '../utils'
const lockFile = 'pnpm-lock.yaml'
const files = [...DEFAULT_FILES, lockFile]
beforeEach(async () => {
await command('pnpm', ['--version'])
// install pnpm if not available
.catch(() => command('corepack', ['prepare', '--activate', 'pnpm@latest']))
.catch(() => command('npm', ['i', '-g', 'pnpm']))
})
describe('create-next-app with package manager pnpm', () => {
it('should use pnpm for --use-pnpm flag', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-pnpm'
const res = await run(
[
projectName,
'--ts',
'--app',
'--use-pnpm',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
it('should use pnpm when user-agent is pnpm', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-pnpm'
const res = await run(
[
projectName,
'--ts',
'--app',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
env: { npm_config_user_agent: 'pnpm' },
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
it('should use pnpm for --use-pnpm flag with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-pnpm-with-example'
const res = await run(
[projectName, '--use-pnpm', '--example', FULL_EXAMPLE_PATH],
{ cwd }
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
it('should use pnpm when user-agent is pnpm with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-pnpm-with-example'
const res = await run([projectName, '--example', FULL_EXAMPLE_PATH], {
cwd,
env: { npm_config_user_agent: 'pnpm' },
})
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
import {
command,
DEFAULT_FILES,
FULL_EXAMPLE_PATH,
projectFilesShouldExist,
run,
useTempDir,
} from '../utils'
const lockFile = 'yarn.lock'
const files = [...DEFAULT_FILES, lockFile]
beforeEach(async () => {
await command('yarn', ['--version'])
// install yarn if not available
.catch(() => command('corepack', ['prepare', '--activate', 'yarn@1.22.19']))
.catch(() => command('npm', ['i', '-g', 'yarn']))
})
describe('create-next-app with package manager yarn', () => {
it('should use yarn for --use-yarn flag', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-yarn'
const res = await run(
[
projectName,
'--ts',
'--app',
'--use-yarn',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
})
it('should use yarn when user-agent is yarn', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-yarn'
const res = await run(
[
projectName,
'--ts',
'--app',
'--no-eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
env: { npm_config_user_agent: 'yarn' },
}
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
it('should use yarn for --use-yarn flag with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'use-yarn-with-example'
const res = await run(
[projectName, '--use-yarn', '--example', FULL_EXAMPLE_PATH],
{ cwd }
)
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
it('should use yarn when user-agent is yarn with example', async () => {
await useTempDir(async (cwd) => {
const projectName = 'user-agent-yarn-with-example'
const res = await run([projectName, '--example', FULL_EXAMPLE_PATH], {
cwd,
env: { npm_config_user_agent: 'yarn' },
})
expect(res.exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files,
})
})
})
import { join } from 'path'
import { check } from 'next-test-utils'
import { createNextApp, projectFilesShouldExist, useTempDir } from './utils'
let testVersion
beforeAll(async () => {
// TODO: investigate moving this post publish or create deployed GH#57025
// tarballs to avoid these failing while a publish is in progress
testVersion = 'canary'
// const span = new Span({ name: 'parent' })
// testVersion = (
// await createNextInstall({ onlyPackages: true, parentSpan: span })
// ).get('next')
})
describe('create-next-app prompts', () => {
it('should prompt user for choice if directory name is absent', async () => {
await useTempDir(async (cwd) => {
const projectName = 'no-dir-name'
const childProcess = createNextApp(
[
'--ts',
'--app',
'--eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
}
)
await new Promise<void>((resolve) => {
childProcess.on('exit', async (exitCode) => {
expect(exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files: ['package.json'],
})
resolve()
})
// enter project name
childProcess.stdin.write(`${projectName}\n`)
})
const pkg = require(join(cwd, projectName, 'package.json'))
expect(pkg.name).toBe(projectName)
})
})
it('should prompt user for choice if --js or --ts flag is absent', async () => {
await useTempDir(async (cwd) => {
const projectName = 'ts-js'
const childProcess = createNextApp(
[
projectName,
'--app',
'--eslint',
'--no-tailwind',
'--no-src-dir',
'--no-import-alias',
],
{
cwd,
},
testVersion
)
await new Promise<void>((resolve) => {
childProcess.on('exit', async (exitCode) => {
expect(exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files: ['tsconfig.json'],
})
resolve()
})
// select default choice: typescript
childProcess.stdin.write('\n')
})
})
})
it('should prompt user for choice if --tailwind is absent', async () => {
await useTempDir(async (cwd) => {
const projectName = 'tw'
const childProcess = createNextApp(
[
projectName,
'--ts',
'--app',
'--eslint',
'--no-src-dir',
'--no-import-alias',
],
{
cwd,
},
testVersion
)
await new Promise<void>((resolve) => {
childProcess.on('exit', async (exitCode) => {
expect(exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files: ['tailwind.config.ts'],
})
resolve()
})
// select default choice: tailwind
childProcess.stdin.write('\n')
})
})
})
it('should prompt user for choice if --import-alias is absent', async () => {
await useTempDir(async (cwd) => {
const projectName = 'import-alias'
const childProcess = createNextApp(
[
projectName,
'--ts',
'--app',
'--eslint',
'--no-tailwind',
'--no-src-dir',
],
{
cwd,
},
testVersion
)
await new Promise<void>(async (resolve) => {
childProcess.on('exit', async (exitCode) => {
expect(exitCode).toBe(0)
resolve()
})
let output = ''
childProcess.stdout.on('data', (data) => {
output += data
process.stdout.write(data)
})
// cursor forward, choose 'Yes' for custom import alias
childProcess.stdin.write('\u001b[C\n')
// used check here since it needs to wait for the prompt
await check(() => output, /What import alias would you like configured/)
childProcess.stdin.write('@/something/*\n')
})
const tsConfig = require(join(cwd, projectName, 'tsconfig.json'))
expect(tsConfig.compilerOptions.paths).toMatchInlineSnapshot(`
{
"@/something/*": [
"./*",
],
}
`)
})
})
})
/* eslint-env jest */
/**
* @fileoverview
*
* This file contains tests for `create-next-app` templates, currently
* JavaScript (default), TypeScript, and appDir.
*/
import path from 'path'
import fs from 'fs-extra'
import {
createNextApp,
shouldBeTemplateProject,
spawnExitPromise,
} from './lib/utils'
import { useTempDir } from '../../lib/use-temp-dir'
import { fetchViaHTTP, findPort, killApp, launchApp } from 'next-test-utils'
import resolveFrom from 'resolve-from'
const startsWithoutError = async (
appDir: string,
modes = ['default', 'turbo'],
usingAppDirectory: boolean = false
) => {
for (const mode of modes) {
appDir = await fs.realpath(appDir)
const appPort = await findPort()
const app = await launchApp(appDir, appPort, {
turbo: mode === 'turbo',
cwd: appDir,
nextBin: resolveFrom(appDir, 'next/dist/bin/next'),
})
try {
const res = await fetchViaHTTP(appPort, '/')
expect(await res.text()).toContain('Get started by editing')
expect(res.status).toBe(200)
tryNextDev,
useTempDir,
} from '../utils'
if (!usingAppDirectory) {
const apiRes = await fetchViaHTTP(appPort, '/api/hello')
expect(await apiRes.json()).toEqual({ name: 'John Doe' })
expect(apiRes.status).toBe(200)
}
} finally {
await killApp(app)
}
}
}
let testVersion
beforeAll(async () => {
if (testVersion) return
// TODO: investigate moving this post publish or create deployed GH#57025
// tarballs to avoid these failing while a publish is in progress
testVersion = 'canary'
// const span = new Span({ name: 'parent' })
// testVersion = (
// await createNextInstall({ onlyPackages: true, parentSpan: span })
// ).get('next')
})
describe('create-next-app --app', () => {
beforeAll(async () => {
if (testVersion) return
// TODO: investigate moving this post publish or create deployed
// tarballs to avoid these failing while a publish is in progress
testVersion = 'canary'
// const span = new Span({ name: 'parent' })
// testVersion = (
// await createNextInstall({ onlyPackages: true, parentSpan: span })
// ).get('next')
})
it('should create TS appDir projects with --ts', async () => {
describe('create-next-app --app (App Router)', () => {
it('should create JavaScript project with --js flag', async () => {
await useTempDir(async (cwd) => {
const projectName = 'appdir-test'
const projectName = 'app-js'
const childProcess = createNextApp(
[
projectName,
'--ts',
'--no-tailwind',
'--js',
'--app',
'--eslint',
'--no-src-dir',
`--import-alias=@/*`,
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
......@@ -82,27 +40,26 @@ describe('create-next-app --app', () => {
const exitCode = await spawnExitPromise(childProcess)
expect(exitCode).toBe(0)
shouldBeTemplateProject({ cwd, projectName, template: 'app', mode: 'ts' })
await startsWithoutError(
path.join(cwd, projectName),
['default', 'turbo'],
true
)
shouldBeTemplateProject({ cwd, projectName, template: 'app', mode: 'js' })
await tryNextDev({
cwd,
projectName,
})
})
})
it('should create JS appDir projects with --js', async () => {
it('should create TypeScript project with --ts flag', async () => {
await useTempDir(async (cwd) => {
const projectName = 'appdir-test'
const childProcess = createNextApp(
const projectName = 'app-ts'
const cp = createNextApp(
[
projectName,
'--js',
'--no-tailwind',
'--ts',
'--app',
'--eslint',
'--no-src-dir',
`--import-alias=@/*`,
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
......@@ -110,29 +67,27 @@ describe('create-next-app --app', () => {
testVersion
)
const exitCode = await spawnExitPromise(childProcess)
const exitCode = await spawnExitPromise(cp)
expect(exitCode).toBe(0)
shouldBeTemplateProject({ cwd, projectName, template: 'app', mode: 'js' })
// is landed
await startsWithoutError(
path.join(cwd, projectName),
['default', 'turbo'],
true
)
shouldBeTemplateProject({ cwd, projectName, template: 'app', mode: 'ts' })
await tryNextDev({
cwd,
projectName,
})
})
})
it('should create JS appDir projects with --js --src-dir', async () => {
it('should create project inside "src" directory with --src-dir flag', async () => {
await useTempDir(async (cwd) => {
const projectName = 'appdir-test'
const projectName = 'app-src-dir'
const childProcess = createNextApp(
[
projectName,
'--js',
'--no-tailwind',
'--ts',
'--app',
'--eslint',
'--src-dir',
'--no-tailwind',
'--no-import-alias',
],
{
......@@ -148,29 +103,28 @@ describe('create-next-app --app', () => {
cwd,
projectName,
template: 'app',
mode: 'js',
mode: 'ts',
srcDir: true,
})
await startsWithoutError(
path.join(cwd, projectName),
['default', 'turbo'],
true
)
await tryNextDev({
cwd,
projectName,
})
})
})
it('should create Tailwind CSS appDir projects with --tailwind', async () => {
it('should create TailwindCSS project with --tailwind flag', async () => {
await useTempDir(async (cwd) => {
const projectName = 'appdir-tailwind-test'
const projectName = 'app-tw'
const childProcess = createNextApp(
[
projectName,
'--ts',
'--tailwind',
'--app',
'--eslint',
'--src-dir',
`--import-alias=@/*`,
'--tailwind',
'--no-import-alias',
],
{
cwd,
......@@ -187,11 +141,10 @@ describe('create-next-app --app', () => {
mode: 'ts',
srcDir: true,
})
await startsWithoutError(
path.join(cwd, projectName),
['default', 'turbo'],
true
)
await tryNextDev({
cwd,
projectName,
})
})
})
})
import {
createNextApp,
shouldBeTemplateProject,
spawnExitPromise,
tryNextDev,
useTempDir,
} from '../utils'
let testVersion
beforeAll(async () => {
// TODO: investigate moving this post publish or create deployed GH#57025
// tarballs to avoid these failing while a publish is in progress
testVersion = 'canary'
// const span = new Span({ name: 'parent' })
// testVersion = (
// await createNextInstall({ onlyPackages: true, parentSpan: span })
// ).get('next')
})
describe('create-next-app --no-app (Pages Router)', () => {
it('should create JavaScript project with --js flag', async () => {
await useTempDir(async (cwd) => {
const projectName = 'pages-js'
const childProcess = createNextApp(
[
projectName,
'--js',
'--no-app',
'--eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
},
testVersion
)
const exitCode = await spawnExitPromise(childProcess)
expect(exitCode).toBe(0)
shouldBeTemplateProject({
cwd,
projectName,
template: 'default',
mode: 'js',
})
await tryNextDev({
cwd,
projectName,
isApp: false,
})
})
})
it('should create TypeScript project with --ts flag', async () => {
await useTempDir(async (cwd) => {
const projectName = 'pages-ts'
const childProcess = createNextApp(
[
projectName,
'--ts',
'--no-app',
'--eslint',
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
},
testVersion
)
const exitCode = await spawnExitPromise(childProcess)
expect(exitCode).toBe(0)
shouldBeTemplateProject({
cwd,
projectName,
template: 'default',
mode: 'ts',
})
await tryNextDev({
cwd,
projectName,
isApp: false,
})
})
})
it('should create project inside "src" directory with --src-dir flag', async () => {
await useTempDir(async (cwd) => {
const projectName = 'pages-src-dir'
const childProcess = createNextApp(
[
projectName,
'--ts',
'--no-app',
'--eslint',
'--src-dir',
'--no-tailwind',
'--no-import-alias',
],
{
cwd,
},
testVersion
)
const exitCode = await spawnExitPromise(childProcess)
expect(exitCode).toBe(0)
shouldBeTemplateProject({
cwd,
projectName,
template: 'default',
mode: 'ts',
srcDir: true,
})
await tryNextDev({
cwd,
projectName,
isApp: false,
})
})
})
it('should create TailwindCSS project with --tailwind flag', async () => {
await useTempDir(async (cwd) => {
const projectName = 'pages-tw'
const childProcess = createNextApp(
[
projectName,
'--ts',
'--no-app',
'--eslint',
'--src-dir',
'--tailwind',
'--no-import-alias',
],
{
cwd,
},
testVersion
)
const exitCode = await spawnExitPromise(childProcess)
expect(exitCode).toBe(0)
shouldBeTemplateProject({
cwd,
projectName,
template: 'default-tw',
mode: 'ts',
srcDir: true,
})
await tryNextDev({
cwd,
projectName,
isApp: false,
})
})
})
})
import execa from 'execa'
import { join } from 'path'
import { fetchViaHTTP, findPort, killApp, launchApp } from 'next-test-utils'
export const CNA_PATH = require.resolve('create-next-app/dist/index.js')
export const EXAMPLE_REPO = 'https://github.com/vercel/next.js/tree/canary'
export const EXAMPLE_PATH = 'examples/basic-css'
export const FULL_EXAMPLE_PATH = `${EXAMPLE_REPO}/${EXAMPLE_PATH}`
export const DEFAULT_FILES = [
'.gitignore',
'package.json',
'app/page.tsx',
'app/layout.tsx',
'node_modules/next',
]
export const run = (
args: string[],
options:
| execa.Options
| {
reject?: boolean
env?: Record<string, string>
}
) =>
execa('node', [CNA_PATH].concat(args), {
// tests with options.reject false are expected to exit(1) so don't inherit
stdio: options.reject === false ? 'pipe' : 'inherit',
...options,
env: { ...process.env, ...options.env },
})
export const command = (cmd: string, args: string[]) =>
execa(cmd, args, {
stdio: 'inherit',
env: { ...process.env },
})
export async function tryNextDev({
cwd,
projectName,
isApp = true,
}: {
cwd: string
projectName: string
isApp?: boolean
}) {
const dir = join(cwd, projectName)
const port = await findPort()
const app = await launchApp(dir, port, {
nextBin: join(dir, 'node_modules/next/dist/bin/next'),
})
try {
const res = await fetchViaHTTP(port, '/')
expect(await res.text()).toContain('Get started by editing')
expect(res.status).toBe(200)
if (!isApp) {
const apiRes = await fetchViaHTTP(port, '/api/hello')
expect(await apiRes.json()).toEqual({ name: 'John Doe' })
expect(apiRes.status).toBe(200)
}
} finally {
await killApp(app)
}
}
export {
createNextApp,
projectFilesShouldExist,
projectFilesShouldNotExist,
shouldBeTemplateProject,
shouldBeJavascriptProject,
shouldBeTypescriptProject,
spawnExitPromise,
} from './lib/utils'
export { useTempDir } from '../../lib/use-temp-dir'
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