/
index.test.js
157 lines (139 loc) · 4.3 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* eslint-env jest */
import mkdirpModule from 'mkdirp'
import path from 'path'
import fs from 'fs'
import execa from 'execa'
import os from 'os'
import { promisify } from 'util'
const mkdirp = promisify(mkdirpModule)
const cli = require.resolve('create-next-app/dist/index.js')
const cwd = path.join(
os.tmpdir(),
Math.random()
.toString(36)
.substring(2)
)
const run = (...args) => execa('node', [cli, ...args], { cwd })
describe('create next app', () => {
beforeAll(async () => {
jest.setTimeout(1000 * 60)
await mkdirp(cwd)
})
it('non-empty directory', async () => {
const projectName = 'non-empty-directory'
await mkdirp(path.join(cwd, projectName))
const pkg = path.join(cwd, projectName, 'package.json')
fs.writeFileSync(pkg, '{ "foo": "bar" }')
expect.assertions(1)
try {
await run(projectName)
} catch (e) {
expect(e.stdout).toMatch(/contains files that could conflict/)
}
})
it('empty directory', async () => {
const projectName = 'empty-directory'
const res = await run(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()
})
it('invalid example name', async () => {
const projectName = 'invalid-example-name'
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)
}
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()
// check we copied default `.gitignore`
expect(
fs.existsSync(path.join(cwd, projectName, '.gitignore'))
).toBeTruthy()
})
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()
})
})