testomatio-reporter-cli 2.9.3-beta-vitestmeta → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "testomatio-reporter-cli",
3
- "version": "2.9.3-beta-vitestmeta",
3
+ "version": "2.9.3-beta-vitestmeta-2",
4
4
  "description": "Yarn Berry compatible standalone CLI for @testomatio/reporter",
5
5
  "type": "module",
6
6
  "bin": {
@@ -47,6 +47,36 @@ function addStep(message, logs) {
47
47
  else services.logger.step(message, logs);
48
48
  }
49
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
+
50
80
  /**
51
81
  * Add key-value pair(s) to the test report
52
82
  * @param {{[key: string]: string} | string} keyValue - object { key: value } (multiple props allowed) OR key (string)
@@ -71,13 +101,11 @@ function setKeyValue(keyValue, value = undefined) {
71
101
  }
72
102
 
73
103
  if (process.env.VITEST || process.env.VITEST_WORKER_ID) {
74
- try {
75
- const vitestTest = requireModule('@vitest/runner').getCurrentTest?.();
76
- if (vitestTest) {
77
- Object.assign((vitestTest.meta ||= {}), keyValue);
78
- return;
79
- }
80
- } catch {}
104
+ const vitestTest = getCurrentVitestTest();
105
+ if (vitestTest) {
106
+ Object.assign((vitestTest.meta ||= {}), keyValue);
107
+ return;
108
+ }
81
109
  }
82
110
 
83
111
  // in this case keyValue is expected to be an object