k6-cucumber-steps 1.2.15 → 1.2.17

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.
@@ -82,7 +82,7 @@ program
82
82
  }
83
83
 
84
84
  // Determine project root (where your main package.json is)
85
- const projectRoot = path.resolve(__dirname, "..");
85
+ const projectRoot = process.cwd();
86
86
 
87
87
  // Determine payload directory from CLI or config, always relative to project root
88
88
  let payloadDirRaw =
@@ -90,9 +90,8 @@ program
90
90
  (cucumberConfig.worldParameters &&
91
91
  cucumberConfig.worldParameters.payloadPath) ||
92
92
  "payloads";
93
- const payloadDir = path.isAbsolute(payloadDirRaw)
94
- ? payloadDirRaw
95
- : path.join(projectRoot, payloadDirRaw);
93
+ const payloadDir = path.resolve(projectRoot, payloadDirRaw);
94
+ console.log("📦 Resolved payload path:", payloadDir);
96
95
 
97
96
  // Add --world-parameters to CLI args
98
97
  const worldParams = {
@@ -109,11 +108,7 @@ program
109
108
  cliParts.push("--k6Config", argv.k6Config);
110
109
  }
111
110
 
112
- const finalCommand = [
113
- "npx",
114
- "cucumber-js",
115
- ...cliParts
116
- ].join(" ");
111
+ const finalCommand = ["npx", "cucumber-js", ...cliParts].join(" ");
117
112
 
118
113
  console.log("▶️ Final arguments passed to cucumber-js:", finalCommand);
119
114
 
@@ -121,13 +116,11 @@ program
121
116
  const extraEnv = {
122
117
  // Priority: cucumberConfig > CLI
123
118
  SAVE_K6_SCRIPT:
124
- cucumberConfig.saveK6Script === true ||
125
- argv.saveK6Script === true
119
+ cucumberConfig.saveK6Script === true || argv.saveK6Script === true
126
120
  ? "true"
127
121
  : "false",
128
122
  K6_CUCUMBER_OVERWRITE:
129
- cucumberConfig.overwrite === true ||
130
- argv.overwrite === true
123
+ cucumberConfig.overwrite === true || argv.overwrite === true
131
124
  ? "true"
132
125
  : "false",
133
126
  CLEAN_REPORTS:
@@ -137,17 +130,14 @@ program
137
130
  ? "true"
138
131
  : "false",
139
132
  K6_CUCUMBER_REPORTER:
140
- cucumberConfig.reporter === true ||
141
- argv.reporter === true
133
+ cucumberConfig.reporter === true || argv.reporter === true
142
134
  ? "true"
143
135
  : "false",
144
136
  };
145
137
 
146
138
  // Clean reports directory if requested
147
139
  const shouldCleanReports =
148
- argv.cleanReports ||
149
- argv.clean ||
150
- cucumberConfig.cleanReports;
140
+ argv.cleanReports || argv.clean || cucumberConfig.cleanReports;
151
141
 
152
142
  if (shouldCleanReports) {
153
143
  const reportsDir = path.join(projectRoot, "reports");
@@ -158,14 +148,10 @@ program
158
148
  }
159
149
 
160
150
  // Now spawn the process
161
- const cucumberProcess = spawn(
162
- "npx",
163
- ["cucumber-js", ...cliParts],
164
- {
165
- stdio: "inherit",
166
- env: { ...process.env, ...extraEnv },
167
- }
168
- );
151
+ const cucumberProcess = spawn("npx", ["cucumber-js", ...cliParts], {
152
+ stdio: "inherit",
153
+ env: { ...process.env, ...extraEnv },
154
+ });
169
155
 
