The ExtendedExpected interface defines the available assertion methods.
Asserts a function to throw an error.
Unlike the epoynmous method from @std/expect, this matcher accepts a second argument that can be either a string or a regular expression when the first argument is a Error class or instance.
In this case, the behavior is the same as assertIsError where you can both check the error type and message.
import { expect } from "@in/test" const throws = () => { throw new Error("Expected error") } expect(throws).toThrow(Error) expect(throws).toThrow("Expected error") expect(throws).toThrow(Error, /Expected/)
Asserts a value matches the given predicate.
import { expect } from "@in/test" expect("foo").toSatisfy((value:string) => value.length > 1)
Asserts a value is of a given type (using typeof operator).
Note that null is not considered of type "object" unless nullable option is set to true.
import { expect } from "@in/test" expect("foo").toBeType("string") expect({}).toBeType("object") expect(null).toBeType("object", { nullable: true }) expect(null).not.toBeType("object")
toHaveDescribedProperty: (key: PropertyKey,expected: PropertyDescriptor,) => unknown
Asserts a property matches a given descriptor (using Object.getOwnPropertyDescriptor).
import { expect } from "@in/test" const foo = Object.defineProperties({}, { bar: { value: true, writable: false, enumerable: true } }) expect(foo).toHaveDescribedProperty("bar", { writable: false, enumerable: true })
toHaveImmutableProperty: (key: PropertyKey,testValue?: unknown,) => unknown
Asserts a writable property is immutable (i.e. setting its value does not throw but does not change its value either).
Note that it will actually proceed to assign testValue and restore it to original value after test.
import { expect } from "@in/test" const foo = new (class { #bar = true get bar() { return this.#bar } set bar(_:unknown) { return } })() expect(foo).toHaveImmutableProperty("bar")
toBeIterable: () => unknown
Assert an object is iterable (checking Symbol.iterator presence).
import { expect } from "@in/test" expect([]).toBeIterable() expect(new Map()).toBeIterable() expect(new Set()).toBeIterable()
toBeSealed: () => unknown
Assert an object is sealed (using Object.isSealed).
import { expect } from "@in/test" expect(Object.seal({})).toBeSealed()
toBeFrozen: () => unknown
Assert an object is frozen (using Object.isFrozen).
import { expect } from "@in/test" expect(Object.freeze({})).toBeFrozen()
toBeExtensible: () => unknown
Assert an object is extensible (using Object.isExtensible).
import { expect } from "@in/test" expect(Object.preventExtensions({})).not.toBeExtensible()
toBeShallowCopyOf: (expected?: Iterable<unknown> | record) => unknown
Asserts an object is a shallow copy (i.e. its content is identical but reference is not).
import { expect } from "@in/test" const foo = { bar: true } expect({...foo}).toBeShallowCopyOf(foo)
toBeEmpty: () => unknown
Asserts an iterable is empty.
import { expect } from "@in/test" expect([]).toBeEmpty() expect(new Map()).toBeEmpty() expect(new Set()).toBeEmpty()
toBeSorted: (compare?: Arg<Array<unknown>["sort"]>) => unknown
Asserts an iterable to be sorted.
import { expect } from "@in/test" expect([1, 2, 3]).toBeSorted()
toBeReverseSorted: (compare?: Arg<Array<unknown>["sort"]>) => unknown
Asserts an iterable to be reverse sorted.
import { expect } from "@in/test" expect([3, 2, 1]).toBeReverseSorted()
toBeOneOf: (values: Iterable<unknown>) => unknown
Asserts a value is one of expected value.
import { expect } from "@in/test" expect("foo").toBeOneOf(["foo", "bar"])
toBeWithin: () => unknown
Asserts a number is with a given range.
import { expect } from "@in/test" expect(Math.PI).toBeWithin([3, 4])
toBeFinite: () => unknown
Asserts a number to be finite.
import { expect } from "@in/test" expect(1).toBeFinite() expect(Infinity).not.toBeFinite()
toBeParseableJSON: (reviver?: Arg<JSON["parse"], 1>) => unknown
Asserts a string to be parseable JSON.
import { expect } from "@in/test" expect('{"foo":"bar"}').toBeParseableJSON()
toBeBase64: () => unknown
Asserts a string is a valid base64 string.
import { expect } from "@in/test" expect(btoa("foo")).toBeBase64()
toRespondWithStatus: (status: ) => unknown
Asserts a response to have a given status code.
Note that Response.body.cancel() is automatically called to prevent leaks.
If you need to perform more assertions on a response, it is advised use separate matchers instead.
import { expect, Status } from "@in/test" expect(new Response(null, { status: Status.OK })).toRespondWithStatus(Status.OK) expect(new Response(null, { status: Status.OK })).toRespondWithStatus([Status.OK, Status.Created]) expect(new Response(null, { status: Status.OK })).toRespondWithStatus("2XX")
toBeHashed: (algorithm: string) => unknown
Asserts a string is hashed by specified algorithm.
Algorithm may be either lowercase or uppercase, and contains dashes or not (e.g. sha256, SHA256 and SHA-256 will all be treated as the same).
Will throw if an unknown algorithm is specified.
Please note that it only checks whether it could be a valid output of specified hash algorithm.
import { expect, Status } from "@in/test" expect("$2a$12$lpGSoVPZ8erLDF93Sqyic.U73v0/w0owPb3dIP9goO7iC5Wp/I8OG").toBeHashed("bcrypt")
toBeDate: () => unknown
Asserts a value is a valid date (using Date).
import { expect, Status } from "@in/test" expect("2024-07-13T20:30:57.958Z").toBeDate() expect(new Date()).toBeDate()
Asserts a date is before another date.
import { expect, Status } from "@in/test" expect(new Date(Date.now() - 5000)).toBePast()
toBeFuture: (date?: ) => unknown
Asserts a date is after another date.
import { expect, Status } from "@in/test" expect(new Date(Date.now() + 5000)).toBeFuture()
not: IsAsync extends true ? Async<ExtendedExpected<true>> : ExtendedExpected<false>
The negation object that allows chaining negated assertions.
resolves: Async<ExtendedExpected<true>>
The object that allows chaining assertions with async functions that are expected to resolve to a value.
rejects: Async<ExtendedExpected<true>>
The object that allows chaining assertions with async functions that are expected to throw an error.
toBeEmail(): () => unknown
Asserts a string is a valid email address (using https://pdw.ex-parrot.com/Mail-RFC822-Address.html).
import { expect } from "@in/test" expect("foo@example.com").toBeEmail() expect("foo+bar@example.com").toBeEmail()
toBeUrl(): () => unknown
Asserts a string is a valid url (using URL).
import { expect } from "@in/test" expect("https://xr.new").toBeUrl()