k6-cucumber-steps 1.2.16 → 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.
@@ -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.16",
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, {