@autometa/assertions 1.0.0-rc.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 +21 -0
- package/README.md +19 -0
- package/dist/__tests__/ensure.test-d.d.ts +67 -0
- package/dist/__tests__/helpers/matcher-context.d.ts +18 -0
- package/dist/assertion-error.d.ts +13 -0
- package/dist/core/constants.d.ts +4 -0
- package/dist/core/context.d.ts +14 -0
- package/dist/core/messages.d.ts +10 -0
- package/dist/core/predicates.d.ts +5 -0
- package/dist/ensure.d.ts +33 -0
- package/dist/index.cjs +815 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +809 -0
- package/dist/index.js.map +1 -0
- package/dist/matchers/__tests__/http.note.d.ts +6 -0
- package/dist/matchers/collections.d.ts +6 -0
- package/dist/matchers/equality.d.ts +4 -0
- package/dist/matchers/http.d.ts +57 -0
- package/dist/matchers/instance.d.ts +2 -0
- package/dist/matchers/nullish.d.ts +4 -0
- package/dist/matchers/numeric.d.ts +6 -0
- package/dist/matchers/truthiness.d.ts +3 -0
- package/dist/plugins/runtime-assertions-plugin.d.ts +140 -0
- package/dist/plugins.d.ts +50 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) undefined Ben Aherne
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @autometa/assertions
|
|
2
|
+
|
|
3
|
+
Autometa's assertion toolkit built around the `ensure(value)` entry point. The package provides a fluent matcher chain with TypeScript-aware narrowing so you can write expressive assertions across runners without depending on Jest/Vitest globals.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { ensure } from "@autometa/assertions";
|
|
7
|
+
|
|
8
|
+
const result: string | undefined = fetchName();
|
|
9
|
+
|
|
10
|
+
const narrowed = ensure(result, { label: "result" })
|
|
11
|
+
.toBeDefined()
|
|
12
|
+
.toBeInstanceOf(String)
|
|
13
|
+
.value;
|
|
14
|
+
|
|
15
|
+
ensure({ count: 1, name: "Foo" }).toBeObjectContaining({ name: "Foo" });
|
|
16
|
+
ensure([1, 2, 3]).toBeArrayContaining([2]);
|
|
17
|
+
ensure(new Set([1, 2, 3])).toBeIterableContaining([3]);
|
|
18
|
+
ensure("text").toHaveLength(4);
|
|
19
|
+
```
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { EnsureChain, EnsureNegatedChain } from "../ensure";
|
|
2
|
+
type Assert<T extends true> = T;
|
|
3
|
+
type BaseValue = EnsureChain<string>["value"];
|
|
4
|
+
type NegatedBaseValue = EnsureNegatedChain<boolean>["value"];
|
|
5
|
+
export type _baseValue = Assert<[BaseValue] extends [string] ? true : false>;
|
|
6
|
+
export type _negatedBaseValue = Assert<[NegatedBaseValue] extends [boolean] ? true : false>;
|
|
7
|
+
type DefinedChain = ReturnType<EnsureChain<string | undefined>["toBeDefined"]>;
|
|
8
|
+
type DefinedValue = DefinedChain["value"];
|
|
9
|
+
export type _definedNarrows = Assert<[DefinedValue] extends [string] ? true : false>;
|
|
10
|
+
export type _definedEqualsString = Assert<[string] extends [DefinedValue] ? true : false>;
|
|
11
|
+
type NegatedDefinedChain = ReturnType<EnsureNegatedChain<string | undefined>["toBeDefined"]>;
|
|
12
|
+
type NegatedDefinedValue = NegatedDefinedChain["value"];
|
|
13
|
+
export type _negatedDefinedRetains = Assert<[
|
|
14
|
+
NegatedDefinedValue
|
|
15
|
+
] extends [string | undefined] ? true : false>;
|
|
16
|
+
export type _negatedDefinedAllowsUndefined = Assert<[
|
|
17
|
+
string | undefined
|
|
18
|
+
] extends [NegatedDefinedValue] ? true : false>;
|
|
19
|
+
type InstanceNarrows = EnsureChain<Error | TypeError> extends {
|
|
20
|
+
toBeInstanceOf(ctor: typeof TypeError): {
|
|
21
|
+
value: TypeError;
|
|
22
|
+
};
|
|
23
|
+
} ? true : false;
|
|
24
|
+
export type _instanceNarrows = Assert<InstanceNarrows>;
|
|
25
|
+
type NegatedInstanceRetains = EnsureNegatedChain<Error | TypeError> extends {
|
|
26
|
+
toBeInstanceOf(ctor: typeof TypeError): {
|
|
27
|
+
value: Error | TypeError;
|
|
28
|
+
};
|
|
29
|
+
} ? true : false;
|
|
30
|
+
export type _negatedInstanceRetains = Assert<NegatedInstanceRetains>;
|
|
31
|
+
type ArrayContainingNarrows = EnsureChain<readonly number[] | Set<number>> extends {
|
|
32
|
+
toBeArrayContaining(expected: readonly number[]): {
|
|
33
|
+
value: readonly number[];
|
|
34
|
+
};
|
|
35
|
+
} ? true : false;
|
|
36
|
+
export type _arrayContainingNarrows = Assert<ArrayContainingNarrows>;
|
|
37
|
+
type NegatedArrayContainingRetains = EnsureNegatedChain<readonly number[] | Set<number>> extends {
|
|
38
|
+
toBeArrayContaining(expected: readonly number[]): {
|
|
39
|
+
value: readonly number[] | Set<number>;
|
|
40
|
+
};
|
|
41
|
+
} ? true : false;
|
|
42
|
+
export type _negatedArrayContainingRetains = Assert<NegatedArrayContainingRetains>;
|
|
43
|
+
type IterableContainingNarrows = EnsureChain<Iterable<number> | number> extends {
|
|
44
|
+
toBeIterableContaining(expected: readonly unknown[]): {
|
|
45
|
+
value: Iterable<number>;
|
|
46
|
+
};
|
|
47
|
+
} ? true : false;
|
|
48
|
+
export type _iterableContainingNarrows = Assert<IterableContainingNarrows>;
|
|
49
|
+
type NegatedIterableContainingRetains = EnsureNegatedChain<Iterable<number> | number> extends {
|
|
50
|
+
toBeIterableContaining(expected: readonly unknown[]): {
|
|
51
|
+
value: Iterable<number> | number;
|
|
52
|
+
};
|
|
53
|
+
} ? true : false;
|
|
54
|
+
export type _negatedIterableContainingRetains = Assert<NegatedIterableContainingRetains>;
|
|
55
|
+
type LengthNarrows = EnsureChain<string | number> extends {
|
|
56
|
+
toHaveLength(expected: number): {
|
|
57
|
+
value: string;
|
|
58
|
+
};
|
|
59
|
+
} ? true : false;
|
|
60
|
+
export type _lengthNarrows = Assert<LengthNarrows>;
|
|
61
|
+
type NegatedLengthRetains = EnsureNegatedChain<string | number> extends {
|
|
62
|
+
toHaveLength(expected: number): {
|
|
63
|
+
value: string | number;
|
|
64
|
+
};
|
|
65
|
+
} ? true : false;
|
|
66
|
+
export type _negatedLengthRetains = Assert<NegatedLengthRetains>;
|
|
67
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { FailureDetails, MatcherContext } from "../../core/context";
|
|
2
|
+
export declare class TestMatcherError extends Error {
|
|
3
|
+
readonly matcher: string;
|
|
4
|
+
readonly details: FailureDetails;
|
|
5
|
+
constructor(matcher: string, details: FailureDetails);
|
|
6
|
+
}
|
|
7
|
+
export interface TestMatcherContext<T> extends MatcherContext<T> {
|
|
8
|
+
readonly failCalls: readonly TestMatcherCall[];
|
|
9
|
+
}
|
|
10
|
+
export interface TestMatcherCall {
|
|
11
|
+
readonly matcher: string;
|
|
12
|
+
readonly details: FailureDetails;
|
|
13
|
+
}
|
|
14
|
+
export interface CreateMatcherContextOptions {
|
|
15
|
+
readonly negated?: boolean;
|
|
16
|
+
readonly label?: string;
|
|
17
|
+
}
|
|
18
|
+
export declare function createMatcherContext<T>(value: T, options?: CreateMatcherContextOptions): TestMatcherContext<T>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface EnsureErrorDetails {
|
|
2
|
+
readonly matcher: string;
|
|
3
|
+
readonly message: string;
|
|
4
|
+
readonly actual?: unknown;
|
|
5
|
+
readonly expected?: unknown;
|
|
6
|
+
readonly receivedLabel?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare class EnsureError extends Error {
|
|
9
|
+
readonly matcher: string;
|
|
10
|
+
readonly actual?: unknown;
|
|
11
|
+
readonly expected?: unknown;
|
|
12
|
+
constructor(details: EnsureErrorDetails);
|
|
13
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface FailureDetails {
|
|
2
|
+
readonly message: string;
|
|
3
|
+
readonly actual?: unknown;
|
|
4
|
+
readonly expected?: unknown;
|
|
5
|
+
}
|
|
6
|
+
export interface MatcherState<T> {
|
|
7
|
+
readonly value: T;
|
|
8
|
+
readonly label?: string;
|
|
9
|
+
readonly negated: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface MatcherContext<T> extends MatcherState<T> {
|
|
12
|
+
fail(matcher: string, details: FailureDetails): never;
|
|
13
|
+
}
|
|
14
|
+
export declare function shouldFail(pass: boolean, negated: boolean): boolean;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface FailureMessageOptions {
|
|
2
|
+
readonly expected?: unknown;
|
|
3
|
+
readonly actual?: unknown;
|
|
4
|
+
readonly diff?: string | undefined;
|
|
5
|
+
readonly extra?: readonly (string | undefined)[];
|
|
6
|
+
readonly actualLabel?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function buildFailureMessage(matcher: string, baseMessage: string, options?: FailureMessageOptions): string;
|
|
9
|
+
export declare function formatDiff(expected: unknown, actual: unknown): string | undefined;
|
|
10
|
+
export declare function formatMissingList(title: string, values: readonly unknown[]): string;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function isRecord(candidate: unknown): candidate is Record<PropertyKey, unknown>;
|
|
2
|
+
export declare function isIterable(candidate: unknown): candidate is Iterable<unknown>;
|
|
3
|
+
export declare function hasLengthProperty(candidate: unknown): candidate is {
|
|
4
|
+
length: number;
|
|
5
|
+
};
|
package/dist/ensure.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export interface EnsureOptions {
|
|
2
|
+
readonly label?: string;
|
|
3
|
+
}
|
|
4
|
+
type Toggle<Negated extends boolean> = Negated extends true ? false : true;
|
|
5
|
+
interface EnsureChainInternal<T, Negated extends boolean> {
|
|
6
|
+
readonly value: T;
|
|
7
|
+
readonly not: EnsureChainInternal<T, Toggle<Negated>>;
|
|
8
|
+
toBe(expected: T): EnsureChainInternal<T, Negated>;
|
|
9
|
+
toEqual(expected: unknown): EnsureChainInternal<T, Negated>;
|
|
10
|
+
toStrictEqual(expected: unknown): EnsureChainInternal<T, Negated>;
|
|
11
|
+
toBeDefined(): EnsureChainInternal<Negated extends true ? T : NonNullable<T>, Negated>;
|
|
12
|
+
toBeUndefined(): EnsureChainInternal<Negated extends true ? T : undefined, Negated>;
|
|
13
|
+
toBeNull(): EnsureChainInternal<Negated extends true ? T : null, Negated>;
|
|
14
|
+
toBeTruthy(): EnsureChainInternal<T, Negated>;
|
|
15
|
+
toBeFalsy(): EnsureChainInternal<T, Negated>;
|
|
16
|
+
toBeGreaterThan(expected: number): EnsureChainInternal<Negated extends true ? T : Extract<T, number>, Negated>;
|
|
17
|
+
toBeGreaterThanOrEqual(expected: number): EnsureChainInternal<Negated extends true ? T : Extract<T, number>, Negated>;
|
|
18
|
+
toBeLessThan(expected: number): EnsureChainInternal<Negated extends true ? T : Extract<T, number>, Negated>;
|
|
19
|
+
toBeLessThanOrEqual(expected: number): EnsureChainInternal<Negated extends true ? T : Extract<T, number>, Negated>;
|
|
20
|
+
toBeCloseTo(expected: number, precision?: number): EnsureChainInternal<Negated extends true ? T : Extract<T, number>, Negated>;
|
|
21
|
+
toBeInstanceOf<Ctor extends abstract new (...args: never[]) => unknown>(ctor: Ctor): EnsureChainInternal<Negated extends true ? T : InstanceType<Ctor>, Negated>;
|
|
22
|
+
toBeObjectContaining<Shape extends Record<PropertyKey, unknown>>(shape: Shape): EnsureChainInternal<T, Negated>;
|
|
23
|
+
toBeArrayContaining(expected: readonly unknown[]): EnsureChainInternal<Negated extends true ? T : Extract<T, readonly unknown[]>, Negated>;
|
|
24
|
+
toContainEqual(expected: unknown): EnsureChainInternal<Negated extends true ? T : Extract<T, readonly unknown[]>, Negated>;
|
|
25
|
+
toBeIterableContaining(expected: readonly unknown[]): EnsureChainInternal<Negated extends true ? T : Extract<T, Iterable<unknown>>, Negated>;
|
|
26
|
+
toHaveLength(expected: number): EnsureChainInternal<Negated extends true ? T : Extract<T, {
|
|
27
|
+
length: number;
|
|
28
|
+
}>, Negated>;
|
|
29
|
+
}
|
|
30
|
+
export type EnsureChain<T> = EnsureChainInternal<T, false>;
|
|
31
|
+
export type EnsureNegatedChain<T> = EnsureChainInternal<T, true>;
|
|
32
|
+
export declare function ensure<T>(value: T, options?: EnsureOptions): EnsureChain<T>;
|
|
33
|
+
export {};
|