deno_std
deno_std copied to clipboard
testing/asserts: `assertNotInstanceOf()`
Is your feature request related to a problem? Please describe.
I would like to assert that an object is NOT an instance of a specific type. (i.e. the complement to #2025).
Describe the solution you'd like
An assertNotInstanceOf to complement assertInstanceOf.
Describe alternatives you've considered
I've currently taken the source for the existing assertInstanceOf and transformed it into an assertNotInstanceOf but IMO it would be more ideal to not need to bring my own but already have this in deno_std:
// deno-lint-ignore no-explicit-any
type AnyConstructor = new (...args: any[]) => any;
/**
* Make an assertion that `actual` is not an instance of `expectedType`.
* If it is an instance of `expectedType` then throw.
*/
export function assertNotInstanceOf<T extends AnyConstructor>(
actual: unknown,
expectedType: T,
msg = `Expected object to not be an instance of "${expectedType.name}".`
): void {
assert(!(actual instanceof expectedType), msg);
}