@testomatio/reporter 2.9.2 → 2.9.3-beta-vitestmeta-2

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.
@@ -393,7 +393,10 @@ function normalizeVitestTest(test) {
393
393
  file,
394
394
  suiteTitle,
395
395
  logs: '',
396
- meta: typeof test.meta === 'function' ? test.meta() : {},
396
+ meta: {
397
+ ...(test.task?.meta || {}),
398
+ ...(typeof test.meta === 'function' ? test.meta() : test.meta || {}),
399
+ },
397
400
  };
398
401
  }
399
402
  return {
@@ -3,10 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const module_1 = require("module");
6
7
  const playwright_js_1 = require("./adapter/utils/playwright.js");
7
8
  const helpers_js_1 = require("./helpers.js");
8
9
  const index_js_1 = require("./services/index.js");
9
10
  const picocolors_1 = __importDefault(require("picocolors"));
11
+ const requireModule = (0, module_1.createRequire)(`${process.cwd()}/package.json`);
10
12
  /**
11
13
  * Stores path to file as artifact and uploads it to the S3 storage
12
14
  * @param {string | {path: string, type: string, name: string}} data - path to file or object with path, type and name
@@ -47,6 +49,36 @@ function addStep(message, logs) {
47
49
  else
48
50
  index_js_1.services.logger.step(message, logs);
49
51
  }
52
+ /**
53
+ * Returns the currently running Vitest test task, so `meta()` (and similar
54
+ * functions) can attach data to it. The task `.meta` object is serialized by
55
+ * Vitest back to the reporter (works across all pools: threads, forks, vmThreads).
56
+ *
57
+ * Resolution order matters:
58
+ * 1. `globalThis.__vitest_worker__.current` — set by Vitest in every worker and
59
+ * independent of module resolution, so it works even when the reporter is the
60
+ * externalized package and `@vitest/runner` is deduped to a different instance
61
+ * (monorepos, pnpm, hoisting). This is also populated inside hooks (afterEach),
62
+ * pointing at the test that the hook belongs to.
63
+ * 2. `@vitest/runner` `getCurrentTest()` — fallback for setups/versions where the
64
+ * worker global is not exposed.
65
+ *
66
+ * @returns {any | null} Vitest test task or null when no test is active
67
+ */
68
+ function getCurrentVitestTest() {
69
+ // @ts-ignore - injected by Vitest into the worker global scope
70
+ const worker = globalThis.__vitest_worker__;
71
+ const current = worker?.current;
72
+ if (current && (current.type === 'test' || current.type === undefined))
73
+ return current;
74
+ try {
75
+ const test = requireModule('@vitest/runner').getCurrentTest?.();
76
+ if (test)
77
+ return test;
78
+ }
79
+ catch { }
80
+ return null;
81
+ }
50
82
  /**
51
83
  * Add key-value pair(s) to the test report
52
84
  * @param {{[key: string]: string} | string} keyValue - object { key: value } (multiple props allowed) OR key (string)
@@ -68,6 +100,13 @@ function setKeyValue(keyValue, value = undefined) {
68
100
  console.log(`${playwright_js_1.playwrightLogsMarkers.meta} ${JSON.stringify(keyValue)}`);
69
101
  return;
70
102
  }
103
+ if (process.env.VITEST || process.env.VITEST_WORKER_ID) {
104
+ const vitestTest = getCurrentVitestTest();
105
+ if (vitestTest) {
106
+ Object.assign((vitestTest.meta ||= {}), keyValue);
107
+ return;
108
+ }
109
+ }
71
110
  // in this case keyValue is expected to be an object
72
111
  index_js_1.services.keyValues.put(keyValue);
73
112
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testomatio/reporter",
3
- "version": "2.9.2",
3
+ "version": "2.9.3-beta-vitestmeta-2",
4
4
  "description": "Testomatio Reporter Client",
5
5
  "engines": {
6
6
  "node": ">=18"
@@ -396,7 +396,10 @@ function normalizeVitestTest(test) {
396
396
  file,
397
397
  suiteTitle,
398
398
  logs: '',
399
- meta: typeof test.meta === 'function' ? test.meta() : {},
399
+ meta: {
400
+ ...(test.task?.meta || {}),
401
+ ...(typeof test.meta === 'function' ? test.meta() : test.meta || {}),
402
+ },
400
403
  };
401
404
  }
402
405
 
@@ -1,8 +1,11 @@
1
+ import { createRequire } from 'module';
1
2
  import { playwrightLogsMarkers } from './adapter/utils/playwright.js';
2
3
  import { isPlaywright } from './helpers.js';
3
4
  import { services } from './services/index.js';
4
5
  import pc from 'picocolors';
5
6
 
7
+ const requireModule = createRequire(`${process.cwd()}/package.json`);
8
+
6
9
  /**
7
10
  * Stores path to file as artifact and uploads it to the S3 storage
8
11
  * @param {string | {path: string, type: string, name: string}} data - path to file or object with path, type and name
@@ -44,6 +47,36 @@ function addStep(message, logs) {
44
47
  else services.logger.step(message, logs);
45
48
  }
46
49
 
50
+ /**
51
+ * Returns the currently running Vitest test task, so `meta()` (and similar
52
+ * functions) can attach data to it. The task `.meta` object is serialized by
53
+ * Vitest back to the reporter (works across all pools: threads, forks, vmThreads).
54
+ *
55
+ * Resolution order matters:
56
+ * 1. `globalThis.__vitest_worker__.current` — set by Vitest in every worker and
57
+ * independent of module resolution, so it works even when the reporter is the
58
+ * externalized package and `@vitest/runner` is deduped to a different instance
59
+ * (monorepos, pnpm, hoisting). This is also populated inside hooks (afterEach),
60
+ * pointing at the test that the hook belongs to.
61
+ * 2. `@vitest/runner` `getCurrentTest()` — fallback for setups/versions where the
62
+ * worker global is not exposed.
63
+ *
64
+ * @returns {any | null} Vitest test task or null when no test is active
65
+ */
66
+ function getCurrentVitestTest() {
67
+ // @ts-ignore - injected by Vitest into the worker global scope
68
+ const worker = globalThis.__vitest_worker__;
69
+ const current = worker?.current;
70
+ if (current && (current.type === 'test' || current.type === undefined)) return current;
71
+
72
+ try {
73
+ const test = requireModule('@vitest/runner').getCurrentTest?.();
74
+ if (test) return test;
75
+ } catch {}
76
+
77
+ return null;
78
+ }
79
+
47
80
  /**
48
81
  * Add key-value pair(s) to the test report
49
82
  * @param {{[key: string]: string} | string} keyValue - object { key: value } (multiple props allowed) OR key (string)
@@ -67,6 +100,14 @@ function setKeyValue(keyValue, value = undefined) {
67
100
  return;
68
101
  }
69
102
 
103
+ if (process.env.VITEST || process.env.VITEST_WORKER_ID) {
104
+ const vitestTest = getCurrentVitestTest();
105
+ if (vitestTest) {
106
+ Object.assign((vitestTest.meta ||= {}), keyValue);
107
+ return;
108
+ }
109
+ }
110
+
70
111
  // in this case keyValue is expected to be an object
71
112
  services.keyValues.put(keyValue);
72
113
  }