@ark/attest 0.26.0 → 0.27.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.
@@ -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
- actual: unknown;
17
+ versionableActual: VersionableActual;
16
18
  originalAssertedValue: unknown;
17
19
  cfg: AttestConfig;
18
20
  allowRegex: boolean;
@@ -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
- actual: value,
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;
@@ -53,6 +58,7 @@ export type comparableValueAssertion<expected, kind extends AssertionKind> = {
53
58
  completions: (value?: Completions) => void;
54
59
  satisfies: <def>(def: type.validate<def>) => nextAssertions<kind>;
55
60
  unknown: Omit<comparableValueAssertion<unknown, kind>, "unknown">;
61
+ unwrap: (opts?: UnwrapOptions) => unknown;
56
62
  };
57
63
  export type TypeAssertionsRoot = {
58
64
  type: TypeAssertionProps;
@@ -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.ctx.actual instanceof TypeAssertionMapping) {
21
- return this.ctx.actual.fn(this.ctx.typeRelationshipAssertionEntries[0][1], this.ctx).actual;
17
+ if (this.versionableActual instanceof TypeAssertionMapping) {
18
+ return this.versionableActual.fn(this.ctx.typeRelationshipAssertionEntries[0][1], this.ctx).actual;
22
19
  }
23
- return this.ctx.actual;
20
+ return this.versionableActual;
21
+ }
22
+ get versionableActual() {
23
+ return this.ctx.versionableActual;
24
24
  }
25
25
  get serializedActual() {
26
- return this.serialize(this.unversionedActual);
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.ctx.actual, this.ctx);
46
+ assertEquals(expected, this.versionableActual, this.ctx);
43
47
  return this;
44
48
  }
45
49
  satisfies(def) {
46
- assertSatisfies(type.raw(def), this.ctx.actual, this.ctx);
50
+ assertSatisfies(type.raw(def), this.versionableActual, this.ctx);
47
51
  return this;
48
52
  }
49
53
  instanceOf(expected) {
50
- if (!(this.ctx.actual instanceof expected)) {
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.ctx.actual === "object" && this.ctx.actual !== null ?
54
- this.ctx.actual.constructor.name
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 = this.serialize(args[0]);
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 = {
@@ -113,9 +118,9 @@ export class ChainableAssertions {
113
118
  }
114
119
  }
115
120
  if (this.ctx.allowRegex)
116
- assertEqualOrMatching(expected, this.ctx.actual, this.ctx);
121
+ assertEqualOrMatching(expected, this.versionableActual, this.ctx);
117
122
  else
118
- assertEquals(expected, this.ctx.actual, this.ctx);
123
+ assertEquals(expected, this.versionableActual, this.ctx);
119
124
  return this;
120
125
  };
121
126
  return new Proxy(immediateAssertion, {
@@ -124,7 +129,7 @@ export class ChainableAssertions {
124
129
  }
125
130
  get throws() {
126
131
  const result = callAssertedFunction(this.unversionedActual);
127
- this.ctx.actual = getThrownMessage(result, this.ctx);
132
+ this.ctx.versionableActual = getThrownMessage(result, this.ctx);
128
133
  this.ctx.allowRegex = true;
129
134
  this.ctx.defaultExpected = "";
130
135
  return this.immediateOrChained();
@@ -140,7 +145,7 @@ export class ChainableAssertions {
140
145
  get completions() {
141
146
  if (this.ctx.cfg.skipTypes)
142
147
  return chainableNoOpProxy;
143
- this.ctx.actual = new TypeAssertionMapping(data => {
148
+ this.ctx.versionableActual = new TypeAssertionMapping(data => {
144
149
  if (typeof data.completions === "string") {
145
150
  // if the completions were ambiguously defined, e.g. two string
146
151
  // literals with the same value, they are writen as an error
@@ -159,14 +164,14 @@ export class ChainableAssertions {
159
164
  const self = this;
160
165
  return {
161
166
  get toString() {
162
- self.ctx.actual = new TypeAssertionMapping(data => ({
167
+ self.ctx.versionableActual = new TypeAssertionMapping(data => ({
163
168
  actual: formatTypeString(data.args[0].type)
164
169
  }));
165
170
  self.ctx.allowRegex = true;
166
171
  return self.immediateOrChained();
167
172
  },
168
173
  get errors() {
169
- self.ctx.actual = new TypeAssertionMapping(data => ({
174
+ self.ctx.versionableActual = new TypeAssertionMapping(data => ({
170
175
  actual: data.errors.join("\n")
171
176
  }));
172
177
  self.ctx.allowRegex = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ark/attest",
3
- "version": "0.26.0",
3
+ "version": "0.27.0",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "David Blass",
@@ -36,9 +36,9 @@
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.22.0",
40
- "@ark/util": "0.22.0",
41
- "arktype": "2.0.0-rc.22"
39
+ "arktype": "2.0.0-rc.23",
40
+ "@ark/util": "0.23.0",
41
+ "@ark/fs": "0.23.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "typescript": "5.7.0-beta"