@ark/attest 0.9.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/LICENSE +7 -0
- package/README.md +284 -0
- package/out/assert/assertions.d.ts +28 -0
- package/out/assert/assertions.js +119 -0
- package/out/assert/attest.d.ts +26 -0
- package/out/assert/attest.js +40 -0
- package/out/assert/chainableAssertions.d.ts +68 -0
- package/out/assert/chainableAssertions.js +176 -0
- package/out/bench/await1k.d.ts +1 -0
- package/out/bench/await1k.js +1002 -0
- package/out/bench/baseline.d.ts +5 -0
- package/out/bench/baseline.js +60 -0
- package/out/bench/bench.d.ts +58 -0
- package/out/bench/bench.js +216 -0
- package/out/bench/call1k.d.ts +1 -0
- package/out/bench/call1k.js +1002 -0
- package/out/bench/measure.d.ts +29 -0
- package/out/bench/measure.js +48 -0
- package/out/bench/type.d.ts +16 -0
- package/out/bench/type.js +36 -0
- package/out/cache/getCachedAssertions.d.ts +11 -0
- package/out/cache/getCachedAssertions.js +70 -0
- package/out/cache/snapshots.d.ts +29 -0
- package/out/cache/snapshots.js +153 -0
- package/out/cache/ts.d.ts +45 -0
- package/out/cache/ts.js +127 -0
- package/out/cache/utils.d.ts +9 -0
- package/out/cache/utils.js +114 -0
- package/out/cache/writeAssertionCache.d.ts +42 -0
- package/out/cache/writeAssertionCache.js +141 -0
- package/out/cli/cli.d.ts +2 -0
- package/out/cli/cli.js +25 -0
- package/out/cli/precache.d.ts +1 -0
- package/out/cli/precache.js +7 -0
- package/out/cli/shared.d.ts +1 -0
- package/out/cli/shared.js +1 -0
- package/out/cli/stats.d.ts +1 -0
- package/out/cli/stats.js +54 -0
- package/out/cli/trace.d.ts +1 -0
- package/out/cli/trace.js +37 -0
- package/out/config.d.ts +42 -0
- package/out/config.js +94 -0
- package/out/fixtures.d.ts +6 -0
- package/out/fixtures.js +29 -0
- package/out/index.d.ts +9 -0
- package/out/index.js +8 -0
- package/out/tsVersioning.d.ts +45 -0
- package/out/tsVersioning.js +119 -0
- package/out/utils.d.ts +15 -0
- package/out/utils.js +36 -0
- package/package.json +48 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { caller } from "@ark/fs";
|
|
2
|
+
import { printable, snapshot } from "@ark/util";
|
|
3
|
+
import { type } from "arktype";
|
|
4
|
+
import * as assert from "node:assert/strict";
|
|
5
|
+
import { isDeepStrictEqual } from "node:util";
|
|
6
|
+
import { getSnapshotByName, queueSnapshotUpdate, updateExternalSnapshot } from "../cache/snapshots.js";
|
|
7
|
+
import { chainableNoOpProxy } from "../utils.js";
|
|
8
|
+
import { TypeAssertionMapping, assertEqualOrMatching, assertEquals, callAssertedFunction, getThrownMessage, throwAssertionError } from "./assertions.js";
|
|
9
|
+
export class ChainableAssertions {
|
|
10
|
+
ctx;
|
|
11
|
+
constructor(ctx) {
|
|
12
|
+
this.ctx = ctx;
|
|
13
|
+
}
|
|
14
|
+
serialize(value) {
|
|
15
|
+
return snapshot(value);
|
|
16
|
+
}
|
|
17
|
+
get unversionedActual() {
|
|
18
|
+
if (this.ctx.actual instanceof TypeAssertionMapping) {
|
|
19
|
+
return this.ctx.actual.fn(this.ctx.typeRelationshipAssertionEntries[0][1], this.ctx).actual;
|
|
20
|
+
}
|
|
21
|
+
return this.ctx.actual;
|
|
22
|
+
}
|
|
23
|
+
get serializedActual() {
|
|
24
|
+
return this.serialize(this.unversionedActual);
|
|
25
|
+
}
|
|
26
|
+
snapRequiresUpdate(expectedSerialized) {
|
|
27
|
+
return (!isDeepStrictEqual(this.serializedActual, expectedSerialized) ||
|
|
28
|
+
// If actual is undefined, we still need to write the "undefined" literal
|
|
29
|
+
// to the snap even though it will serialize to the same value as the (nonexistent) first arg
|
|
30
|
+
this.unversionedActual === undefined);
|
|
31
|
+
}
|
|
32
|
+
get unknown() {
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
is(expected) {
|
|
36
|
+
assert.equal(this.unversionedActual, expected);
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
equals(expected) {
|
|
40
|
+
assertEquals(expected, this.ctx.actual, this.ctx);
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
satisfies(def) {
|
|
44
|
+
type(def).assert(def);
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
instanceOf(expected) {
|
|
48
|
+
if (!(this.ctx.actual instanceof expected)) {
|
|
49
|
+
throwAssertionError({
|
|
50
|
+
ctx: this.ctx,
|
|
51
|
+
message: `Expected an instance of ${expected.name} (was ${typeof this.ctx.actual === "object" && this.ctx.actual !== null ?
|
|
52
|
+
this.ctx.actual.constructor.name
|
|
53
|
+
: this.serializedActual})`
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
get snap() {
|
|
59
|
+
// Use variadic args to distinguish undefined being passed explicitly from no args
|
|
60
|
+
const inline = (...args) => {
|
|
61
|
+
const snapName = this.ctx.lastSnapName ?? "snap";
|
|
62
|
+
const expectedSerialized = this.serialize(args[0]);
|
|
63
|
+
if (!args.length || this.ctx.cfg.updateSnapshots) {
|
|
64
|
+
if (this.snapRequiresUpdate(expectedSerialized)) {
|
|
65
|
+
const snapshotArgs = {
|
|
66
|
+
position: caller(),
|
|
67
|
+
serializedValue: this.serializedActual,
|
|
68
|
+
snapFunctionName: snapName
|
|
69
|
+
};
|
|
70
|
+
queueSnapshotUpdate(snapshotArgs);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
// compare as strings, but if match fails, compare again as objects
|
|
75
|
+
// to give a clearer error message. This avoid problems with objects
|
|
76
|
+
// like subtypes of array that do not pass node's deep equality test
|
|
77
|
+
// but serialize to the same value.
|
|
78
|
+
if (printable(args[0]) !== printable(this.unversionedActual))
|
|
79
|
+
assertEquals(expectedSerialized, this.serializedActual, this.ctx);
|
|
80
|
+
}
|
|
81
|
+
return this;
|
|
82
|
+
};
|
|
83
|
+
const toFile = (id, opts) => {
|
|
84
|
+
const expectedSnapshot = getSnapshotByName(this.ctx.position.file, id, opts?.path);
|
|
85
|
+
if (!expectedSnapshot || this.ctx.cfg.updateSnapshots) {
|
|
86
|
+
if (this.snapRequiresUpdate(expectedSnapshot)) {
|
|
87
|
+
updateExternalSnapshot({
|
|
88
|
+
serializedValue: this.serializedActual,
|
|
89
|
+
position: caller(),
|
|
90
|
+
name: id,
|
|
91
|
+
customPath: opts?.path
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else
|
|
96
|
+
assertEquals(expectedSnapshot, this.serializedActual, this.ctx);
|
|
97
|
+
return this;
|
|
98
|
+
};
|
|
99
|
+
return Object.assign(inline, { toFile });
|
|
100
|
+
}
|
|
101
|
+
immediateOrChained() {
|
|
102
|
+
const immediateAssertion = (...args) => {
|
|
103
|
+
let expected;
|
|
104
|
+
if (args.length)
|
|
105
|
+
expected = args[0];
|
|
106
|
+
else {
|
|
107
|
+
if ("defaultExpected" in this.ctx)
|
|
108
|
+
expected = this.ctx.defaultExpected;
|
|
109
|
+
else {
|
|
110
|
+
throw new Error(`Assertion call requires an arg representing the expected value.`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (this.ctx.allowRegex)
|
|
114
|
+
assertEqualOrMatching(expected, this.ctx.actual, this.ctx);
|
|
115
|
+
else
|
|
116
|
+
assertEquals(expected, this.ctx.actual, this.ctx);
|
|
117
|
+
return this;
|
|
118
|
+
};
|
|
119
|
+
return new Proxy(immediateAssertion, {
|
|
120
|
+
get: (target, prop) => this[prop]
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
get throws() {
|
|
124
|
+
const result = callAssertedFunction(this.unversionedActual);
|
|
125
|
+
this.ctx.actual = getThrownMessage(result, this.ctx);
|
|
126
|
+
this.ctx.allowRegex = true;
|
|
127
|
+
this.ctx.defaultExpected = "";
|
|
128
|
+
return this.immediateOrChained();
|
|
129
|
+
}
|
|
130
|
+
throwsAndHasTypeError(matchValue) {
|
|
131
|
+
assertEqualOrMatching(matchValue, getThrownMessage(callAssertedFunction(this.unversionedActual), this.ctx), this.ctx);
|
|
132
|
+
if (!this.ctx.cfg.skipTypes) {
|
|
133
|
+
assertEqualOrMatching(matchValue, new TypeAssertionMapping(data => ({
|
|
134
|
+
actual: data.errors.join("\n")
|
|
135
|
+
})), this.ctx);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
get completions() {
|
|
139
|
+
if (this.ctx.cfg.skipTypes)
|
|
140
|
+
return chainableNoOpProxy;
|
|
141
|
+
this.ctx.actual = new TypeAssertionMapping(data => {
|
|
142
|
+
checkCompletionsForErrors(data.completions);
|
|
143
|
+
return { actual: data.completions };
|
|
144
|
+
});
|
|
145
|
+
this.ctx.lastSnapName = "completions";
|
|
146
|
+
return this.snap;
|
|
147
|
+
}
|
|
148
|
+
get type() {
|
|
149
|
+
if (this.ctx.cfg.skipTypes)
|
|
150
|
+
return chainableNoOpProxy;
|
|
151
|
+
// We need to bind this to return an object with getters
|
|
152
|
+
const self = this;
|
|
153
|
+
return {
|
|
154
|
+
get toString() {
|
|
155
|
+
self.ctx.actual = new TypeAssertionMapping(data => ({
|
|
156
|
+
actual: data.args[0].type
|
|
157
|
+
}));
|
|
158
|
+
return self.immediateOrChained();
|
|
159
|
+
},
|
|
160
|
+
get errors() {
|
|
161
|
+
self.ctx.actual = new TypeAssertionMapping(data => ({
|
|
162
|
+
actual: data.errors.join("\n")
|
|
163
|
+
}));
|
|
164
|
+
self.ctx.allowRegex = true;
|
|
165
|
+
return self.immediateOrChained();
|
|
166
|
+
},
|
|
167
|
+
get completions() {
|
|
168
|
+
return self.completions;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const checkCompletionsForErrors = (completions) => {
|
|
174
|
+
if (typeof completions === "string")
|
|
175
|
+
throw new Error(completions);
|
|
176
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const await1K: (fn: () => Promise<void>) => Promise<void>;
|