@t0.labs/daxta 0.4.10 → 0.4.11

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/README.md CHANGED
@@ -33,8 +33,8 @@ Jest finishes — Docs ready on /docs
33
33
  Docs ready → http://localhost:3000/docs
34
34
  ```
35
35
 
36
- 1. **Install** attaches DAxTA as a Jest plugin (`setupFilesAfterEnv` + reporter). Your existing `test:integration` stays the entrypoint — DAxTA does not wrap the process.
37
- 2. **During the run** only traffic is recorded (disk flush). OpenAPI/HTML is built once when Jest finishes.
36
+ 1. **Install** attaches DAxTA as a Jest plugin (`setupFilesAfterEnv` + reporter, and `globalSetup` when you don’t already have one). Your existing `test:integration` stays the entrypoint — DAxTA does not wrap the process.
37
+ 2. **During the run** only traffic is recorded (disk flush). OpenAPI/HTML is rebuilt once when Jest finishes, from **this run’s hits only** (previous worker shards are cleared so examples do not stack).
38
38
  3. `apiDocs(app)` in `main.ts` serves the generated viewer when `DAXTA_DOCS=true`.
39
39
 
40
40
 
@@ -78,7 +78,7 @@ npx @t0.labs/daxta uninstall
78
78
  Otomatik temizler:
79
79
 
80
80
  - `apiDocs` + `@t0.labs/daxta` import (`main.ts`)
81
- - Jest `setup` + `jest-reporter` hooks
81
+ - Jest `setup` + `jest-reporter` (+ `globalSetup` when missing)
82
82
  - `daxta.config.*` ve `daxta/` output
83
83
  - `.gitignore` / `.env` içindeki DAxTA satırları
84
84
  - `@t0.labs/daxta` paketi
@@ -1,4 +1,4 @@
1
- import type { RecordedHit } from '../recorder';
1
+ import { type RecordedHit } from '../recorder';
2
2
  export type Scenario = {
3
3
  name: string;
4
4
  group: string;
@@ -42,6 +42,7 @@ const catalog_1 = require("../catalog");
42
42
  const config_1 = require("../config");
43
43
  const dto_fields_1 = require("../fields/dto-fields");
44
44
  const path_util_1 = require("../path.util");
45
+ const recorder_1 = require("../recorder");
45
46
  const paths_1 = require("../serve/paths");
46
47
  const viewer_store_1 = require("../serve/viewer-store");
47
48
  const METHOD_RANK = { get: 0, post: 1, put: 2, patch: 3, delete: 4 };
@@ -403,33 +404,48 @@ function dedupeScenarios(scenarios) {
403
404
  return true;
404
405
  });
405
406
  }
406
- function loadHits() {
407
- if (!(0, fs_1.existsSync)((0, paths_1.getOutDir)()))
407
+ /** `hits-w{workerId}-p{pid}.json` leftover from a previous Jest run must not stack. */
408
+ function currentRunWorkerFiles(outDir) {
409
+ const names = (0, fs_1.readdirSync)(outDir).filter((fileName) => fileName.startsWith('hits-w') && fileName.endsWith('.json'));
410
+ if (!names.length)
408
411
  return [];
409
- const workerFiles = (0, fs_1.readdirSync)((0, paths_1.getOutDir)()).filter((fileName) => fileName.startsWith('hits-w') && fileName.endsWith('.json'));
410
- if (workerFiles.length) {
411
- return canonicalizeHits(workerFiles.flatMap((fileName) => {
412
- try {
413
- const parsed = JSON.parse((0, fs_1.readFileSync)(path.join((0, paths_1.getOutDir)(), fileName), 'utf8'));
414
- return Array.isArray(parsed) ? parsed : [];
415
- }
416
- catch (error) {
417
- console.error(`DAxTA: skipped corrupt hits file ${fileName}:`, error);
418
- return [];
419
- }
420
- }));
412
+ const hitsJsonPath = (0, paths_1.getHitsJsonPath)();
413
+ const watermark = (0, fs_1.existsSync)(hitsJsonPath) ? (0, fs_1.statSync)(hitsJsonPath).mtimeMs - 1000 : 0;
414
+ const fromThisRun = names.filter((fileName) => (0, fs_1.statSync)(path.join(outDir, fileName)).mtimeMs >= watermark);
415
+ const pool = fromThisRun.length ? fromThisRun : names;
416
+ const newestByWorker = new Map();
417
+ for (const fileName of pool) {
418
+ const workerId = fileName.match(/^hits-w([^-]+)-p\d+\.json$/)?.[1] ?? fileName;
419
+ const mtime = (0, fs_1.statSync)(path.join(outDir, fileName)).mtimeMs;
420
+ const prev = newestByWorker.get(workerId);
421
+ if (!prev || mtime >= prev.mtime)
422
+ newestByWorker.set(workerId, { fileName, mtime });
421
423
  }
422
- // Fallback after workers were cleaned — rebuild from last merged hits.json
424
+ return [...newestByWorker.values()].map((entry) => entry.fileName);
425
+ }
426
+ function readHitFile(filePath, label) {
423
427
  try {
424
- if (!(0, fs_1.existsSync)((0, paths_1.getHitsJsonPath)()))
425
- return [];
426
- const parsed = JSON.parse((0, fs_1.readFileSync)((0, paths_1.getHitsJsonPath)(), 'utf8'));
427
- return canonicalizeHits(Array.isArray(parsed) ? parsed : []);
428
+ const parsed = JSON.parse((0, fs_1.readFileSync)(filePath, 'utf8'));
429
+ return Array.isArray(parsed) ? parsed : [];
428
430
  }
429
431
  catch (error) {
430
- console.error('DAxTA: skipped corrupt hits.json:', error);
432
+ console.error(`DAxTA: skipped corrupt hits file ${label}:`, error);
433
+ return [];
434
+ }
435
+ }
436
+ function loadHits() {
437
+ if (!(0, fs_1.existsSync)((0, paths_1.getOutDir)()))
431
438
  return [];
439
+ const workerFiles = currentRunWorkerFiles((0, paths_1.getOutDir)());
440
+ if (workerFiles.length) {
441
+ const loaded = canonicalizeHits(workerFiles.flatMap((fileName) => readHitFile(path.join((0, paths_1.getOutDir)(), fileName), fileName)));
442
+ if (loaded.length)
443
+ return loaded;
432
444
  }
445
+ // Fallback after workers were cleaned — last completed run's hits.json
446
+ if (!(0, fs_1.existsSync)((0, paths_1.getHitsJsonPath)()))
447
+ return [];
448
+ return canonicalizeHits(readHitFile((0, paths_1.getHitsJsonPath)(), 'hits.json'));
433
449
  }
434
450
  function sortOperations(entries) {
435
451
  return [...entries].sort((left, right) => {
@@ -737,13 +753,16 @@ function buildDaxtaSpec(options = {}) {
737
753
  const refreshHtml = writeHtml && (operationsChanged || metaChanged || viewerTemplateNewerThanHtml());
738
754
  const changed = operationsChanged || hitsChanged || metaChanged || refreshHtml;
739
755
  if (changed) {
740
- if (hitsChanged)
741
- writeJson((0, paths_1.getHitsJsonPath)(), hits);
756
+ writeJson((0, paths_1.getHitsJsonPath)(), hits);
742
757
  if (operationsChanged || metaChanged)
743
758
  writeJson((0, paths_1.getSpecJsonPath)(), spec);
744
759
  if (refreshHtml)
745
760
  (0, fs_1.writeFileSync)((0, paths_1.getHtmlPath)(), toStandaloneHtml(spec));
746
761
  }
762
+ else {
763
+ writeJson((0, paths_1.getHitsJsonPath)(), hits);
764
+ }
765
+ (0, recorder_1.clearWorkerHits)();
747
766
  const operations = countOperations(spec);
748
767
  const result = {
749
768
  skipped: false,
@@ -6,7 +6,8 @@ export declare function resolveJestConfigPath(cwd: string, options: {
6
6
  }): string | null;
7
7
  /**
8
8
  * Inject DAxTA as a Jest plugin (setup + reporter). Does not wrap the test process.
9
- * Supports JSON and JS/CJS/MJS/TS config files. Leaves globalSetup / globalTeardown untouched.
9
+ * Supports JSON and JS/CJS/MJS/TS config files. Adds globalSetup only when missing
10
+ * (does not replace an existing user globalSetup).
10
11
  */
11
12
  export declare function injectJestHooks(jestConfigPath: string, dryRun?: boolean): string[];
12
13
  /** Restore package.json script if it was wrapped as `daxta test --yes`. */
@@ -43,6 +43,7 @@ const path = __importStar(require("path"));
43
43
  const discover_test_1 = require("./discover-test");
44
44
  const SETUP = '@t0.labs/daxta/setup';
45
45
  const REPORTER = '@t0.labs/daxta/jest-reporter';
46
+ const GLOBAL_SETUP = '@t0.labs/daxta/jest-global-setup';
46
47
  function loadJestJson(filePath) {
47
48
  return JSON.parse((0, fs_1.readFileSync)(filePath, 'utf8'));
48
49
  }
@@ -135,6 +136,13 @@ function injectIntoJsSource(source) {
135
136
  notes.push(`setupFilesAfterEnv = [${SETUP}]`);
136
137
  }
137
138
  }
139
+ if (!/\bglobalSetup\s*:/.test(next) && !next.includes(GLOBAL_SETUP)) {
140
+ const inserted = insertIntoObjectLiteral(next, `globalSetup: '${GLOBAL_SETUP}',`);
141
+ if (inserted) {
142
+ next = inserted;
143
+ notes.push(`globalSetup = ${GLOBAL_SETUP}`);
144
+ }
145
+ }
138
146
  if (!next.includes(REPORTER)) {
139
147
  const range = findArrayPropRange(next, 'reporters');
140
148
  if (range) {
@@ -177,6 +185,10 @@ function injectIntoJsonConfig(filePath, dryRun) {
177
185
  data.reporters = reporters;
178
186
  notes.push(`reporters += ${REPORTER}`);
179
187
  }
188
+ if (!data.globalSetup) {
189
+ data.globalSetup = GLOBAL_SETUP;
190
+ notes.push(`globalSetup = ${GLOBAL_SETUP}`);
191
+ }
180
192
  if (!dryRun && notes.length) {
181
193
  (0, fs_1.writeFileSync)(filePath, `${JSON.stringify(data, null, 2)}\n`);
182
194
  }
@@ -184,7 +196,8 @@ function injectIntoJsonConfig(filePath, dryRun) {
184
196
  }
185
197
  /**
186
198
  * Inject DAxTA as a Jest plugin (setup + reporter). Does not wrap the test process.
187
- * Supports JSON and JS/CJS/MJS/TS config files. Leaves globalSetup / globalTeardown untouched.
199
+ * Supports JSON and JS/CJS/MJS/TS config files. Adds globalSetup only when missing
200
+ * (does not replace an existing user globalSetup).
188
201
  */
189
202
  function injectJestHooks(jestConfigPath, dryRun = false) {
190
203
  if (!(0, fs_1.existsSync)(jestConfigPath))
@@ -261,6 +274,11 @@ function removeFromJsSource(source) {
261
274
  if (plain.removed)
262
275
  notes.push(`reporters -= ${REPORTER}`);
263
276
  }
277
+ const globalSetupRe = new RegExp(`\\s*globalSetup\\s*:\\s*['"]${GLOBAL_SETUP.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}['"]\\s*,?`);
278
+ if (globalSetupRe.test(next)) {
279
+ next = next.replace(globalSetupRe, '');
280
+ notes.push(`globalSetup -= ${GLOBAL_SETUP}`);
281
+ }
264
282
  return { source: next, notes };
265
283
  }
266
284
  function removeFromJsonConfig(filePath, dryRun) {
@@ -288,6 +306,10 @@ function removeFromJsonConfig(filePath, dryRun) {
288
306
  notes.push(`reporters -= ${REPORTER}`);
289
307
  }
290
308
  }
309
+ if (data.globalSetup === GLOBAL_SETUP) {
310
+ delete data.globalSetup;
311
+ notes.push(`globalSetup -= ${GLOBAL_SETUP}`);
312
+ }
291
313
  if (!dryRun && notes.length) {
292
314
  (0, fs_1.writeFileSync)(filePath, `${JSON.stringify(data, null, 2)}\n`);
293
315
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t0.labs/daxta",
3
- "version": "0.4.10",
3
+ "version": "0.4.11",
4
4
  "description": "DAxTA \u2014 docs from Nest integration traffic + DTO field CLI / Try-it",
5
5
  "license": "MIT",
6
6
  "repository": {