-
-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix timing issue breaking node --test serialization
The TestMap class was getting confused between the `Test` object and the `TestBase` object, becuase the `.t` member can't be accessed while the Test class is still constructing. The intent was for it to swap over to the t.t once available, but that wasn't happening properly if multiple a subtests were created in the same tick as the root TAP object being created. In any event, it was overly clever. Just slap a pseudorandom key on it with a known Symbol property, and use that as the key in the diags and subs maps.
- Loading branch information
Showing
3 changed files
with
66 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,40 @@ | ||
// A map of test objects, but make sure that we don't get confused | ||
// if we get the Test proxy or the underlying TestBase. | ||
|
||
import type { Base, TestBase } from '@tapjs/core' | ||
import { Base } from '@tapjs/core' | ||
|
||
const kSerializationKey = Symbol.for('@tapjs/node-serialize.key') | ||
const getKey = ( | ||
t: Base & { [kSerializationKey]?: string } | ||
): string => { | ||
const k = t[kSerializationKey] | ||
if (k) return k | ||
const n = String(Math.random()) | ||
Object.defineProperty(t, kSerializationKey, { | ||
value: n, | ||
writable: false, | ||
configurable: true, | ||
enumerable: false, | ||
}) | ||
return n | ||
} | ||
|
||
export class TestMap<T extends {}> extends Map<Base, T> { | ||
get(t: Base): T | undefined { | ||
const tt = (t as TestBase).t | ||
if (tt !== undefined && tt !== t) { | ||
let vt = super.get((t as TestBase).t) | ||
if (vt === undefined) { | ||
const v = super.get(t) | ||
if (v !== undefined) { | ||
super.set(tt, v) | ||
super.delete(t) | ||
return v | ||
} | ||
constructor(items?: [Base, T][]) { | ||
super() | ||
if (items) { | ||
for (const [t, v] of items) { | ||
this.set(t, v) | ||
} | ||
return vt | ||
} | ||
return super.get(t) | ||
} | ||
get(t: Base): T | undefined { | ||
return super.get(getKey(t) as unknown as Base) | ||
} | ||
has(t: Base) { | ||
const tt = (t as TestBase).t | ||
return super.has(t) || super.has(tt) | ||
return super.has(getKey(t) as unknown as Base) | ||
} | ||
set(t: Base, v: T) { | ||
const tt = (t as TestBase).t | ||
if (tt !== undefined && tt !== t) { | ||
super.delete(t) | ||
super.set(tt, v) | ||
} else { | ||
super.set(t, v) | ||
} | ||
return this | ||
return super.set(getKey(t) as unknown as Base, v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters