2019-07-17 22:53:56 +02:00
|
|
|
/* eslint-env jest */
|
|
|
|
import path from 'path'
|
2020-03-21 17:02:05 +01:00
|
|
|
import fs from 'fs-extra'
|
2019-07-17 22:53:56 +02:00
|
|
|
import execa from 'execa'
|
|
|
|
import os from 'os'
|
|
|
|
|
2019-07-19 21:55:30 +02:00
|
|
|
const cli = require.resolve('create-next-app/dist/index.js')
|
2019-07-17 22:53:56 +02:00
|
|
|
const cwd = path.join(
|
|
|
|
os.tmpdir(),
|
|
|
|
Math.random()
|
|
|
|
.toString(36)
|
|
|
|
.substring(2)
|
|
|
|
)
|
|
|
|
|
|
|
|
const run = (...args) => execa('node', [cli, ...args], { cwd })
|
2020-04-07 19:11:29 +02:00
|
|
|
const runStarter = (...args) => {
|
|
|
|
const res = run(...args)
|
|
|
|
|
|
|
|
res.stdout.on('data', data => {
|
|
|
|
const stdout = data.toString()
|
|
|
|
|
|
|
|
if (/Pick a template/.test(stdout)) {
|
|
|
|
res.stdin.write('\n')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2019-07-17 22:53:56 +02:00
|
|
|
|
|
|
|
describe('create next app', () => {
|
|
|
|
beforeAll(async () => {
|
2020-04-09 19:57:59 +02:00
|
|
|
jest.setTimeout(1000 * 60 * 2)
|
2020-03-21 17:02:05 +01:00
|
|
|
await fs.mkdirp(cwd)
|
2019-07-17 22:53:56 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('non-empty directory', async () => {
|
|
|
|
const projectName = 'non-empty-directory'
|
2020-03-21 17:02:05 +01:00
|
|
|
await fs.mkdirp(path.join(cwd, projectName))
|
2019-07-17 22:53:56 +02:00
|
|
|
const pkg = path.join(cwd, projectName, 'package.json')
|
|
|
|
fs.writeFileSync(pkg, '{ "foo": "bar" }')
|
|
|
|
|
|
|
|
expect.assertions(1)
|
|
|
|
try {
|
2020-04-07 19:11:29 +02:00
|
|
|
await runStarter(projectName)
|
2019-07-17 22:53:56 +02:00
|
|
|
} catch (e) {
|
2019-07-19 21:55:30 +02:00
|
|
|
expect(e.stdout).toMatch(/contains files that could conflict/)
|
2019-07-17 22:53:56 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-04-09 19:57:59 +02:00
|
|
|
// TODO: investigate why this test stalls on yarn install when
|
|
|
|
// stdin is piped instead of inherited on windows
|
|
|
|
if (process.platform !== 'win32') {
|
|
|
|
it('empty directory', async () => {
|
|
|
|
const projectName = 'empty-directory'
|
|
|
|
const res = await runStarter(projectName)
|
|
|
|
|
|
|
|
expect(res.exitCode).toBe(0)
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, 'package.json'))
|
|
|
|
).toBeTruthy()
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, 'pages/index.js'))
|
|
|
|
).toBeTruthy()
|
|
|
|
})
|
|
|
|
}
|
2019-07-17 22:53:56 +02:00
|
|
|
|
|
|
|
it('invalid example name', async () => {
|
|
|
|
const projectName = 'invalid-example-name'
|
2019-07-19 21:55:30 +02:00
|
|
|
expect.assertions(2)
|
|
|
|
try {
|
|
|
|
await run(projectName, '--example', 'not a real example')
|
|
|
|
} catch (e) {
|
|
|
|
expect(e.stderr).toMatch(/Could not locate an example named/i)
|
|
|
|
}
|
2019-07-17 22:53:56 +02:00
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, 'package.json'))
|
|
|
|
).toBeFalsy()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('valid example', async () => {
|
|
|
|
const projectName = 'valid-example'
|
|
|
|
const res = await run(projectName, '--example', 'basic-css')
|
|
|
|
expect(res.exitCode).toBe(0)
|
|
|
|
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, 'package.json'))
|
|
|
|
).toBeTruthy()
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, 'pages/index.js'))
|
|
|
|
).toBeTruthy()
|
2019-11-11 06:42:51 +01:00
|
|
|
// check we copied default `.gitignore`
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, '.gitignore'))
|
|
|
|
).toBeTruthy()
|
2019-07-17 22:53:56 +02:00
|
|
|
})
|
2020-02-27 16:32:33 +01:00
|
|
|
|
|
|
|
it('should allow example with GitHub URL', async () => {
|
|
|
|
const projectName = 'github-app'
|
|
|
|
const res = await run(
|
|
|
|
projectName,
|
|
|
|
'--example',
|
|
|
|
'https://github.com/zeit/next-learn-demo/tree/master/1-navigate-between-pages'
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(res.exitCode).toBe(0)
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, 'package.json'))
|
|
|
|
).toBeTruthy()
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, 'pages/index.js'))
|
|
|
|
).toBeTruthy()
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, 'pages/about.js'))
|
|
|
|
).toBeTruthy()
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, '.gitignore'))
|
|
|
|
).toBeTruthy()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should allow example with GitHub URL and example-path', async () => {
|
|
|
|
const projectName = 'github-example-path'
|
|
|
|
const res = await run(
|
|
|
|
projectName,
|
|
|
|
'--example',
|
|
|
|
'https://github.com/zeit/next-learn-demo/tree/master',
|
|
|
|
'--example-path',
|
|
|
|
'1-navigate-between-pages'
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(res.exitCode).toBe(0)
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, 'package.json'))
|
|
|
|
).toBeTruthy()
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, 'pages/index.js'))
|
|
|
|
).toBeTruthy()
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, 'pages/about.js'))
|
|
|
|
).toBeTruthy()
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, '.gitignore'))
|
|
|
|
).toBeTruthy()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should use --example-path over the file path in the GitHub URL', async () => {
|
|
|
|
const projectName = 'github-example-path-2'
|
|
|
|
const res = await run(
|
|
|
|
projectName,
|
|
|
|
'--example',
|
|
|
|
'https://github.com/zeit/next-learn-demo/tree/master/1-navigate-between-pages',
|
|
|
|
'--example-path',
|
|
|
|
'1-navigate-between-pages'
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(res.exitCode).toBe(0)
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, 'package.json'))
|
|
|
|
).toBeTruthy()
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, 'pages/index.js'))
|
|
|
|
).toBeTruthy()
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, 'pages/about.js'))
|
|
|
|
).toBeTruthy()
|
|
|
|
expect(
|
|
|
|
fs.existsSync(path.join(cwd, projectName, '.gitignore'))
|
|
|
|
).toBeTruthy()
|
|
|
|
})
|
2020-04-07 19:11:29 +02:00
|
|
|
|
2020-04-09 19:57:59 +02:00
|
|
|
// TODO: investigate why this test stalls on yarn install when
|
|
|
|
// stdin is piped instead of inherited on windows
|
|
|
|
if (process.platform !== 'win32') {
|
|
|
|
it('should allow to manually select an example', async () => {
|
|
|
|
const runExample = (...args) => {
|
|
|
|
const res = run(...args)
|
|
|
|
|
|
|
|
function pickExample(data) {
|
|
|
|
if (/hello-world/.test(data.toString())) {
|
|
|
|
res.stdout.removeListener('data', pickExample)
|
|
|
|
res.stdin.write('\n')
|
|
|
|
}
|
2020-04-07 19:11:29 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 19:57:59 +02:00
|
|
|
function searchExample(data) {
|
|
|
|
if (/Pick an example/.test(data.toString())) {
|
|
|
|
res.stdout.removeListener('data', searchExample)
|
|
|
|
res.stdin.write('hello-world')
|
|
|
|
res.stdout.on('data', pickExample)
|
|
|
|
}
|
2020-04-07 19:11:29 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 19:57:59 +02:00
|
|
|
function selectExample(data) {
|
|
|
|
if (/Pick a template/.test(data.toString())) {
|
|
|
|
res.stdout.removeListener('data', selectExample)
|
|
|
|
res.stdin.write('\u001b[B\n') // Down key and enter
|
|
|
|
res.stdout.on('data', searchExample)
|
|
|
|
}
|
2020-04-07 19:11:29 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 19:57:59 +02:00
|
|
|
res.stdout.on('data', selectExample)
|
2020-04-07 19:11:29 +02:00
|
|
|
|
2020-04-09 19:57:59 +02:00
|
|
|
return res
|
|
|
|
}
|
2020-04-07 19:11:29 +02:00
|
|
|
|
2020-04-09 19:57:59 +02:00
|
|
|
const res = await runExample('no-example')
|
2020-04-07 19:11:29 +02:00
|
|
|
|
2020-04-09 19:57:59 +02:00
|
|
|
expect(res.exitCode).toBe(0)
|
|
|
|
expect(res.stdout).toMatch(/Downloading files for example hello-world/)
|
|
|
|
})
|
|
|
|
}
|
2019-07-17 22:53:56 +02:00
|
|
|
})
|