170
156
  cucumberProcess.on("close", async (code) => {
171
157
  if (code === 0) {
@@ -0,0 +1,25 @@
1
+ import path from "path";
2
+ import fs from "fs";
3
+
4
+ /**
5
+ * Resolves the full path to a payload file based on a root directory (e.g., from config)
6
+ * and filename. Throws a clear error if file does not exist.
7
+ *
8
+ * @param {string} fileName - The file name (e.g., login.json)
9
+ * @param {string} payloadDir - Directory path for payloads (absolute or relative)
10
+ * @returns {string} - Full path to the resolved file
11
+ */
12
+ export default function resolvePayloadPath(fileName, payloadDir = "payloads") {
13
+ const projectRoot = path.resolve(process.cwd());
14
+ const baseDir = path.isAbsolute(payloadDir)
15
+ ? payloadDir
16
+ : path.join(projectRoot, payloadDir);
17
+
18
+ const fullPath = path.join(baseDir, fileName);
19
+
20
+ if (!fs.existsSync(fullPath)) {
21
+ throw new Error(`❌ Payload file not found: "${fullPath}"`);
22
+ }
23
+
24
+ return fullPath;
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "k6-cucumber-steps",
3
- "version": "1.2.15",
3
+ "version": "1.2.17",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -5,7 +5,7 @@ import fs from "fs";
5
5
  import path from "path";
6
6
  import crypto from "crypto";
7
7
  import * as dotenv from "dotenv";
8
-
8
+ import resolvePayloadPath from "../lib/helpers/resolvePayloadPath.js";
9
9
  import resolveBody from "../lib/helpers/resolveBody.js";
10
10
  import buildK6Script from "../lib/helpers/buildK6Script.js";
11
11
  import generateHeaders from "../lib/helpers/generateHeaders.js";
@@ -293,19 +293,58 @@ export async function When_I_set_method_body_for_endpoint(
293
293
  docString
294
294
  ) {
295
295
  /** @type {CustomWorld} */ (this);
296
- this.config.method = method.toUpperCase();
297
- this.config.endpoint = endpoint;
298
- this.config.body = resolveBody(docString, process.env);
296
+ const methodUpper = method.toUpperCase();
297
+ const payloadDir = this.parameters?.payloadPath || "payloads";
298
+ const doc = docString.trim();
299
+
300
+ let body = "";
301
+
302
+ // Try resolving from file if it looks like a filename
303
+ const isLikelyFile = /^[\w\-.]+(\.json)?$/.test(doc);
304
+ const fileName = doc.endsWith(".json") ? doc : `${doc}.json`;
305
+
306
+ try {
307
+ if (isLikelyFile) {
308
+ const filePath = resolvePayloadPath(fileName, payloadDir);
309
+ const fileContent = fs.readFileSync(filePath, "utf-8");
310
+ body = resolveBody(fileContent, {
311
+ ...process.env,
312
+ ...(this.aliases || {}),
313
+ });
314
+ this.log?.(`📁 Loaded payload from file: "${fileName}"`);
315
+ } else {
316
+ throw new Error("Skipping file load; using raw input as body.");
317
+ }
318
+ } catch (e) {
319
+ // If file doesn't exist or error occurs, treat as inline string
320
+ body = resolveBody(doc, {
321
+ ...process.env,
322
+ ...(this.aliases || {}),
323
+ });
324
+ this.log?.("📝 Using docString directly as payload body.");
325
+ }
326
+
327
+ this.config = {
328
+ ...(this.config || {}),
329
+ method: methodUpper,
330
+ endpoint,
331
+ body,
332
+ headers: this.config?.headers || {},
333
+ };
334
+
335
+ this.lastRequest = {
336
+ method: methodUpper,
337
+ endpoint,
338
+ body,
339
+ };
340
+
299
341
  this.log?.(
300
- `⚙️ Body set for ${this.config.method} to "${
301
- this.config.endpoint
302
- }". Body preview: ${this.config.body.slice(0, 100)}...`
342
+ `⚙️ Body set for ${methodUpper} to "${endpoint}". Body preview: ${body.slice(
343
+ 0,
344
+ 100
345
+ )}...`
303
346
  );
304
347
  }
305
- When(
306
- /^I set the following (\w+) body is used for "([^"]+)"$/,
307
- When_I_set_method_body_for_endpoint
308
- );
309
348
 
310
349
  /**
311
350
  * Loads a JSON payload from a file to be used as the request body for a specific
@@ -346,15 +385,8 @@ export async function When_I_use_JSON_payload_from_file_for_method_to_endpoint(
346
385
  );
347
386
  }
348
387
 
349
- const projectRoot = path.resolve(__dirname, "..", "..");
350
388
  const payloadDir = this.parameters?.payloadPath || "payloads";
351
- const payloadPath = path.isAbsolute(payloadDir)
352
- ? path.join(payloadDir, fileName)
353
- : path.join(projectRoot, payloadDir, fileName);
354
-
355
- if (!fs.existsSync(payloadPath)) {
356
- throw new Error(`Payload file not found: "${payloadPath}"`);
357
- }
389
+ const payloadPath = resolvePayloadPath(fileName, payloadDir);
358
390
 
359
391
  const rawTemplate = fs.readFileSync(payloadPath, "utf-8");
360
392
  const resolved = resolveBody(rawTemplate, {
@@ -504,14 +536,7 @@ export async function When_I_login_via_POST_with_payload_from_file(
504
536
  ) {
505
537
  /** @type {CustomWorld} */ (this);
506
538
  const payloadDir = this.parameters?.payloadPath || "payloads";
507
- const projectRoot = path.resolve(__dirname, "..", "..");
508
- const payloadPath = path.isAbsolute(payloadDir)
509
- ? path.join(payloadDir, fileName)
510
- : path.join(projectRoot, payloadDir, fileName);
511
-
512
- if (!fs.existsSync(payloadPath)) {
513
- throw new Error(`Payload file not found: "${payloadPath}"`);
514
- }
539
+ const payloadPath = resolvePayloadPath(fileName, payloadDir);
515
540
 
516
541
  const rawTemplate = fs.readFileSync(payloadPath, "utf-8");
517
542
  const resolved = resolveBody(rawTemplate, {