@brillout/test-e2e 0.1.19 → 0.1.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/setup.js +59 -54
  2. package/package.json +1 -1
package/dist/setup.js CHANGED
@@ -83,15 +83,7 @@ additionalTimeout = 0, serverIsReadyMessage, serverIsReadyDelay = 1000, inspect
83
83
  page_1.page.off('pageerror', onPageError);
84
84
  // `runProcess` is `undefined` if `start()` failed.
85
85
  if (runProcess) {
86
- try {
87
- await runProcess.terminate('SIGINT');
88
- }
89
- catch (err) {
90
- Logs_1.Logs.add({
91
- logSource: 'run()',
92
- logText: String(err),
93
- });
94
- }
86
+ await runProcess.terminate('SIGINT');
95
87
  }
96
88
  };
97
89
  return;
@@ -141,6 +133,8 @@ async function start() {
141
133
  let rejectServerStart;
142
134
  const promise = new Promise((_resolve, _reject) => {
143
135
  resolveServerStart = () => {
136
+ (0, utils_2.assert)(!processHasExited());
137
+ (0, utils_2.assert)(getRunInfo().cmd === cmd);
144
138
  Logs_1.Logs.add({
145
139
  logSource: 'run()',
146
140
  logText: 'server is ready',
@@ -151,45 +145,40 @@ async function start() {
151
145
  _resolve(runProcess);
152
146
  };
153
147
  rejectServerStart = async (err) => {
148
+ (0, utils_2.assert)(processHasExited());
154
149
  (0, utils_2.assert)(getRunInfo().cmd === cmd);
155
150
  done(true);
156
- Logs_1.Logs.add({
157
- logSource: 'run() failure',
158
- logText: String(err),
159
- });
160
151
  clearTimeout(serverStartTimeout);
161
- try {
162
- await terminate('SIGKILL');
163
- }
164
- catch (err) {
165
- Logs_1.Logs.add({
166
- logSource: 'run()',
167
- logText: String(err),
168
- });
169
- }
170
152
  _reject(err);
171
153
  };
172
154
  });
173
155
  const timeoutTotal = TIMEOUT_NPM_SCRIPT + additionalTimeout;
174
156
  let hasTimedout = false;
175
- const serverStartTimeout = setTimeout(() => {
157
+ const serverStartTimeout = setTimeout(async () => {
158
+ hasTimedout = true;
176
159
  let errMsg = '';
177
160
  errMsg += `Server still didn't start after ${(0, utils_1.humanizeTime)(timeoutTotal)} of running the npm script \`${cmd}\`.`;
178
161
  if (serverIsReadyMessage) {
179
162
  errMsg += ` (The stdout of the npm script did not include: "${serverIsReadyMessage}".)`;
180
163
  }
181
- hasTimedout = true;
164
+ Logs_1.Logs.add({
165
+ logSource: 'run() failure',
166
+ logText: errMsg,
167
+ });
168
+ await terminate();
182
169
  rejectServerStart(new Error(errMsg));
183
170
  }, timeoutTotal);
184
171
  // Kill any process that listens to port `3000`
185
172
  if (!process.env.CI && isLinux()) {
186
173
  await (0, utils_1.runCommand)('fuser -k 3000/tcp', { swallowError: true, timeout: 10 * 1000 });
187
174
  }
188
- const { terminate } = execRunScript({
189
- onError(err) {
175
+ const { terminate, processHasExited } = execRunScript({
176
+ async onFailure(err) {
177
+ (0, utils_2.assert)(processHasExited());
190
178
  rejectServerStart(err);
191
179
  },
192
180
  onExit() {
181
+ (0, utils_2.assert)(processHasExited());
193
182
  const exitIsPossible = hasSuccessfullyStarted === true || hasTimedout;
194
183
  return exitIsPossible;
195
184
  },
@@ -211,11 +200,6 @@ async function start() {
211
200
  done();
212
201
  }
213
202
  },
214
- onStderr(data) {
215
- if (data.includes('EADDRINUSE')) {
216
- rejectServerStart(new Error('Port conflict? Port already in use EADDRINUSE.'));
217
- }
218
- },
219
203
  });
220
204
  return promise;
221
205
  }
@@ -276,7 +260,7 @@ function stopProcess({ proc, cwd, cmd, signal, }) {
276
260
  }
277
261
  return promise;
278
262
  }
279
- function execRunScript({ onStdout, onStderr, onError, onExit, }) {
263
+ function execRunScript({ onStdout, onFailure, onExit, }) {
280
264
  const { cwd, cmd } = getRunInfo();
281
265
  let [command, ...args] = cmd.split(' ');
282
266
  let detached = true;
@@ -291,10 +275,20 @@ function execRunScript({ onStdout, onStderr, onError, onExit, }) {
291
275
  logText: `Spawn command \`${cmd}\``,
292
276
  });
293
277
  const proc = (0, child_process_1.spawn)(command, args, { cwd, detached });
278
+ let procExited = false;
279
+ const exitAndFail = async (err) => {
280
+ Logs_1.Logs.add({
281
+ logText: err.message,
282
+ logSource: 'run() failure',
283
+ });
284
+ await terminate();
285
+ onFailure(err);
286
+ };
294
287
  proc.stdin.on('data', async (data) => {
295
- onError(new Error(`Command is \`${cmd}\` (${cwd}) is invoking \`stdin\`: ${data}.`));
288
+ await exitAndFail(new Error(`Command is \`${cmd}\` (${cwd}) is invoking \`stdin\`: ${data}.`));
296
289
  });
297
290
  proc.stdout.on('data', (data) => {
291
+ (0, utils_2.assert)(!procExited);
298
292
  (0, utils_2.assert)(getRunInfo().cmd === cmd);
299
293
  data = data.toString();
300
294
  Logs_1.Logs.add({
@@ -303,16 +297,20 @@ function execRunScript({ onStdout, onStderr, onError, onExit, }) {
303
297
  });
304
298
  onStdout?.(data);
305
299
  });
306
- proc.stderr.on('data', (data) => {
300
+ proc.stderr.on('data', async (data) => {
301
+ (0, utils_2.assert)(!procExited);
307
302
  (0, utils_2.assert)(getRunInfo().cmd === cmd);
308
303
  data = data.toString();
309
304
  Logs_1.Logs.add({
310
305
  logSource: 'stderr',
311
306
  logText: data,
312
307
  });
313
- onStderr?.(data);
308
+ if (data.includes('EADDRINUSE')) {
309
+ await exitAndFail(new Error('Port conflict? Port already in use EADDRINUSE.'));
310
+ }
314
311
  });
315
312
  proc.on('exit', (code) => {
313
+ procExited = true;
316
314
  (0, utils_2.assert)(getRunInfo().cmd === cmd);
317
315
  const exitIsPossible = onExit();
318
316
  const isSuccessCode = [0, null].includes(code) || (isWindows() && code === 1);
@@ -323,16 +321,7 @@ function execRunScript({ onStdout, onStderr, onError, onExit, }) {
323
321
  logText: errMsg,
324
322
  logSource: 'run() failure',
325
323
  });
326
- onError(new Error(errMsg));
327
- /*
328
- try {
329
- await terminate('SIGKILL')
330
- } catch (err: unknown) {
331
- if( !err.code === 'ESRCH' ) {
332
- onError(err as Error)
333
- }
334
- }
335
- */
324
+ onFailure(new Error(errMsg));
336
325
  }
337
326
  else {
338
327
  Logs_1.Logs.add({
@@ -341,8 +330,11 @@ function execRunScript({ onStdout, onStderr, onError, onExit, }) {
341
330
  });
342
331
  }
343
332
  });
344
- return { terminate };
345
- async function terminate(signal) {
333
+ return { terminate, processHasExited };
334
+ function processHasExited() {
335
+ return procExited;
336
+ }
337
+ async function terminate(signal = 'SIGKILL') {
346
338
  let resolve;
347
339
  let reject;
348
340
  const promise = new Promise((_resolve, _reject) => {
@@ -350,15 +342,28 @@ function execRunScript({ onStdout, onStderr, onError, onExit, }) {
350
342
  reject = _reject;
351
343
  });
352
344
  const timeout = setTimeout(() => {
353
- reject(new Error('Process termination timeout. Cmd: ' + cmd));
345
+ const errMsg = 'Process termination timeout. Cmd: ' + cmd;
346
+ Logs_1.Logs.add({
347
+ logSource: 'run() failure',
348
+ logText: errMsg,
349
+ });
350
+ reject(new Error(errMsg));
354
351
  }, TIMEOUT_PROCESS_TERMINATION);
355
352
  (0, utils_2.assert)(proc);
356
- await stopProcess({
357
- proc,
358
- cwd,
359
- cmd,
360
- signal,
361
- });
353
+ try {
354
+ await stopProcess({
355
+ proc,
356
+ cwd,
357
+ cmd,
358
+ signal,
359
+ });
360
+ }
361
+ catch (err) {
362
+ Logs_1.Logs.add({
363
+ logSource: 'run() failure',
364
+ logText: String(err),
365
+ });
366
+ }
362
367
  clearTimeout(timeout);
363
368
  resolve();
364
369
  return promise;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brillout/test-e2e",
3
- "version": "0.1.19",
3
+ "version": "0.1.20",
4
4
  "bin": "./bin.js",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {