@abide/harness 0.7.2 → 0.8.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abide/harness",
3
- "version": "0.7.2",
3
+ "version": "0.8.0",
4
4
  "type": "module",
5
5
  "description": "Ratios against hand-written code, counts of the work an op did, and gates whose revert can be run — three measurement lanes for anything that renders, on bun",
6
6
  "license": "MIT",
@@ -56,7 +56,7 @@
56
56
  },
57
57
  "peerDependencies": {
58
58
  "@playwright/test": "^1.64.0",
59
- "@abide/abide": ">=0.145.0"
59
+ "@abide/abide": ">=0.152.0"
60
60
  },
61
61
  "peerDependenciesMeta": {
62
62
  "@playwright/test": {
@@ -35,10 +35,19 @@ export type AbideWork = Work & {
35
35
  /**
36
36
  * Runs `body` inside an armed case and returns the work it did, abide's counters included.
37
37
  *
38
+ * A body answering a thenable is counted until it settles, as the lane's own `measure` is (43.50).
38
39
  * Throws where abide's record is published without one of its fields as a number (43.20).
39
40
  */
40
- export function measure(body: () => void): AbideWork {
41
- const work = measureWork(body)
41
+ export function measure(body: () => PromiseLike<unknown>): Promise<AbideWork>
42
+ export function measure(body: () => void): AbideWork
43
+ export function measure(body: () => unknown): AbideWork | Promise<AbideWork> {
44
+ const work: Work | Promise<Work> = measureWork(body as () => void)
45
+ return work instanceof Promise
46
+ ? work.then(withAbideCounters)
47
+ : withAbideCounters(work)
48
+ }
49
+
50
+ function withAbideCounters(work: Work): AbideWork {
42
51
  const counted = publishedBy(work, 'abide', FIELDS) ?? {
43
52
  wakes: null,
44
53
  bindingRuns: null,
@@ -10,6 +10,8 @@
10
10
  // happy-dom keeps selectedness under its own symbols, which no export names; they are found by their
11
11
  // descriptions, as `bridgeFiles` finds the one a `Blob` keeps its bytes under.
12
12
 
13
+ import { symbolNamed } from './symbolNamed.ts'
14
+
13
15
  export function installSelect(): void {
14
16
  const optionPrototype = HTMLOptionElement.prototype
15
17
  if (Object.getOwnPropertyDescriptor(optionPrototype, 'defaultSelected'))
@@ -23,21 +25,12 @@ export function installSelect(): void {
23
25
  this.toggleAttribute('selected', Boolean(selected))
24
26
  },
25
27
  })
26
- const symbolNamed = (holder: object, description: string): symbol => {
27
- const found = Object.getOwnPropertySymbols(holder).find(
28
- (symbol) => symbol.description === description,
29
- )
30
- if (found === undefined)
31
- throw new Error(
32
- `@abide/harness: happy-dom no longer keeps a select's '${description}' where installSelect reads it. RULEBOOK 43.47.`,
33
- )
34
- return found
35
- }
36
28
  const selectPrototype = HTMLSelectElement.prototype
37
- const UPDATE = symbolNamed(selectPrototype, 'updateSelectedness')
29
+ const UPDATE = symbolNamed(selectPrototype, 'updateSelectedness', '43.47')
38
30
  const SELECTEDNESS = symbolNamed(
39
31
  document.createElement('option'),
40
32
  'selectedness',
33
+ '43.47',
41
34
  )
42
35
  type Held = HTMLOptionElement & Record<symbol, boolean>
43
36
  const optionsOf = (select: HTMLSelectElement): Held[] =>
@@ -53,15 +53,47 @@ function copyPublished(record: Work): void {
53
53
  }
54
54
  }
55
55
 
56
- /** Runs `body` inside an armed case and returns the `Work` it did. */
57
- export function measure(body: () => void): Work {
56
+ /**
57
+ * Runs `body` inside an armed case and returns the `Work` it did.
58
+ *
59
+ * A body answering a thenable keeps the case armed until it settles, so the answer is a promise of
60
+ * the record, and every counted call in that window lands in it, the body's or not (43.50).
61
+ */
62
+ export function measure(body: () => PromiseLike<unknown>): Promise<Work>
63
+ export function measure(body: () => void): Work
64
+ export function measure(body: () => unknown): Work | Promise<Work> {
58
65
  const record = blankWork()
59
66
  zeroPublished()
60
67
  arm(record)
68
+ let answer: unknown
61
69
  // The case is closed before a throw leaves, or the next `measure()` reports that one is
62
70
  // already armed and the real failure is two frames down.
63
71
  try {
64
- body()
72
+ answer = body()
73
+ } catch (error) {
74
+ disarm()
75
+ throw error
76
+ }
77
+ if (isThenable(answer)) return closeAfter(answer, record)
78
+ disarm()
79
+ copyPublished(record)
80
+ return record
81
+ }
82
+
83
+ function isThenable(value: unknown): value is PromiseLike<unknown> {
84
+ return (
85
+ value !== null &&
86
+ (typeof value === 'object' || typeof value === 'function') &&
87
+ typeof (value as { then?: unknown }).then === 'function'
88
+ )
89
+ }
90
+
91
+ async function closeAfter(
92
+ pending: PromiseLike<unknown>,
93
+ record: Work,
94
+ ): Promise<Work> {
95
+ try {
96
+ await pending
65
97
  } finally {
66
98
  disarm()
67
99
  }
@@ -8,6 +8,7 @@ import { installFormReset } from './installFormReset.ts'
8
8
  import { installLeadingNewline } from './installLeadingNewline.ts'
9
9
  import { installPopover } from './installPopover.ts'
10
10
  import { installSelect } from './installSelect.ts'
11
+ import { symbolNamed } from './symbolNamed.ts'
11
12
 
12
13
  // A child spawned with this env would paint its output in ANSI off a pipe (bun's console, and
13
14
  // abide's own log under RULEBOOK 36.9), and the suites compare that output as text. A terminal
@@ -77,9 +78,7 @@ function bridgeFiles(): void {
77
78
  const RuntimeBlob = native.Blob as typeof Blob
78
79
  const RuntimeFile = native.File as typeof File
79
80
  const RuntimeFormData = native.FormData as typeof FormData
80
- const BYTES = Object.getOwnPropertySymbols(
81
- Reflect.construct(DomBlob, []),
82
- ).find((symbol) => symbol.description === 'buffer') as symbol
81
+ const BYTES = symbolNamed(Reflect.construct(DomBlob, []), 'buffer', '43.37')
83
82
  const bytesOf = (parts: readonly unknown[]): Buffer | undefined => {
84
83
  const buffers: Buffer[] = []
85
84
  for (const part of parts) {
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The symbol `holder` keeps under `description`, a private one of happy-dom's the preload reaches
3
+ * for — thrown for by `clause` where happy-dom no longer keeps it, rather than read as `undefined`
4
+ * and degrading the bridge it was found for in silence.
5
+ */
6
+ export function symbolNamed(
7
+ holder: object,
8
+ description: string,
9
+ clause: string,
10
+ ): symbol {
11
+ const found = Object.getOwnPropertySymbols(holder).find(
12
+ (symbol) => symbol.description === description,
13
+ )
14
+ if (found === undefined)
15
+ throw new Error(
16
+ `@abide/harness: happy-dom no longer keeps '${description}' where the preload reads it. RULEBOOK ${clause}.`,
17
+ )
18
+ return found
19
+ }