-
Notifications
You must be signed in to change notification settings - Fork 582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat request: assertIsNotError
#2444
Comments
assertIsNotError
@harrysolovay There is assertIsError. Is it your want ? export function assertIsError<E extends Error = Error>(
error: unknown,
// deno-lint-ignore no-explicit-any
ErrorClass?: new (...args: any[]) => E,
msgIncludes?: string,
msg?: string,
): asserts error is E {
if (error instanceof Error === false) {
throw new AssertionError(`Expected "error" to be an Error object.`);
}
if (ErrorClass && !(error instanceof ErrorClass)) {
msg = `Expected error to be instance of "${ErrorClass.name}", but was "${
typeof error === "object" ? error?.constructor?.name : "[not an object]"
}"${msg ? `: ${msg}` : "."}`;
throw new AssertionError(msg);
}
if (
msgIncludes && (!(error instanceof Error) ||
!stripColor(error.message).includes(stripColor(msgIncludes)))
) {
msg = `Expected error message to include "${msgIncludes}", but got "${
error instanceof Error ? error.message : "[not an Error]"
}"${msg ? `: ${msg}` : "."}`;
throw new AssertionError(msg);
}
} |
As I specified above, this util would be...
|
In my mind about your above case is const theThingOrError = tryGetThing();
if (!(theThingOrError instanceof Error)) {
// `theThingOrError` is not an error within this scope
}
// Equal
const theThingOrError = tryGetThing();
if ((theThingOrError instanceof Error)) {
throw new Error()
}
// `theThingOrError` is not an error within this scope
// Equal
const theThingOrError = tryGetThing();
assertIsError(theThingOrError)
//`theThingOrError` is not an error within this scope
// Or
const theThingOrError = tryGetThing();
if (! assertIsNotError(theThingOrError)) {
//`theThingOrError` is not an error within this scope
} Is right ? |
export function assertNotInstanceOf<T, U>(
actual: T,
Expected: { new (...args: never): unknown; prototype: U },
message?: string,
): asserts actual is Exclude<T, U> {
if (actual instanceof Expected) {
throw new Error(
message ?? `Expected object to be not an instance of ${Expected.name}`,
);
}
} |
|
Is anyone currently working on this? |
|
@iuioiua while I like that you've "deliberately kept simple"... the lack of narrowing is (imo) not ideal. I'd recommend reworking as follows. - export function assertNotInstanceOf<T extends AnyConstructor>(
+ export function assertNotInstanceOf<A, T>(
- actual: unknown,
+ actual: A,
- unexpectedType: T,
+ unexpectedType: new(...args: any[]) => T,
msg = `Expected object to not be an instance of "${typeof unexpectedType}"`,
- ) {
+ ): asserts actual is Exclude<A, T> {
assertFalse(actual instanceof unexpectedType, msg);
} Without the diff noise: export function assertNotInstanceOf<A, T>(
actual: A,
unexpectedType: new(...args: any[]) => T,
msg = `Expected object to not be an instance of "${typeof unexpectedType}"`,
): asserts actual is Exclude<A, T> {
assertFalse(actual instanceof unexpectedType, msg);
} |
I agree, @harrysolovay. PR submitted. |
done in #2558 |
Feature request: an
assertIsNotError
util to compliment assertIsError assertion. I frequently return (not throw) errors.To test such function requires us to perform checks to narrow the fn's result union type to exclude its error-based members.
Ideally I could utilize a built-in assertion instead.
The text was updated successfully, but these errors were encountered: