Skip to content

Commit 9f739b9

Browse files
refactor: cleaned e2e runner (#215)
1 parent c06ec4f commit 9f739b9

File tree

1 file changed

+52
-59
lines changed

1 file changed

+52
-59
lines changed

e2e/utils.ts

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,6 @@ export type CreateLlamaResult = {
1818
appProcess: ChildProcess;
1919
};
2020

21-
// eslint-disable-next-line max-params
22-
export async function checkAppHasStarted(
23-
frontend: boolean,
24-
framework: TemplateFramework,
25-
port: number,
26-
externalPort: number,
27-
timeout: number,
28-
) {
29-
if (frontend) {
30-
await Promise.all([
31-
waitPort({
32-
host: "localhost",
33-
port: port,
34-
timeout,
35-
}),
36-
waitPort({
37-
host: "localhost",
38-
port: externalPort,
39-
timeout,
40-
}),
41-
]).catch((err) => {
42-
console.error(err);
43-
throw err;
44-
});
45-
} else {
46-
let wPort: number;
47-
if (framework === "nextjs") {
48-
wPort = port;
49-
} else {
50-
wPort = externalPort;
51-
}
52-
await waitPort({
53-
host: "localhost",
54-
port: wPort,
55-
timeout,
56-
}).catch((err) => {
57-
console.error(err);
58-
throw err;
59-
});
60-
}
61-
}
62-
6321
// eslint-disable-next-line max-params
6422
export async function runCreateLlama(
6523
cwd: string,
@@ -142,25 +100,10 @@ export async function runCreateLlama(
142100
templateFramework,
143101
port,
144102
externalPort,
145-
1000 * 60 * 5,
146103
);
147104
} else {
148-
// wait create-llama to exit
149-
// we don't test install dependencies for now, so just set timeout for 10 seconds
150-
await new Promise((resolve, reject) => {
151-
const timeout = setTimeout(() => {
152-
reject(new Error("create-llama timeout error"));
153-
}, 1000 * 10);
154-
appProcess.on("exit", (code) => {
155-
if (code !== 0 && code !== null) {
156-
clearTimeout(timeout);
157-
reject(new Error("create-llama command was failed!"));
158-
} else {
159-
clearTimeout(timeout);
160-
resolve(undefined);
161-
}
162-
});
163-
});
105+
// wait 10 seconds for create-llama to exit
106+
await waitForProcess(appProcess, 1000 * 10);
164107
}
165108

166109
return {
@@ -174,3 +117,53 @@ export async function createTestDir() {
174117
await mkdir(cwd, { recursive: true });
175118
return cwd;
176119
}
120+
121+
// eslint-disable-next-line max-params
122+
async function checkAppHasStarted(
123+
frontend: boolean,
124+
framework: TemplateFramework,
125+
port: number,
126+
externalPort: number,
127+
) {
128+
const portsToWait = frontend
129+
? [port, externalPort]
130+
: [framework === "nextjs" ? port : externalPort];
131+
await waitPorts(portsToWait);
132+
}
133+
134+
async function waitPorts(ports: number[]): Promise<void> {
135+
const waitForPort = async (port: number): Promise<void> => {
136+
await waitPort({
137+
host: "localhost",
138+
port: port,
139+
// wait max. 5 mins for start up of app
140+
timeout: 1000 * 60 * 5,
141+
});
142+
};
143+
try {
144+
await Promise.all(ports.map(waitForPort));
145+
} catch (err) {
146+
console.error(err);
147+
throw err;
148+
}
149+
}
150+
151+
async function waitForProcess(
152+
process: ChildProcess,
153+
timeoutMs: number,
154+
): Promise<void> {
155+
return new Promise((resolve, reject) => {
156+
const timeout = setTimeout(() => {
157+
reject(new Error("Process timeout error"));
158+
}, timeoutMs);
159+
160+
process.on("exit", (code) => {
161+
clearTimeout(timeout);
162+
if (code !== 0 && code !== null) {
163+
reject(new Error("Process exited with non-zero code"));
164+
} else {
165+
resolve();
166+
}
167+
});
168+
});
169+
}

0 commit comments

Comments
 (0)