forked from guocaoyi/create-chrome-ext
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.spec.ts
93 lines (75 loc) · 3.07 KB
/
index.spec.ts
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
import { join } from 'node:path'
import type { ExecaSyncReturnValue, SyncOptions } from 'execa'
import { execaCommandSync } from 'execa'
import { mkdirpSync, readdirSync, remove, writeFileSync } from 'fs-extra'
import { afterEach, beforeAll, expect, test } from 'vitest'
const CLI_PATH = join(__dirname, '..')
const projectName = 'test-app'
const genPath = join(__dirname, projectName)
const run = (args: string[], options: SyncOptions<string> = {}): ExecaSyncReturnValue<string> => {
return execaCommandSync(`node ${CLI_PATH} ${args.join(' ')}`, options)
}
// Helper to create a non-empty directory
const createNonEmptyDir = () => {
// Create the temporary directory
mkdirpSync(genPath)
// Create a package.json file
const pkgJson = join(genPath, 'package.json')
writeFileSync(pkgJson, '{ "foo": "bar" }')
}
// Vue 3 starter template
const templateFiles = readdirSync(join(CLI_PATH, 'template-vue'))
// _gitignore is renamed to .gitignore
.map((filePath) => (filePath === '_gitignore' ? '.gitignore' : filePath))
.sort()
beforeAll(() => remove(genPath))
afterEach(() => remove(genPath))
test('prompts for the project name if none supplied', () => {
const { stdout, exitCode } = run([])
expect(stdout).toContain('Project name:')
})
test('prompts for the framework if none supplied when target dir is current directory', () => {
mkdirpSync(genPath)
const { stdout } = run(['.'], { cwd: genPath })
expect(stdout).toContain('Select a framework:')
})
test('prompts for the framework if none supplied', () => {
const { stdout } = run([projectName])
expect(stdout).toContain('Select a framework:')
})
test('prompts for the framework on not supplying a value for --template', () => {
const { stdout } = run([projectName, '--template'])
expect(stdout).toContain('Select a framework:')
})
test('prompts for the framework on supplying an invalid template', () => {
const { stdout } = run([projectName, '--template', 'unknown'])
expect(stdout).toContain(`"unknown" isn't a valid template. Please choose from below:`)
})
test('asks to overwrite non-empty target directory', () => {
createNonEmptyDir()
const { stdout } = run([projectName], { cwd: __dirname })
expect(stdout).toContain(`Target directory "${projectName}" is not empty.`)
})
test('asks to overwrite non-empty current directory', () => {
createNonEmptyDir()
const { stdout } = run(['.'], { cwd: genPath })
expect(stdout).toContain(`Current directory is not empty.`)
})
test('successfully scaffolds a project based on vue starter template', () => {
const { stdout } = run([projectName, '--template', 'vue'], {
cwd: __dirname,
})
const generatedFiles = readdirSync(genPath).sort()
// Assertions
expect(stdout).toContain(`Scaffolding project in ${genPath}`)
expect(templateFiles).toEqual(generatedFiles)
})
test('works with the -t alias', () => {
const { stdout } = run([projectName, '-t', 'vue'], {
cwd: __dirname,
})
const generatedFiles = readdirSync(genPath).sort()
// Assertions
expect(stdout).toContain(`Scaffolding project in ${genPath}`)
expect(templateFiles).toEqual(generatedFiles)
})