k6-cucumber-steps 1.1.10 → 1.1.12

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.
@@ -18,8 +18,14 @@ module.exports = function buildK6Script(config) {
18
18
  return url.startsWith("/") ? `${BASE_URL}${url}` : url;
19
19
  };
20
20
 
21
- const stringifiedHeaders = JSON.stringify(headers, null, 2);
22
- const stringifiedBody = JSON.stringify(body, null, 2);
21
+ // Default headers/body/options for safety
22
+ const safeHeaders = headers && Object.keys(headers).length ? headers : {};
23
+ const safeBody = body !== undefined ? body : null;
24
+ const safeOptions = options && Object.keys(options).length ? options : {};
25
+
26
+ // Stringify for script
27
+ const stringifiedHeaders = JSON.stringify(safeHeaders, null, 2);
28
+ const stringifiedBody = JSON.stringify(safeBody, null, 2);
23
29
 
24
30
  return `
25
31
  import http from 'k6/http';
@@ -27,7 +33,7 @@ import { check } from 'k6';
27
33
  import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";
28
34
  import { textSummary } from "https://jslib.k6.io/k6-summary/0.0.1/index.js";
29
35
 
30
- export const options = ${JSON.stringify(options, null, 2)};
36
+ export const options = ${JSON.stringify(safeOptions, null, 2)};
31
37
 
32
38
  export default function () {
33
39
  const headers = ${stringifiedHeaders};
@@ -48,10 +48,9 @@ const delay = (ms) => new Promise((res) => setTimeout(res, ms));
48
48
  * Runs the k6 script with custom branding.
49
49
  * @param {string} scriptPath - Path to the k6 script file.
50
50
  * @param {boolean} [overwrite=false] - Whether to overwrite the report file.
51
- * @returns {Promise<string>} - Standard output from k6 execution.
51
+ * @returns {Promise<{stdout: string, stderr: string, code: number}>} - k6 execution result.
52
52
  */
53
53
  const runK6Script = async (scriptPath, overwrite = false) => {
54
- // ANSI escape codes for colors
55
54
  const chalkGreen = "\x1b[38;2;0;255;0m"; // Green
56
55
  const chalkYellow = "\x1b[38;2;255;255;0m"; // Yellow
57
56
  const resetColor = "\x1b[0m";
@@ -59,7 +58,7 @@ const runK6Script = async (scriptPath, overwrite = false) => {
59
58
  // Custom logo with version information
60
59
  const customLogo = `${chalkGreen} with @qaPaschalE's ${chalkYellow}k6-cucumber-steps v${packageJson.version}${resetColor}`;
61
60
 
62
- return new Promise(async (resolve, reject) => {
61
+ return new Promise((resolve, reject) => {
63
62
  exec(`k6 run "${scriptPath}"`, async (error, stdout, stderr) => {
64
63
  // Split the k6 logo lines
65
64
  const logoLines = stdout.split("\n");
@@ -75,27 +74,17 @@ const runK6Script = async (scriptPath, overwrite = false) => {
75
74
  modifiedStdout += "\n";
76
75
  }
77
76
 
78
- // Handle errors and cleanup
79
- if (error) {
80
- console.error("k6 error:", error);
81
- console.error("k6 stdout:", modifiedStdout);
82
- await delay(3000); // Wait for 3 seconds
83
- console.error("k6 stderr:", stderr);
84
- reject(new Error(`k6 test execution failed: ${error.message}`));
85
- } else if (stderr) {
86
- console.log("k6 stdout:", modifiedStdout);
87
- await delay(3000); // Wait for 3 seconds
88
- resolve(stdout);
89
- } else {
90
- console.log("k6 stdout:", modifiedStdout);
91
- await delay(3000); // Wait for 3 seconds
92
- resolve(stdout);
93
- }
94
-
95
77
  // Clean up the temporary script file
96
78
  fs.unlink(scriptPath).catch((err) =>
97
79
  console.error("Error deleting temporary k6 script:", err)
98
80
  );
81
+
82
+ // Always resolve with all outputs and code
83
+ resolve({
84
+ stdout: modifiedStdout,
85
+ stderr,
86
+ code: error ? error.code ?? 1 : 0,
87
+ });
99
88
  });
100
89
  });
101
90
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "k6-cucumber-steps",
3
- "version": "1.1.10",
3
+ "version": "1.1.12",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -325,19 +325,31 @@ Then(
325
325
  );
326
326
  }
327
327
  try {
328
+ // Build the k6 script content
328
329
  const scriptContent = buildK6Script(this.config);
330
+
331
+ // Generate the k6 script file
329
332
  const scriptPath = await generateK6Script(
330
333
  scriptContent,
331
334
  "load",
332
335
  process.env.K6_CUCUMBER_OVERWRITE === "true"
333
336
  );
334
- const stdout = await runK6Script(
337
+
338
+ // Run the k6 script and capture output
339
+ const { stdout, stderr, code } = await runK6Script(
335
340
  scriptPath,
336
341
  process.env.K6_CUCUMBER_OVERWRITE === "true"
337
342
  );
338
- console.log(stdout);
343
+
344
+ if (stdout) console.log(stdout);
345
+ if (stderr) console.error(stderr);
346
+
347
+ if (code !== 0) {
348
+ throw new Error(`k6 exited with code ${code}`);
349
+ }
339
350
  } catch (error) {
340
- console.error("k6 execution failed:", error.message);
351
+ // Print the full error for debugging
352
+ console.error("k6 execution failed:", error.stack || error);
341
353
  throw new Error("k6 test execution failed");
342
354
  }
343
355
  }