Expected
import { Expected } from "jsr:@ende/expect";interface
interface Expected<IsAsync = false>The Expected interface defines the available assertion methods.
Index signatures
Index signature
[name: string]: unknownAdditional custom assertion methods can be added here.
Properties
not
not: IsAsync extends true ? Async<Expected<true>> : Expected<false>The negation object that allows chaining negated assertions.
resolves
resolves: Async<Expected<true>>The object that allows chaining assertions with async functions that are expected to resolve to a value.
rejects
rejects: Async<Expected<true>>The object that allows chaining assertions with async functions that are expected to throw an error.
Methods
lastCalledWith
lastCalledWith(...expected: unknown[]): voidAsserts that the function was called with the specified arguments.
...expected: unknown[]The expected arguments.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn();
mock("foo", 42);
mock("bar", 43)
expect(mock).lastCalledWith("bar", 43);
expect(mock).not.lastCalledWith("foo", 42);
lastReturnedWith
lastReturnedWith(expected: unknown): voidAsserts that the function returned the specified value.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn((str: string, num: number) => [str, num]);
mock("foo", 42);
mock("bar", 43);
expect(mock).lastReturnedWith(["bar", 43]);
expect(mock).not.lastReturnedWith(["foo", 42]);
@param expected The expected return value.
nthCalledWith
nthCalledWith(nth: number, ...expected: unknown[]): voidAsserts that the function was called with the specified arguments at the specified call index.
nth: numberThe call index.
...expected: unknown[]The expected arguments.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn();
mock("foo", 42);
mock("bar", 43);
mock("baz", 44);
expect(mock).nthCalledWith(1, "foo", 42);
expect(mock).nthCalledWith(2, "bar", 43);
expect(mock).nthCalledWith(3, "baz", 44);
nthReturnedWith
nthReturnedWith(nth: number, expected: unknown): voidAsserts that the function returned the specified value at the specified call index.
nth: numberThe call index.
expected: unknownThe expected return value.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn((str: string, num: number) => [str, num]);
mock("foo", 42);
mock("bar", 43);
mock("baz", 44);
expect(mock).nthReturnedWith(1, ["foo", 42]);
expect(mock).nthReturnedWith(2, ["bar", 43]);
expect(mock).nthReturnedWith(3, ["baz", 44]);
toBeCalled
toBeCalled(): voidAsserts that the function was called at least once.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn();
const mock2 = fn();
mock();
expect(mock).toBeCalled();
expect(mock2).not.toBeCalled();
toBeCalledTimes
toBeCalledTimes(expected: number): voidAsserts that the function was called the specified number of times.
expected: numberThe expected number of times.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn();
mock();
mock();
expect(mock).not.toBeCalledTimes(1);
expect(mock).toBeCalledTimes(2);
expect(mock).not.toBeCalledTimes(3);
toBeCalledWith
toBeCalledWith(...expected: unknown[]): voidAsserts that the function was called with the specified arguments.
...expected: unknown[]The expected arguments.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn();
mock("foo", 42);
mock("bar", 43);
expect(mock).toBeCalledWith("foo", 42);
expect(mock).toBeCalledWith("bar", 43);
expect(mock).not.toBeCalledWith("baz", 44);
toBeCloseTo
toBeCloseTo(candidate: number, tolerance?: number): voidAsserts 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.
candidate: numberThe candidate number.
tolerance?: numberThe number of significant decimal digits to consider when comparing the values (optional, default 2).
Example: Usage
import { expect } from "@ende/expect";
expect(0.2 + 0.1).toBeCloseTo(0.3);
expect(0.2 + 0.1).toBeCloseTo(0.3, 15);
expect(0.2 + 0.1).not.toBeCloseTo(0.3, 16);
toBeDefined
toBeDefined(): voidAsserts that the value is defined.
Example: Usage
import { expect } from "@ende/expect";
expect(null).toBeDefined();
expect(undefined).not.toBeDefined();
toBeFalsy
toBeFalsy(): voidAsserts that the value is falsy.
Example: Usage
import { expect } from "@ende/expect";
expect(null).toBeFalsy();
expect(undefined).toBeFalsy();
expect(0).toBeFalsy();
expect("").toBeFalsy();
expect(false).toBeFalsy();
expect(NaN).toBeFalsy();
expect(1).not.toBeFalsy();
expect("foo").not.toBeFalsy();
expect([]).not.toBeFalsy();
toBeGreaterThan
toBeGreaterThan(expected: number): voidAsserts that the value is greater than the specified number.
expected: numberThe expected number.
Example: Usage
import { expect } from "@ende/expect";
expect(2).toBeGreaterThan(1);
expect(2).not.toBeGreaterThan(2);
expect(2).not.toBeGreaterThan(3);
toBeGreaterThanOrEqual
toBeGreaterThanOrEqual(expected: number): voidAsserts that the value is greater than or equal to the specified number.
expected: numberThe expected number.
Example: Usage
import { expect } from "@ende/expect";
expect(2).toBeGreaterThanOrEqual(1);
expect(2).toBeGreaterThanOrEqual(2);
expect(2).not.toBeGreaterThanOrEqual(3);
toBeInstanceOf
toBeInstanceOf<T extends AnyConstructor>(expected: T): voidAsserts that the value is an instance of the specified constructor.
expected: TThe expected constructor.
Example: Usage
import { expect } from "@ende/expect";
expect(new Error()).toBeInstanceOf(Error);
expect(new Error()).not.toBeInstanceOf(TypeError);
toBeLessThan
toBeLessThan(expected: number): voidAsserts that the value is less than the specified number.
expected: numberThe expected number.
Example: Usage
import { expect } from "@ende/expect";
expect(1).toBeLessThan(2);
expect(1).not.toBeLessThan(1);
expect(1).not.toBeLessThan(0);
toBeLessThanOrEqual
toBeLessThanOrEqual(expected: number): voidAsserts that the value is less than or equal to the specified number.
expected: numberThe expected number.
Example: Usage
import { expect } from "@ende/expect";
expect(1).toBeLessThanOrEqual(2);
expect(1).toBeLessThanOrEqual(1);
expect(1).not.toBeLessThanOrEqual(0);
toBeNaN
toBeNaN(): voidAsserts that the value is NaN.
Example: Usage
import { expect } from "@ende/expect";
expect(NaN).toBeNaN();
expect(1).not.toBeNaN();
expect(undefined).toBeNaN();
expect(null).not.toBeNaN();
toBeNull
toBeNull(): voidAsserts that the value is null.
Example: Usage
import { expect } from "@ende/expect";
expect(null).toBeNull();
expect(undefined).not.toBeNull();
toBeTruthy
toBeTruthy(): voidAsserts that the value is truthy.
Example: Usage
import { expect } from "@ende/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
toBeUndefined(): voidAsserts that the value is undefined.
Example: Usage
import { expect } from "@ende/expect";
expect(undefined).toBeUndefined();
expect(null).not.toBeUndefined();
toBe
toBe(expected: unknown): voidAsserts that the value is equal to the specified value.
expected: unknownThe expected value.
Example: Usage
import { expect } from "@ende/expect";
expect(42).toBe(42);
expect("foo").toBe("foo");
const obj = {};
expect(obj).toBe(obj);
expect(obj).not.toBe({});
toContainEqual
toContainEqual(expected: unknown): voidAsserts that the value contains the specified value (deep equality).
expected: unknownThe expected value.
Example: Usage
import { expect } from "@ende/expect";
expect([1, 2, 3]).toContainEqual(2);
expect([1, 2, 3]).not.toContainEqual(4);
expect([{ foo: 42 }, { bar: 43 }]).toContainEqual({ bar: 43 });
expect([{ foo: 42 }, { bar: 43 }]).not.toContainEqual({ baz: 44 });
toContain
toContain(expected: unknown): voidAsserts that the value contains the specified value (shallow equality).
expected: unknownThe expected value.
Example: Usage
import { expect } from "@ende/expect";
expect([1, 2, 3]).toContain(2);
expect([1, 2, 3]).not.toContain(4);
const item0 = { foo: 42 };
const item1 = { bar: 43 };
const items = [item0, item1];
expect(items).toContain(item1);
expect(items).not.toContain({ foo: 42 });
toEqual
toEqual(expected: unknown): voidAsserts that the value is equal to the specified value (deep equality).
expected: unknownThe expected value.
Example: Usage
import { expect } from "@ende/expect";
expect(42).toEqual(42);
expect({ foo: 42 }).toEqual({ foo: 42 });
expect([1, 2, 3]).toEqual([1, 2, 3]);
toHaveBeenCalledTimes
toHaveBeenCalledTimes(expected: number): voidAsserts that the function was called the specified number of times.
expected: numberThe expected number of times.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn();
mock();
mock();
expect(mock).not.toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledTimes(2);
expect(mock).not.toHaveBeenCalledTimes(3);
toHaveBeenCalledWith
toHaveBeenCalledWith(...expected: unknown[]): voidAsserts that the function was called with the specified arguments.
...expected: unknown[]The expected arguments.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn();
mock("foo", 42);
mock("bar", 43);
expect(mock).toHaveBeenCalledWith("foo", 42);
expect(mock).toHaveBeenCalledWith("bar", 43);
expect(mock).not.toHaveBeenCalledWith("baz", 44);
toHaveBeenCalled
toHaveBeenCalled(): voidAsserts that the function was called at least once.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn();
const mock2 = fn();
mock();
expect(mock).toHaveBeenCalled();
expect(mock2).not.toHaveBeenCalled();
toHaveBeenLastCalledWith
toHaveBeenLastCalledWith(...expected: unknown[]): voidAsserts that the function was last called with the specified arguments.
...expected: unknown[]The expected arguments.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn();
mock("foo", 42);
mock("bar", 43);
expect(mock).toHaveBeenLastCalledWith("bar", 43);
expect(mock).not.toHaveBeenLastCalledWith("foo", 42);
toHaveBeenNthCalledWith
toHaveBeenNthCalledWith(nth: number, ...expected: unknown[]): voidAsserts that the function was called with the specified arguments at the specified call index.
nth: numberThe call index.
...expected: unknown[]The expected arguments.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn();
mock("foo", 42);
mock("bar", 43);
mock("baz", 44);
expect(mock).toHaveBeenNthCalledWith(1, "foo", 42);
expect(mock).toHaveBeenNthCalledWith(2, "bar", 43);
expect(mock).toHaveBeenNthCalledWith(3, "baz", 44);
toHaveLength
toHaveLength(expected: number): voidAsserts that the value has the specified length.
expected: numberThe expected length.
Example: Usage
import { expect } from "@ende/expect";
expect([1, 2, 3]).toHaveLength(3);
expect("foo").toHaveLength(3);
expect([]).toHaveLength(0);
toHaveLastReturnedWith
toHaveLastReturnedWith(expected: unknown): voidAsserts that the function last returned the specified value.
expected: unknownThe expected return value.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn((str: string, num: number) => [str, num]);
mock("foo", 42);
mock("bar", 43);
expect(mock).toHaveLastReturnedWith(["bar", 43]);
expect(mock).not.toHaveLastReturnedWith(["foo", 42]);
toHaveNthReturnedWith
toHaveNthReturnedWith(nth: number, expected: unknown): voidAsserts that the function returned the specified value at the specified call index.
nth: numberThe call index.
expected: unknownThe expected return value.
Example: Usage
import { expect, fn } from "@ende/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]);
toHaveProperty
toHaveProperty(propName: string | string[], value?: unknown): voidAsserts that the value has the specified property with an optional value.
propName: string | string[]The property name or an array of property names.
value?: unknownThe expected value (optional).
Example: Usage
import { expect } from "@ende/expect";
expect({ foo: 42 }).toHaveProperty("foo");
expect({ foo: 42 }).toHaveProperty("foo", 42);
expect({ foo: 42 }).not.toHaveProperty("bar");
toHaveReturnedTimes
toHaveReturnedTimes(expected: number): voidAsserts that the function returned the specified number of times.
expected: numberThe expected number of times.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn();
mock();
mock();
expect(mock).toHaveReturnedTimes(2);
expect(mock).not.toHaveReturnedTimes(3);
toHaveReturnedWith
toHaveReturnedWith(expected: unknown): voidAsserts that the function returned the specified value.
expected: unknownThe expected return value.
Example: Usage
import { expect, fn } from "@ende/expect";
const mockFn = fn((x: number) => ({ foo: x + 1 }));
mockFn(5);
mockFn(6);
expect(mockFn).toHaveReturnedWith({ foo: 7 });
expect(mockFn).not.toHaveReturnedWith({ foo: 5 });
toHaveReturned
toHaveReturned(): voidAsserts that the function returned.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn();
expect(mock).not.toHaveReturned();
mock();
expect(mock).toHaveReturned();
toMatch
toMatch(expected: RegExp): voidAsserts that the value matches the specified regular expression.
expected: RegExpThe expected regular expression.
Example: Usage
import { expect } from "@ende/expect";
expect("foo").toMatch(/^foo$/);
expect("bar").not.toMatch(/^foo$/);
toMatchObject
toMatchObject(expected: Record<PropertyKey, unknown> | Record<PropertyKey, unknown>[]): voidAsserts that the value matches the specified object (deep equality).
expected: Record<PropertyKey, unknown> | Record<PropertyKey, unknown>[]The expected object or array of objects.
Example: Usage
import { expect } from "@ende/expect";
expect({ foo: 42 }).toMatchObject({ foo: 42 });
expect({ foo: 42 }).not.toMatchObject({ foo: 43 });
toMatchSnapshot
toMatchSnapshot(hint?: string): voidAsserts that the value matches the most recent snapshot. If no snapshot
exists, one will be created when running in update mode (-- --update).
Requires expect.setState({ currentTestName: "..." }) to be called before use.
The test file path is auto-detected from the call stack.
hint?: stringAn optional hint string appended to the snapshot name.
Example: Usage
import { expect } from "@ende/expect";
Ende.test("snapshot test", () => {
expect.setState({ currentTestName: "snapshot test", testPath: import.meta.url });
expect({ foo: 42 }).toMatchSnapshot();
});
toMatchSnapshot(propertyMatchers: Record<string, unknown>, hint?: string): voidAsserts that the value matches the most recent snapshot, using property matchers to handle dynamic values like timestamps or IDs.
propertyMatchers: Record<string, unknown>An object of asymmetric matchers for dynamic values.
hint?: stringAn optional hint string appended to the snapshot name.
Example: Usage
import { expect } from "@ende/expect";
Ende.test("snapshot with matchers", () => {
expect.setState({ currentTestName: "snapshot with matchers", testPath: import.meta.url });
expect({ id: 1, name: "Alice" }).toMatchSnapshot({
id: expect.any(Number),
});
});
toMatchInlineSnapshot
toMatchInlineSnapshot(inlineSnapshot?: string): voidAsserts that the value matches the inline snapshot string. If no snapshot
is provided or the value differs, the source file will be updated when
running in update mode (-- --update).
inlineSnapshot?: stringThe expected inline snapshot string.
Example: Usage
import { expect } from "@ende/expect";
Ende.test("inline snapshot", () => {
expect({ foo: 42 }).toMatchInlineSnapshot(`
{
foo: 42,
}
`);
});
toMatchInlineSnapshot(propertyMatchers: Record<string, unknown>, inlineSnapshot?: string): voidAsserts that the value matches the inline snapshot string, using property matchers to handle dynamic values like timestamps or IDs.
propertyMatchers: Record<string, unknown>An object of asymmetric matchers for dynamic values.
inlineSnapshot?: stringThe expected inline snapshot string.
Example: Usage
import { expect } from "@ende/expect";
Ende.test("inline snapshot with matchers", () => {
expect({ id: 1, name: "Alice" }).toMatchInlineSnapshot({
id: expect.any(Number),
}, `
{
id: Any<Number>,
name: "Alice",
}
`);
});
toReturn
toReturn(): voidAsserts that the function returned.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn();
expect(mock).not.toReturn();
mock();
expect(mock).toReturn();
toReturnTimes
toReturnTimes(expected: number): voidAsserts that the function returns the specified number of times.
expected: numberThe expected number of times.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn();
mock();
mock();
expect(mock).not.toReturnTimes(1);
expect(mock).toReturnTimes(2);
expect(mock).not.toReturnTimes(3);
toReturnWith
toReturnWith(expected: unknown): voidAsserts that the function returns the specified value.
expected: unknownThe expected return value.
Example: Usage
import { expect, fn } from "@ende/expect";
const mock = fn(() => 42);
// expect(mock).toReturnWith(42);
expect(mock).not.toReturnWith(43);
toStrictEqual
toStrictEqual(candidate: unknown): voidAsserts that the value is strictly equal to the specified value.
candidate: unknownThe candidate value.
Example: Usage
import { expect } from "@ende/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" });
toThrow
toThrow<E extends Error = Error>(expected?: string | RegExp | E | (new (...args: any[]) => E)): voidAsserts that the function throws an error.
expected?: string | RegExp | E | (new (...args: any[]) => E)The expected error message, regular expression, or error constructor.
Example: Usage
import { expect } from "@ende/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/);