Skip to content

Commit d374472

Browse files
Merge branch 'feat/native-to-be-empty' into feat/native-to-be-visible
2 parents abf480a + e7424fd commit d374472

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

packages/core/src/lib/helpers/TypeFactories.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ArrayAssertion } from "../ArrayAssertion";
22
import { Assertion, Constructor } from "../Assertion";
33
import { BooleanAssertion } from "../BooleanAssertion";
44
import { DateAssertion } from "../DateAssertion";
5+
import { ErrorAssertion } from "../ErrorAssertion";
56
import { type AnyFunction, FunctionAssertion } from "../FunctionAssertion";
67
import { NumberAssertion } from "../NumberAssertion";
78
import { ObjectAssertion } from "../ObjectAssertion";
@@ -47,6 +48,10 @@ export interface StaticTypeFactories {
4748
* A `Date` TypeFactory.
4849
*/
4950
Date: TypeFactory<Date, DateAssertion>;
51+
/**
52+
* An `Error` TypeFactory.
53+
*/
54+
Error: TypeFactory<Error, ErrorAssertion<Error>>;
5055
/**
5156
* A `function` TypeFactory.
5257
*/
@@ -71,6 +76,20 @@ export interface StaticTypeFactories {
7176
* @param innerType the TypeFactory for the array type
7277
*/
7378
array<T>(innerType?: TypeFactory<T, Assertion<T>>): TypeFactory<T[], ArrayAssertion<T>>;
79+
/**
80+
* Creates an `Error` TypeFactory for a specific error constructor.
81+
*
82+
* @example
83+
* ```
84+
* class CustomError extends Error { ... }
85+
*
86+
* TypeFactories.error(CustomError); // a `CustomError` error factory
87+
* ```
88+
*
89+
* @typeParam T the type of the error constructor
90+
* @param Type the error constructor
91+
*/
92+
error<T extends Error>(Type: Constructor<T>): TypeFactory<T, ErrorAssertion<T>>;
7493
/**
7594
* Creates a TypeFactory for an instance of the given constructor.
7695
*
@@ -114,6 +133,11 @@ export const TypeFactories: Readonly<StaticTypeFactories> = {
114133
predicate: (value): value is Date => value instanceof Date,
115134
typeName: Date.name,
116135
},
136+
Error: {
137+
Factory: ErrorAssertion,
138+
predicate: (value): value is Error => value instanceof Error,
139+
typeName: Error.name,
140+
},
117141
Function: {
118142
Factory: FunctionAssertion,
119143
predicate: (value): value is AnyFunction => typeof value === "function",
@@ -139,6 +163,13 @@ export const TypeFactories: Readonly<StaticTypeFactories> = {
139163
typeName: "array",
140164
};
141165
},
166+
error<T extends Error>(Type: Constructor<T>) {
167+
return {
168+
Factory: ErrorAssertion,
169+
predicate: (value): value is T => value instanceof Type,
170+
typeName: Type.name,
171+
};
172+
},
142173
instanceOf<T>(type: Constructor<T>) {
143174
return {
144175
Factory: Assertion,

packages/core/test/lib/helpers/TypeFactories.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import { TypeFactories } from "../../../src/lib/helpers/TypeFactories";
22

33
import assert from "assert";
44

5+
class CustomError extends Error {
6+
7+
public constructor(message?: string) {
8+
super(message);
9+
10+
this.name = this.constructor.name;
11+
Error.captureStackTrace(this, this.constructor);
12+
Object.setPrototypeOf(this, CustomError.prototype);
13+
}
14+
}
15+
516
describe("[Unit] TypeFactories.test.ts", () => {
617
describe(".predicate", () => {
718
describe("#Boolean", () => {
@@ -42,6 +53,24 @@ describe("[Unit] TypeFactories.test.ts", () => {
4253
});
4354
});
4455

56+
describe("#Error", () => {
57+
context("when the value is an Error", () => {
58+
it("returns true", () => {
59+
const result = TypeFactories.Error.predicate(new Error("foo"));
60+
61+
assert.equal(result, true);
62+
});
63+
});
64+
65+
context("when the value is not an Error", () => {
66+
it("returns false", () => {
67+
const result = TypeFactories.Error.predicate("foo");
68+
69+
assert.equal(result, false);
70+
});
71+
});
72+
});
73+
4574
describe("#Function", () => {
4675
context("when the value is a function", () => {
4776
it("returns true", () => {
@@ -143,6 +172,24 @@ describe("[Unit] TypeFactories.test.ts", () => {
143172
});
144173
});
145174

175+
describe(".error", () => {
176+
context("when the value is an instace o the error type", () => {
177+
it("returns true", () => {
178+
const result = TypeFactories.error(CustomError).predicate(new CustomError("foo"));
179+
180+
assert.equal(result, true);
181+
});
182+
});
183+
184+
context("when the value is not an instace o the error type", () => {
185+
it("returns false", () => {
186+
const result = TypeFactories.error(CustomError).predicate(new Error("foo"));
187+
188+
assert.equal(result, false);
189+
});
190+
});
191+
});
192+
146193
describe(".instanceOf", () => {
147194
context("when the value is an instance of the passed type", () => {
148195
it("returns true", () => {

0 commit comments

Comments
 (0)