Interface Expected<IsAsync>
not: IsAsync extends true
? Async<Expected<true>>
: Expected<false>;
rejects: Async<Expected<true>>;
resolves: Async<Expected<true>>;
toEqualBook: ((expected: unknown) => ExtendMatchResult);
lastCalledWith(...expected: unknown[]): void;
lastReturnedWith(expected: unknown): void;
nthCalledWith(nth: number, ...expected: unknown[]): void;
nthReturnedWith(nth: number, expected: unknown): void;
toBe(expected: unknown): void;
toBeCalled(): void;
toBeCalledTimes(expected: number): void;
toBeCalledWith(...expected: unknown[]): void;
toBeCloseTo(candidate: number, tolerance?: number): void;
toBeDefined(): void;
toBeFalsy(): void;
toBeGreaterThan(expected: number): void;
toBeGreaterThanOrEqual(expected: number): void;
toBeInstanceOf<T>(expected: T): void;
toBeLessThan(expected: number): void;
toBeLessThanOrEqual(expected: number): void;
toBeNaN(): void;
toBeNull(): void;
toBeTruthy(): void;
toBeUndefined(): void;
toContain(expected: unknown): void;
toContainEqual(expected: unknown): void;
toEqual(expected: unknown): void;
toHaveBeenCalled(): void;
toHaveBeenCalledTimes(expected: number): void;
toHaveBeenCalledWith(...expected: unknown[]): void;
toHaveBeenLastCalledWith(...expected: unknown[]): void;
toHaveBeenNthCalledWith(nth: number, ...expected: unknown[]): void;
toHaveLastReturnedWith(expected: unknown): void;
toHaveLength(expected: number): void;
toHaveNthReturnedWith(nth: number, expected: unknown): void;
toHaveProperty(propName: string | string[], value?: unknown): void;
toHaveReturned(): void;
toHaveReturnedTimes(expected: number): void;
toHaveReturnedWith(expected: unknown): void;
toMatch(expected: RegExp): void;
toMatchObject(expected: Record<PropertyKey, unknown> | Record<PropertyKey, unknown>[]): void;
toReturn(): void;
toReturnTimes(expected: number): void;
toReturnWith(expected: unknown): void;
toStrictEqual(candidate: unknown): void;
toThrow<E>(expected?:
| string
| RegExp
| E
| (new (...args: any[]) => E)): void;
[name: string]: unknown;
}
Type Parameters
Indexable
- [name: string]: unknown
Additional custom assertion methods can be added here.
Index
Properties
Methods
Properties
not
The negation object that allows chaining negated assertions.
rejects
The object that allows chaining assertions with async functions that are expected to throw an error.
resolves
The object that allows chaining assertions with async functions that are expected to resolve to a value.
toEqualBook
Methods
lastCalledWith
lastReturnedWith
nthCalledWith
nthReturnedWith
toBe
toBeCalled
toBeCalledTimes
toBeCalledWith
toBeCloseTo
- to
Be (candidate, tolerance?): voidClose To Asserts that a given numerical value is approximately equal to an expected number within a certain margin of error (tolerance). Useful when comparing floating-point numbers, which may be represented internally with precision errors.
Parameters
- candidate: number
The candidate number.
Optionaltolerance: numberThe number of significant decimal digits to consider when comparing the values (optional, default 2).
Returns void
- candidate: number
toBeDefined
toBeFalsy
toBeGreaterThan
toBeGreaterThanOrEqual
toBeInstanceOf
- to
Be <T>(expected): voidInstance Of Asserts that the value is an instance of the specified constructor.
Type Parameters
- T extends AnyConstructor
Parameters
- expected: T
The expected constructor.
Returns void
toBeLessThan
toBeLessThanOrEqual
toBeNaN
toBeNull
toBeTruthy
- to
Be (): voidTruthy Asserts that the value is truthy.
Returns void
Example: Usage
import { expect } from "@std/expect";
expect(1).toBeTruthy();
expect("foo").toBeTruthy();
expect([]).toBeTruthy();
expect({}).toBeTruthy();
expect(true).toBeTruthy();
expect(0).not.toBeTruthy();
expect("").not.toBeTruthy();
expect(null).not.toBeTruthy();
expect(undefined).not.toBeTruthy();
expect(false).not.toBeTruthy();
expect(NaN).not.toBeTruthy();
toBeUndefined
toContain
toContainEqual
toEqual
toHaveBeenCalled
toHaveBeenCalledTimes
toHaveBeenCalledWith
toHaveBeenLastCalledWith
toHaveBeenNthCalledWith
toHaveLastReturnedWith
toHaveLength
toHaveNthReturnedWith
- to
Have (nth, expected): voidNth Returned With Asserts that the function returned the specified value at the specified call index.
Parameters
- nth: number
The call index.
- expected: unknown
The expected return value.
Returns void
Example: Usage
import { expect, fn } from "@std/expect";
const mock = fn((str: string, num: number) => [str, num]);
mock("foo", 42);
mock("bar", 43);
mock("baz", 44);
expect(mock).toHaveNthReturnedWith(1, ["foo", 42]);
expect(mock).toHaveNthReturnedWith(2, ["bar", 43]);
expect(mock).toHaveNthReturnedWith(3, ["baz", 44]);- nth: number
toHaveProperty
toHaveReturned
toHaveReturnedTimes
toHaveReturnedWith
toMatch
toMatchObject
toReturn
toReturnTimes
toReturnWith
toStrictEqual
- to
Strict (candidate): voidEqual Asserts that the value is strictly equal to the specified value.
Parameters
- candidate: unknown
The candidate value.
Returns void
Example: Usage
import { expect } from "@std/expect";
expect(42).toStrictEqual(42);
expect("foo").toStrictEqual("foo");
const obj = {};
expect(obj).toStrictEqual(obj);
class LaCroix {
flavor: string;
constructor(flavor: string) {
this.flavor = flavor;
}
}
expect(new LaCroix("lemon")).not.toStrictEqual({ flavor: "lemon" });- candidate: unknown
toThrow
- to
Throw <E>(expected?): void Asserts that the function throws an error.
Type Parameters
Parameters
Returns void
Example: Usage
import { expect } from "@std/expect";
const fn = () => { throw new TypeError("foo") };
const fn2 = () => {};
expect(fn).toThrow();
expect(fn2).not.toThrow();
expect(fn).toThrow(TypeError);
expect(fn).toThrow("foo");
expect(fn).toThrow(/foo/);
expect(fn).not.toThrow(SyntaxError);
expect(fn).not.toThrow("bar");
expect(fn).not.toThrow(/bar/);
The Expected interface defines the available assertion methods.