Skip to content

Commit

Permalink
feat(testing/asserts): add assertNotInstanceOf (#2530)
Browse files Browse the repository at this point in the history
  • Loading branch information
iuioiua committed Aug 18, 2022
1 parent 51bf5f7 commit 30f771d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions testing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pretty-printed diff of failing assertion.
`expected`, according to a given `epsilon` _(defaults to `1e-7`)_
- `assertInstanceOf()` - Make an assertion that `actual` is an instance of
`expectedType`.
- `assertNotInstanceOf()` - Make an assertion that `actual` is not an instance
of `expectedType`.
- `assertStringIncludes()` - Make an assertion that `actual` includes
`expected`.
- `assertMatch()` - Make an assertion that `actual` match RegExp `expected`.
Expand Down
12 changes: 12 additions & 0 deletions testing/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,18 @@ export function assertInstanceOf<T extends AnyConstructor>(
assert(actual instanceof expectedType, msg);
}

/**
* Make an assertion that `obj` is not an instance of `type`.
* If so, then throw.
*/
export function assertNotInstanceOf<T extends AnyConstructor>(
actual: unknown,
unexpectedType: T,
msg = `Expected object to not be an instance of "${typeof unexpectedType}"`,
) {
assertFalse(actual instanceof unexpectedType, msg);
}

/**
* Make an assertion that actual is not null or undefined.
* If not then throw.
Expand Down
10 changes: 10 additions & 0 deletions testing/asserts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
assertIsError,
assertMatch,
assertNotEquals,
assertNotInstanceOf,
assertNotMatch,
assertNotStrictEquals,
assertObjectMatch,
Expand Down Expand Up @@ -1424,6 +1425,15 @@ Deno.test({
},
});

Deno.test({
name: "assertNotInstanceOf",
fn() {
assertNotInstanceOf("not a number", Number);
assertNotInstanceOf(42, String);
assertNotInstanceOf(new URL("http://example.com"), Boolean);
},
});

Deno.test({
name: "assert* functions with specified type parameter",
fn(): void {
Expand Down

0 comments on commit 30f771d

Please sign in to comment.