Unverified Commit 5d80e684 authored by James George's avatar James George Committed by GitHub

tests(create-next-app): increase coverage (#18630)

Added a test case for asking for a name for the project if not supplied.
parent 7b66da83
......@@ -8,9 +8,12 @@ const cli = require.resolve('create-next-app/dist/index.js')
jest.setTimeout(1000 * 60 * 5)
const run = (cwd, ...args) => execa('node', [cli, ...args], { cwd })
const run = (cwd, args, input) => {
const options = input ? { cwd, input } : { cwd }
return execa('node', [cli].concat(args), options)
}
const runStarter = (cwd, ...args) => {
const res = run(cwd, ...args)
const res = run(cwd, args)
res.stdout.on('data', (data) => {
const stdout = data.toString()
......@@ -75,7 +78,7 @@ describe('create next app', () => {
const projectName = 'invalid-example-name'
expect.assertions(2)
try {
await run(cwd, projectName, '--example', 'not a real example')
await run(cwd, [projectName, '--example', 'not a real example'])
} catch (e) {
// eslint-disable-next-line jest/no-try-expect
expect(e.stderr).toMatch(/Could not locate an example named/i)
......@@ -89,7 +92,7 @@ describe('create next app', () => {
it('valid example', async () => {
await usingTempDir(async (cwd) => {
const projectName = 'valid-example'
const res = await run(cwd, projectName, '--example', 'basic-css')
const res = await run(cwd, [projectName, '--example', 'basic-css'])
expect(res.exitCode).toBe(0)
expect(
......@@ -108,12 +111,11 @@ describe('create next app', () => {
it('should allow example with GitHub URL', async () => {
await usingTempDir(async (cwd) => {
const projectName = 'github-app'
const res = await run(
cwd,
const res = await run(cwd, [
projectName,
'--example',
'https://github.com/zeit/next-learn-demo/tree/master/1-navigate-between-pages'
)
'https://github.com/zeit/next-learn-demo/tree/master/1-navigate-between-pages',
])
expect(res.exitCode).toBe(0)
expect(
......@@ -134,14 +136,13 @@ describe('create next app', () => {
it('should allow example with GitHub URL and example-path', async () => {
await usingTempDir(async (cwd) => {
const projectName = 'github-example-path'
const res = await run(
cwd,
const res = await run(cwd, [
projectName,
'--example',
'https://github.com/zeit/next-learn-demo/tree/master',
'--example-path',
'1-navigate-between-pages'
)
'1-navigate-between-pages',
])
expect(res.exitCode).toBe(0)
expect(
......@@ -162,14 +163,13 @@ describe('create next app', () => {
it('should use --example-path over the file path in the GitHub URL', async () => {
await usingTempDir(async (cwd) => {
const projectName = 'github-example-path-2'
const res = await run(
cwd,
const res = await run(cwd, [
projectName,
'--example',
'https://github.com/zeit/next-learn-demo/tree/master/1-navigate-between-pages',
'--example-path',
'1-navigate-between-pages'
)
'1-navigate-between-pages',
])
expect(res.exitCode).toBe(0)
expect(
......@@ -193,7 +193,7 @@ describe('create next app', () => {
it('should fall back to default template', async () => {
await usingTempDir(async (cwd) => {
const runExample = (...args) => {
const res = run(cwd, ...args)
const res = run(cwd, args)
function fallbackToTemplate(data) {
if (
......@@ -224,7 +224,7 @@ describe('create next app', () => {
it('should allow an example named default', async () => {
await usingTempDir(async (cwd) => {
const projectName = 'default-example'
const res = await run(cwd, projectName, '--example', 'default')
const res = await run(cwd, [projectName, '--example', 'default'])
expect(res.exitCode).toBe(0)
expect(
......@@ -244,7 +244,7 @@ describe('create next app', () => {
await usingTempDir(async (cwd) => {
try {
const projectName = 'no-example-provided'
await run(cwd, projectName, '--example')
await run(cwd, [projectName, '--example'])
} catch (e) {
// eslint-disable-next-line jest/no-try-expect
expect(e.exitCode).toBe(1)
......@@ -278,7 +278,7 @@ describe('create next app', () => {
it('should create a project in the current directory', async () => {
await usingTempDir(async (cwd) => {
const res = await run(cwd, '.')
const res = await run(cwd, ['.'])
expect(res.exitCode).toBe(0)
const files = ['package.json', 'pages/index.js', '.gitignore']
......@@ -287,4 +287,17 @@ describe('create next app', () => {
)
})
})
it('should ask the user for a name for the project if none supplied', async () => {
await usingTempDir(async (cwd) => {
const projectName = 'test-project'
const res = await run(cwd, [], `${projectName}\n`)
expect(res.exitCode).toBe(0)
const files = ['package.json', 'pages/index.js', '.gitignore']
files.forEach((file) =>
expect(fs.existsSync(path.join(cwd, projectName, file))).toBeTruthy()
)
})
})
})
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