@ark/attest 0.26.0 → 0.28.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/out/assert/attest.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { ErrorMessage } from "@ark/util";
|
|
|
3
3
|
import type { Measure } from "../bench/measure.ts";
|
|
4
4
|
import { type VersionedTypeAssertion } from "../cache/getCachedAssertions.ts";
|
|
5
5
|
import { type AttestConfig } from "../config.ts";
|
|
6
|
+
import { type TypeAssertionMapping } from "./assertions.ts";
|
|
6
7
|
import { ChainableAssertions, type AssertionKind, type rootAssertions } from "./chainableAssertions.ts";
|
|
7
8
|
export type AttestFn = {
|
|
8
9
|
<expected, actual extends expected = never>(...args: actual extends never ? [
|
|
@@ -11,8 +12,9 @@ export type AttestFn = {
|
|
|
11
12
|
<expected>(actual: expected): rootAssertions<expected, AssertionKind>;
|
|
12
13
|
instantiations: (count?: Measure<"instantiations"> | undefined) => void;
|
|
13
14
|
};
|
|
15
|
+
export type VersionableActual = {} | null | undefined | TypeAssertionMapping;
|
|
14
16
|
export type AssertionContext = {
|
|
15
|
-
|
|
17
|
+
versionableActual: VersionableActual;
|
|
16
18
|
originalAssertedValue: unknown;
|
|
17
19
|
cfg: AttestConfig;
|
|
18
20
|
allowRegex: boolean;
|
package/out/assert/attest.js
CHANGED
|
@@ -9,7 +9,7 @@ export const attestInternal = (value, { cfg: cfgHooks, ...ctxHooks } = {}) => {
|
|
|
9
9
|
const position = caller();
|
|
10
10
|
const cfg = { ...getConfig(), ...cfgHooks };
|
|
11
11
|
const ctx = {
|
|
12
|
-
|
|
12
|
+
versionableActual: value,
|
|
13
13
|
allowRegex: false,
|
|
14
14
|
originalAssertedValue: value,
|
|
15
15
|
position,
|
|
@@ -7,12 +7,17 @@ export type ChainableAssertionOptions = {
|
|
|
7
7
|
defaultExpected?: unknown;
|
|
8
8
|
};
|
|
9
9
|
type AssertionRecord = Record<keyof rootAssertions<any, AssertionKind>, unknown>;
|
|
10
|
+
export type UnwrapOptions = {
|
|
11
|
+
versionable?: boolean;
|
|
12
|
+
serialize?: boolean;
|
|
13
|
+
};
|
|
10
14
|
export declare class ChainableAssertions implements AssertionRecord {
|
|
11
15
|
private ctx;
|
|
12
16
|
constructor(ctx: AssertionContext);
|
|
13
|
-
private serialize;
|
|
14
17
|
private get unversionedActual();
|
|
18
|
+
private get versionableActual();
|
|
15
19
|
private get serializedActual();
|
|
20
|
+
unwrap(opts?: UnwrapOptions): unknown;
|
|
16
21
|
private snapRequiresUpdate;
|
|
17
22
|
get unknown(): this;
|
|
18
23
|
is(expected: unknown): this;
|
|
@@ -44,23 +49,30 @@ export type valueFromTypeAssertion<expected, chained = expected> = inferredAsser
|
|
|
44
49
|
type snapProperty<expected, kind extends AssertionKind> = {
|
|
45
50
|
(expected?: snapshot<expected>): nextAssertions<kind>;
|
|
46
51
|
toFile: (id: string, options?: ExternalSnapshotOptions) => nextAssertions<kind>;
|
|
52
|
+
unwrap: Unwrapper<expected>;
|
|
47
53
|
};
|
|
54
|
+
export type Unwrapper<expected = unknown> = (opts?: UnwrapOptions) => expected;
|
|
48
55
|
export type comparableValueAssertion<expected, kind extends AssertionKind> = {
|
|
49
56
|
snap: snapProperty<expected, kind>;
|
|
50
57
|
equals: (value: expected) => nextAssertions<kind>;
|
|
51
58
|
instanceOf: (constructor: Constructor) => nextAssertions<kind>;
|
|
52
59
|
is: (value: expected) => nextAssertions<kind>;
|
|
53
|
-
completions:
|
|
60
|
+
completions: CompletionsSnap;
|
|
54
61
|
satisfies: <def>(def: type.validate<def>) => nextAssertions<kind>;
|
|
55
62
|
unknown: Omit<comparableValueAssertion<unknown, kind>, "unknown">;
|
|
63
|
+
unwrap: Unwrapper<expected>;
|
|
56
64
|
};
|
|
65
|
+
export interface CompletionsSnap {
|
|
66
|
+
(value?: Completions): void;
|
|
67
|
+
unwrap: Unwrapper<Completions>;
|
|
68
|
+
}
|
|
57
69
|
export type TypeAssertionsRoot = {
|
|
58
70
|
type: TypeAssertionProps;
|
|
59
71
|
};
|
|
60
72
|
export type TypeAssertionProps = {
|
|
61
73
|
toString: valueFromTypeAssertion<string | RegExp>;
|
|
62
74
|
errors: valueFromTypeAssertion<string | RegExp, string>;
|
|
63
|
-
completions:
|
|
75
|
+
completions: CompletionsSnap;
|
|
64
76
|
};
|
|
65
77
|
export type ExternalSnapshotOptions = {
|
|
66
78
|
path?: string;
|
|
@@ -13,17 +13,21 @@ export class ChainableAssertions {
|
|
|
13
13
|
constructor(ctx) {
|
|
14
14
|
this.ctx = ctx;
|
|
15
15
|
}
|
|
16
|
-
serialize(value) {
|
|
17
|
-
return snapshot(value);
|
|
18
|
-
}
|
|
19
16
|
get unversionedActual() {
|
|
20
|
-
if (this.
|
|
21
|
-
return this.
|
|
17
|
+
if (this.versionableActual instanceof TypeAssertionMapping) {
|
|
18
|
+
return this.versionableActual.fn(this.ctx.typeRelationshipAssertionEntries[0][1], this.ctx).actual;
|
|
22
19
|
}
|
|
23
|
-
return this.
|
|
20
|
+
return this.versionableActual;
|
|
21
|
+
}
|
|
22
|
+
get versionableActual() {
|
|
23
|
+
return this.ctx.versionableActual;
|
|
24
24
|
}
|
|
25
25
|
get serializedActual() {
|
|
26
|
-
return
|
|
26
|
+
return snapshot(this.unversionedActual);
|
|
27
|
+
}
|
|
28
|
+
unwrap(opts) {
|
|
29
|
+
const value = opts?.versionable ? this.versionableActual : this.unversionedActual;
|
|
30
|
+
return opts?.serialize ? snapshot(value) : value;
|
|
27
31
|
}
|
|
28
32
|
snapRequiresUpdate(expectedSerialized) {
|
|
29
33
|
return (!isDeepStrictEqual(this.serializedActual, expectedSerialized) ||
|
|
@@ -39,19 +43,20 @@ export class ChainableAssertions {
|
|
|
39
43
|
return this;
|
|
40
44
|
}
|
|
41
45
|
equals(expected) {
|
|
42
|
-
assertEquals(expected, this.
|
|
46
|
+
assertEquals(expected, this.versionableActual, this.ctx);
|
|
43
47
|
return this;
|
|
44
48
|
}
|
|
45
49
|
satisfies(def) {
|
|
46
|
-
assertSatisfies(type.raw(def), this.
|
|
50
|
+
assertSatisfies(type.raw(def), this.versionableActual, this.ctx);
|
|
47
51
|
return this;
|
|
48
52
|
}
|
|
49
53
|
instanceOf(expected) {
|
|
50
|
-
if (!(this.
|
|
54
|
+
if (!(this.versionableActual instanceof expected)) {
|
|
51
55
|
throwAssertionError({
|
|
52
56
|
stack: this.ctx.assertionStack,
|
|
53
|
-
message: `Expected an instance of ${expected.name} (was ${typeof this.
|
|
54
|
-
this.
|
|
57
|
+
message: `Expected an instance of ${expected.name} (was ${(typeof this.versionableActual === "object" &&
|
|
58
|
+
this.versionableActual !== null) ?
|
|
59
|
+
this.versionableActual.constructor.name
|
|
55
60
|
: this.serializedActual})`
|
|
56
61
|
});
|
|
57
62
|
}
|
|
@@ -61,7 +66,7 @@ export class ChainableAssertions {
|
|
|
61
66
|
// Use variadic args to distinguish undefined being passed explicitly from no args
|
|
62
67
|
const inline = (...args) => {
|
|
63
68
|
const snapName = this.ctx.lastSnapName ?? "snap";
|
|
64
|
-
const expectedSerialized =
|
|
69
|
+
const expectedSerialized = snapshot(args[0]);
|
|
65
70
|
if (!args.length || this.ctx.cfg.updateSnapshots) {
|
|
66
71
|
if (this.snapRequiresUpdate(expectedSerialized)) {
|
|
67
72
|
const snapshotArgs = {
|
|
@@ -98,7 +103,10 @@ export class ChainableAssertions {
|
|
|
98
103
|
assertEquals(expectedSnapshot, this.serializedActual, this.ctx);
|
|
99
104
|
return this;
|
|
100
105
|
};
|
|
101
|
-
return Object.assign(inline, {
|
|
106
|
+
return Object.assign(inline, {
|
|
107
|
+
toFile,
|
|
108
|
+
unwrap: this.unwrap.bind(this)
|
|
109
|
+
});
|
|
102
110
|
}
|
|
103
111
|
immediateOrChained() {
|
|
104
112
|
const immediateAssertion = (...args) => {
|
|
@@ -113,9 +121,9 @@ export class ChainableAssertions {
|
|
|
113
121
|
}
|
|
114
122
|
}
|
|
115
123
|
if (this.ctx.allowRegex)
|
|
116
|
-
assertEqualOrMatching(expected, this.
|
|
124
|
+
assertEqualOrMatching(expected, this.versionableActual, this.ctx);
|
|
117
125
|
else
|
|
118
|
-
assertEquals(expected, this.
|
|
126
|
+
assertEquals(expected, this.versionableActual, this.ctx);
|
|
119
127
|
return this;
|
|
120
128
|
};
|
|
121
129
|
return new Proxy(immediateAssertion, {
|
|
@@ -124,7 +132,7 @@ export class ChainableAssertions {
|
|
|
124
132
|
}
|
|
125
133
|
get throws() {
|
|
126
134
|
const result = callAssertedFunction(this.unversionedActual);
|
|
127
|
-
this.ctx.
|
|
135
|
+
this.ctx.versionableActual = getThrownMessage(result, this.ctx);
|
|
128
136
|
this.ctx.allowRegex = true;
|
|
129
137
|
this.ctx.defaultExpected = "";
|
|
130
138
|
return this.immediateOrChained();
|
|
@@ -140,7 +148,7 @@ export class ChainableAssertions {
|
|
|
140
148
|
get completions() {
|
|
141
149
|
if (this.ctx.cfg.skipTypes)
|
|
142
150
|
return chainableNoOpProxy;
|
|
143
|
-
this.ctx.
|
|
151
|
+
this.ctx.versionableActual = new TypeAssertionMapping(data => {
|
|
144
152
|
if (typeof data.completions === "string") {
|
|
145
153
|
// if the completions were ambiguously defined, e.g. two string
|
|
146
154
|
// literals with the same value, they are writen as an error
|
|
@@ -159,14 +167,14 @@ export class ChainableAssertions {
|
|
|
159
167
|
const self = this;
|
|
160
168
|
return {
|
|
161
169
|
get toString() {
|
|
162
|
-
self.ctx.
|
|
170
|
+
self.ctx.versionableActual = new TypeAssertionMapping(data => ({
|
|
163
171
|
actual: formatTypeString(data.args[0].type)
|
|
164
172
|
}));
|
|
165
173
|
self.ctx.allowRegex = true;
|
|
166
174
|
return self.immediateOrChained();
|
|
167
175
|
},
|
|
168
176
|
get errors() {
|
|
169
|
-
self.ctx.
|
|
177
|
+
self.ctx.versionableActual = new TypeAssertionMapping(data => ({
|
|
170
178
|
actual: data.errors.join("\n")
|
|
171
179
|
}));
|
|
172
180
|
self.ctx.allowRegex = true;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ark/attest",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.28.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "David Blass",
|
|
@@ -36,12 +36,12 @@
|
|
|
36
36
|
"@typescript/analyze-trace": "0.10.1",
|
|
37
37
|
"prettier": "3.3.3",
|
|
38
38
|
"@prettier/sync": "0.5.2",
|
|
39
|
-
"@ark/fs": "0.
|
|
40
|
-
"
|
|
41
|
-
"
|
|
39
|
+
"@ark/fs": "0.24.0",
|
|
40
|
+
"arktype": "2.0.0-rc.24",
|
|
41
|
+
"@ark/util": "0.24.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"typescript": "5.7.
|
|
44
|
+
"typescript": "5.7.1-rc"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"typescript": "*"
|