-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathcli-integration.test.ts
173 lines (136 loc) · 5.23 KB
/
cli-integration.test.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import Bun from 'bun';
import { version } from '../package.json';
import { test, expect, afterEach } from 'bun:test';
import * as path from 'node:path';
type InputFlag = `--${string}`;
const cli = async (inputs: string[]) => {
const pathToFile = `${path.join(__dirname, '../', 'bin', 'create-expo-stack.js')}`;
console.log('running', `bun ${pathToFile} ${inputs.join(' ')}`);
const { stdout, exitCode, success, stderr } = Bun.spawnSync(['bun', pathToFile, ...inputs]);
if (!success || exitCode !== 0) {
const stdoutStr = stdout.toString();
console.log('failed command', `bun ${pathToFile} ${inputs.join(' ')}`);
console.log('stderr: ', stderr.toString());
console.log('stdout: ', stdoutStr);
throw new Error(stderr.toString());
}
return stdout.toString();
};
// we can generate combinations soon.
const generateProject = async ({
projectName = 'myTestProject',
flags
}: {
projectName?: string;
flags: Array<InputFlag>;
}) => {
return cli([projectName, ...flags]);
};
// Run tests for each package manager
// if we can find a good way to run tests in parallel we could go back to
// running in npm by default but right now its unbearably slow
// const packageManagers = process.env.ALL_PACKAGE_MANAGERS
// ? ([`npm`, `yarn`, `pnpm`, `bun`] as const)
// : (['npm'] as const);
const packageManagers = process.env.ALL_PACKAGE_MANAGERS
? ([`npm`, `yarn`, `pnpm`, `bun`] as const)
: (['bun'] as const);
test(`outputs version`, async () => {
const output = await cli([`--version`]);
expect(output).toContain(version);
});
test(`outputs help`, async () => {
const output = await cli([`--help`]);
expect(output).toContain(`Info`);
});
// we could later generate all combinations and have a "run everything" option that only runs very rarely
const popularCombinations = [
['--expo-router', '--nativewind'],
['--expo-router', '--stylesheet'],
['--expo-router', '--tabs', '--nativewind'],
['--expo-router', '--tabs', '--stylesheet'],
['--expo-router', '--drawer+tabs', '--nativewind'],
['--expo-router', '--drawer+tabs', '--stylesheet'],
// nativewindui selections
[
'--expo-router',
'--drawer+tabs',
'--nativewindui',
'--selected-components=date-picker,picker,text',
'--expo-router'
],
// nativewindui no selections
['--expo-router', '--drawer+tabs', '--nativewindui', '--expo-router'],
// no install is important for the website cli that generates a project zip file
['--nativewindui', '--no-install'],
// nativewindui blank
['--expo-router', '--drawer+tabs', '--nativewindui', '--blank', '--expo-router'],
// clerk expo-router tabs nativewind
['--expo-router', '--tabs', '--clerk', '--nativewind']
] as const;
const projectName = `myTestProject`;
const pathToProject = `../${projectName}`;
afterEach(() => {
Bun.$`rm -rf ./myTestProject`;
});
for (const packageManager of packageManagers) {
const packageManagerFlag = `--${packageManager}` as const;
for (const flags of popularCombinations) {
const finalFlags = [...flags, packageManagerFlag, '--overwrite' as const];
test(`generates a project with ${finalFlags.join(' ')}`, async () => {
const output = await generateProject({
projectName: projectName,
flags: finalFlags
});
expect(output).toContain(packageManager);
if (!finalFlags.includes('--no-install')) {
expect(output).toContain('Installing dependencies');
}
const pkgjson = await import(`${pathToProject}/package.json`);
const pkgJsonWithoutVersions = {
...pkgjson.default,
dependencies: Object.keys(pkgjson.default.dependencies).reduce((acc, key) => {
return {
...acc,
[key]: ''
};
}, {}),
devDependencies: Object.keys(pkgjson.default.devDependencies).reduce((acc, key) => {
return {
...acc,
[key]: ''
};
}, {})
};
expect(pkgJsonWithoutVersions).toMatchSnapshot(`${finalFlags.join(', ')}-package-json`);
const cesconfig = await import(`${pathToProject}/cesconfig.json`);
const cesconfigWithoutOS = {
...cesconfig.default,
cesVersion: undefined,
os: {},
packageManager: { ...cesconfig.default.packageManager, version: undefined }
};
expect(cesconfigWithoutOS).toMatchSnapshot(`${finalFlags.join(', ')}-ces-config-json`);
const fileList =
await Bun.$`find ./${projectName} -not -path "./${projectName}/node_modules*" -not -path "./${projectName}/.git*" | sort`.text();
expect(fileList).toMatchSnapshot(`${finalFlags.join(', ')}-file-list`);
// typecheck only works if we have packages installed
if (!finalFlags.includes('--no-install')) {
const { stderr, stdout, exitCode } = await Bun.$`cd ${projectName} && bun run tsc --noEmit`;
if (exitCode !== 0) {
console.warn('stdout', stdout.toString());
console.warn('stderr', stderr.toString());
}
expect(exitCode).toBe(0);
}
});
}
}
// i18next
test(`generates a default project with i18n`, async () => {
const output = await generateProject({
projectName: 'myTestProject',
flags: ['--default', `--i18next`, `--bun`, '--overwrite']
});
expect(output).toContain('--i18next');
});