-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathe2e.test.ts
More file actions
275 lines (239 loc) · 8.04 KB
/
e2e.test.ts
File metadata and controls
275 lines (239 loc) · 8.04 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import * as fs from "node:fs";
import path from "node:path";
const packageDir = path.resolve(import.meta.dir, "../..");
const cliPath = path.join(packageDir, "dist", "main.js");
const e2eDir = path.join(packageDir, "test-output-e2e");
const e2eTmpDir = path.join(e2eDir, ".tmp");
const baseProjectName = "e2e-basic";
const precompilesProjectName = "e2e-precompiles";
const e2eTmpDirEnv = `${e2eTmpDir}${path.sep}`;
const e2eSpawnEnv = {
...process.env,
TMPDIR: e2eTmpDirEnv,
BUN_TMPDIR: e2eTmpDirEnv,
};
async function runCli(
args: string[],
cwd: string,
): Promise<{ stdout: string; stderr: string; exitCode: number }> {
const proc = Bun.spawn(["node", cliPath, ...args], {
cwd,
stdout: "pipe",
stderr: "pipe",
env: { ...process.env, NO_COLOR: "1" },
});
const [stdout, stderr] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
]);
const exitCode = await proc.exited;
return { stdout, stderr, exitCode };
}
async function pathExists(targetPath: string): Promise<boolean> {
return fs.promises
.access(targetPath)
.then(() => true)
.catch(() => false);
}
async function ensureCliBuilt(): Promise<void> {
if (await pathExists(cliPath)) {
return;
}
const proc = Bun.spawn(["bun", "run", "build"], {
cwd: packageDir,
stdout: "pipe",
stderr: "pipe",
env: { ...process.env, NO_COLOR: "1" },
});
const [stdout, stderr] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
]);
await proc.exited;
if (proc.exitCode !== 0 || !(await pathExists(cliPath))) {
throw new Error(
`Failed to build create-sei CLI before e2e tests.\nstdout:\n${stdout}\nstderr:\n${stderr}`,
);
}
}
async function ensureProject(
projectName: string,
args: string[] = [],
): Promise<void> {
const projectDir = path.join(e2eDir, projectName);
if (await pathExists(projectDir)) {
return;
}
const { exitCode, stderr } = await runCli(
["app", "--name", projectName, ...args],
e2eDir,
);
if (exitCode !== 0) {
throw new Error(
`Failed to create fixture project '${projectName}'.\nstderr:\n${stderr}`,
);
}
}
async function installDeps(projectDir: string): Promise<void> {
const proc = Bun.spawn(["bun", "install"], {
cwd: projectDir,
stdout: "pipe",
stderr: "pipe",
env: e2eSpawnEnv,
});
const [stdout, stderr] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
]);
await proc.exited;
if (proc.exitCode !== 0) {
throw new Error(
`bun install failed in '${projectDir}'.\nstdout:\n${stdout}\nstderr:\n${stderr}`,
);
}
}
describe("create-sei CLI e2e", () => {
beforeAll(async () => {
await fs.promises.rm(e2eDir, { recursive: true, force: true });
await fs.promises.mkdir(e2eDir, { recursive: true });
await fs.promises.mkdir(e2eTmpDir, { recursive: true });
await ensureCliBuilt();
}, 120_000);
afterAll(async () => {
await fs.promises.rm(e2eDir, { recursive: true, force: true });
}, 120_000);
test("app --name creates a project directory", async () => {
const projectName = "e2e-create-check";
const { exitCode } = await runCli(["app", "--name", projectName], e2eDir);
expect(exitCode).toBe(0);
const projectDir = path.join(e2eDir, projectName);
const exists = await pathExists(projectDir);
expect(exists).toBe(true);
});
test("generated project has valid package.json", async () => {
await ensureProject(baseProjectName);
const pkgPath = path.join(e2eDir, baseProjectName, "package.json");
const raw = await fs.promises.readFile(pkgPath, "utf-8");
const pkg = JSON.parse(raw);
expect(pkg.scripts).toBeDefined();
expect(pkg.scripts.dev).toBe("next dev");
expect(pkg.scripts.build).toBe("next build");
expect(pkg.dependencies).toBeDefined();
expect(pkg.dependencies.next).toBeDefined();
expect(pkg.dependencies.react).toBeDefined();
expect(pkg.dependencies.viem).toBeDefined();
});
test("generated project has expected file structure", async () => {
await ensureProject(baseProjectName);
const projectDir = path.join(e2eDir, baseProjectName);
const expectedFiles = [
"package.json",
"tsconfig.json",
"next.config.mjs",
"src",
];
for (const file of expectedFiles) {
const exists = await pathExists(path.join(projectDir, file));
expect(exists).toBe(true);
}
});
test("generated project can install dependencies", async () => {
await ensureProject(baseProjectName);
const projectDir = path.join(e2eDir, baseProjectName);
await installDeps(projectDir);
// node_modules should exist
const nmExists = await pathExists(path.join(projectDir, "node_modules"));
expect(nmExists).toBe(true);
}, 60_000);
test("generated project can build successfully", async () => {
await ensureProject(baseProjectName);
const projectDir = path.join(e2eDir, baseProjectName);
await installDeps(projectDir);
const proc = Bun.spawn(["bun", "run", "build"], {
cwd: projectDir,
stdout: "pipe",
stderr: "pipe",
env: e2eSpawnEnv,
});
const [stdout, stderr] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
]);
await proc.exited;
const exitCode = proc.exitCode;
if (exitCode !== 0) {
console.error("Build stdout:", stdout);
console.error("Build stderr:", stderr);
}
expect(exitCode).toBe(0);
}, 120_000);
test("app --name --extension precompiles creates project with extension", async () => {
const projectName = "e2e-precompiles-create-check";
const { exitCode, stdout } = await runCli(
["app", "--name", projectName, "--extension", "precompiles"],
e2eDir,
);
expect(exitCode).toBe(0);
expect(stdout).toContain("Applied extension: precompiles");
// Extension should have overwritten package.json
const pkgPath = path.join(e2eDir, projectName, "package.json");
const pkg = JSON.parse(await fs.promises.readFile(pkgPath, "utf-8"));
expect(pkg.name).toBe("template-next-create-sei-app-precompiles");
});
test("extension project can install dependencies", async () => {
await ensureProject(precompilesProjectName, ["--extension", "precompiles"]);
const projectDir = path.join(e2eDir, precompilesProjectName);
await installDeps(projectDir);
}, 60_000);
test("extension project can build successfully", async () => {
await ensureProject(precompilesProjectName, ["--extension", "precompiles"]);
const projectDir = path.join(e2eDir, precompilesProjectName);
await installDeps(projectDir);
const proc = Bun.spawn(["bun", "run", "build"], {
cwd: projectDir,
stdout: "pipe",
stderr: "pipe",
env: e2eSpawnEnv,
});
const [stdout, stderr] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
]);
await proc.exited;
if (proc.exitCode !== 0) {
console.error("Build stdout:", stdout);
console.error("Build stderr:", stderr);
}
expect(proc.exitCode).toBe(0);
}, 120_000);
test("list-extensions command outputs available extensions", async () => {
const { exitCode, stdout } = await runCli(["list-extensions"], e2eDir);
expect(exitCode).toBe(0);
expect(stdout).toContain("Available extensions:");
expect(stdout).toContain("precompiles");
});
test("app with invalid name does not create directory", async () => {
const { exitCode, stdout } = await runCli(
["app", "--name", "INVALID NAME!"],
e2eDir,
);
expect(exitCode).toBe(0);
expect(stdout).toContain("Invalid package name");
const exists = await pathExists(path.join(e2eDir, "INVALID NAME!"));
expect(exists).toBe(false);
});
test("app with nonexistent extension falls back to base template", async () => {
const { exitCode, stdout } = await runCli(
["app", "--name", "e2e-fallback", "--extension", "does-not-exist"],
e2eDir,
);
expect(exitCode).toBe(0);
expect(stdout).toContain("Warning");
expect(stdout).toContain("does-not-exist");
// Should still have created the project from base template
const pkgPath = path.join(e2eDir, "e2e-fallback", "package.json");
const pkg = JSON.parse(await fs.promises.readFile(pkgPath, "utf-8"));
expect(pkg.scripts.dev).toBe("next dev");
});
});