@describe-me/core 0.1.1 → 0.3.0

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.
@@ -1,7 +1,9 @@
1
- import type { ComponentInfo, FrameKind, TestRecord } from './types.js';
1
+ import type { CaptureOptions, ComponentInfo, FrameKind, TestRecord } from './types.js';
2
+ type Teardown = () => void;
2
3
  /**
3
- * Browser-side recorder. One instance per test iframe; `begin()` in beforeEach,
4
- * `end()` in afterEach. Framework adapters call `capture()` and `setComponent()`.
4
+ * Test-side recorder, running wherever the tests run: the browser-mode iframe
5
+ * or jsdom. `begin()` in beforeEach, `end()` then `teardown()` in afterEach.
6
+ * Framework adapters call `capture()`, `setComponent()` and `onTeardown()`.
5
7
  */
6
8
  declare class Recorder {
7
9
  private frames;
@@ -10,11 +12,20 @@ declare class Recorder {
10
12
  private active;
11
13
  private lastHash;
12
14
  private seq;
15
+ private teardowns;
13
16
  begin(): void;
14
17
  get isActive(): boolean;
15
18
  setComponent(info: ComponentInfo): void;
16
- capture(kind: FrameKind, label: string, meta?: Record<string, unknown>): Promise<void>;
19
+ capture(kind: FrameKind, label: string, meta?: Record<string, unknown>, options?: CaptureOptions): Promise<void>;
17
20
  end(): TestRecord;
21
+ /**
22
+ * Register work that must run after the test's frames are handed over, such
23
+ * as unmounting. Adapters call this once at import; registering the same
24
+ * callback again is a no-op.
25
+ */
26
+ onTeardown(callback: Teardown): void;
27
+ /** Run the registered teardowns. Called by the runner integration after `end()`. */
28
+ teardown(): void;
18
29
  }
19
30
  export declare const recorder: Recorder;
20
31
  export {};
package/dist/recorder.js CHANGED
@@ -3,8 +3,9 @@ import { materializeAdoptedStyles } from './materialize-adopted-styles.js';
3
3
  import { quickHash } from './quick-hash.js';
4
4
  import { settle } from './settle.js';
5
5
  /**
6
- * Browser-side recorder. One instance per test iframe; `begin()` in beforeEach,
7
- * `end()` in afterEach. Framework adapters call `capture()` and `setComponent()`.
6
+ * Test-side recorder, running wherever the tests run: the browser-mode iframe
7
+ * or jsdom. `begin()` in beforeEach, `end()` then `teardown()` in afterEach.
8
+ * Framework adapters call `capture()`, `setComponent()` and `onTeardown()`.
8
9
  */
9
10
  class Recorder {
10
11
  frames = [];
@@ -13,6 +14,7 @@ class Recorder {
13
14
  active = false;
14
15
  lastHash = '';
15
16
  seq = 0;
17
+ teardowns = new Set();
16
18
  begin() {
17
19
  this.frames = [];
18
20
  this.component = undefined;
@@ -29,11 +31,13 @@ class Recorder {
29
31
  this.component = info;
30
32
  }
31
33
  }
32
- async capture(kind, label, meta) {
34
+ async capture(kind, label, meta, options = {}) {
33
35
  if (!this.active) {
34
36
  return;
35
37
  }
36
- await settle();
38
+ if (options.settle !== false) {
39
+ await settle();
40
+ }
37
41
  const node = await materializeAdoptedStyles(() => snapshot(document, { mirror: createMirror(), inlineStylesheet: true }));
38
42
  if (!node) {
39
43
  return;
@@ -65,6 +69,20 @@ class Recorder {
65
69
  this.active = false;
66
70
  return { frames: this.frames, component: this.component };
67
71
  }
72
+ /**
73
+ * Register work that must run after the test's frames are handed over, such
74
+ * as unmounting. Adapters call this once at import; registering the same
75
+ * callback again is a no-op.
76
+ */
77
+ onTeardown(callback) {
78
+ this.teardowns.add(callback);
79
+ }
80
+ /** Run the registered teardowns. Called by the runner integration after `end()`. */
81
+ teardown() {
82
+ for (const callback of this.teardowns) {
83
+ callback();
84
+ }
85
+ }
68
86
  }
69
87
  const RECORDER_KEY = Symbol.for('describe-me.recorder');
70
88
  /**
package/dist/types.d.ts CHANGED
@@ -4,6 +4,16 @@ export interface ComponentInfo {
4
4
  name: string;
5
5
  props: Record<string, unknown>;
6
6
  }
7
+ /** Options for `recorder.capture()`. */
8
+ export interface CaptureOptions {
9
+ /**
10
+ * Wait one macrotask for the framework to flush before looking at the DOM.
11
+ * Default: true. Pass false when the caller knows the DOM is already
12
+ * committed (e.g. right after Testing Library's `act()`-wrapped `rerender`):
13
+ * the snapshot is then taken synchronously, before the caller moves on.
14
+ */
15
+ settle?: boolean;
16
+ }
7
17
  /** A frame as captured in the browser. `snapshot` is an rrweb serialized document. */
8
18
  export interface Frame {
9
19
  id: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@describe-me/core",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "description": "Recorder and manifest types for describe-me: living component docs from your tests",
5
5
  "keywords": [
6
6
  "vitest",