close
Skip to main content

@std/expect@1.0.19
Built and signed on GitHub Actions

Works with
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score94%
License
MIT
Downloads973/wk
Publisheda month ago (1.0.19)

Jest compatible `expect` assertion functions

Functions

f
N
expect<T extends Expected = Expected>(
value: unknown,
customMessage?: string
): T

Note: the documentation for this module is taken from Jest and the examples are updated for Deno.

f
fn(...stubs: Function[]): Function

Creates a mock function that can be used for testing and assertions.

Interfaces

I

The Expected interface defines the available assertion methods.

  • lastCalledWith(...expected: unknown[]): void

    Asserts that the function was called with the specified arguments.

  • lastReturnedWith(expected: unknown): void

    Asserts that the function returned the specified value.

  • not: IsAsync extends true ? Async<Expected<true>> : Expected<false>

    The negation object that allows chaining negated assertions.

  • nthCalledWith(
    nth: number,
    ...expected: unknown[]
    ): void

    Asserts that the function was called with the specified arguments at the specified call index.

  • nthReturnedWith(
    nth: number,
    expected: unknown
    ): void

    Asserts that the function returned the specified value at the specified call index.

  • rejects: Async<Expected<true>>

    The object that allows chaining assertions with async functions that are expected to throw an error.

  • resolves: Async<Expected<true>>

    The object that allows chaining assertions with async functions that are expected to resolve to a value.

  • toBe(expected: unknown): void

    Asserts that the value is equal to the specified value.

  • toBeCalled(): void

    Asserts that the function was called at least once.

  • toBeCalledTimes(expected: number): void

    Asserts that the function was called the specified number of times.

  • toBeCalledWith(...expected: unknown[]): void

    Asserts that the function was called with the specified arguments.

  • toBeCloseTo(
    candidate: number,
    tolerance?: number
    ): void

    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.

  • toBeDefined(): void

    Asserts that the value is defined.

  • toBeFalsy(): void

    Asserts that the value is falsy.

  • toBeGreaterThan(expected: number): void

    Asserts that the value is greater than the specified number.

  • toBeGreaterThanOrEqual(expected: number): void

    Asserts that the value is greater than or equal to the specified number.

  • toBeInstanceOf<T extends AnyConstructor>(expected: T): void

    Asserts that the value is an instance of the specified constructor.

  • toBeLessThan(expected: number): void

    Asserts that the value is less than the specified number.

  • toBeLessThanOrEqual(expected: number): void

    Asserts that the value is less than or equal to the specified number.

  • toBeNaN(): void

    Asserts that the value is NaN.

  • toBeNull(): void

    Asserts that the value is null.

  • toBeTruthy(): void

    Asserts that the value is truthy.

  • Asserts that the value is undefined.

  • toContain(expected: unknown): void

    Asserts that the value contains the specified value (shallow equality).

  • toContainEqual(expected: unknown): void

    Asserts that the value contains the specified value (deep equality).

  • toEqual(expected: unknown): void

    Asserts that the value is equal to the specified value (deep equality).

  • Asserts that the function was called at least once.

  • toHaveBeenCalledTimes(expected: number): void

    Asserts that the function was called the specified number of times.

  • toHaveBeenCalledWith(...expected: unknown[]): void

    Asserts that the function was called with the specified arguments.

  • toHaveBeenLastCalledWith(...expected: unknown[]): void

    Asserts that the function was last called with the specified arguments.

  • toHaveBeenNthCalledWith(
    nth: number,
    ...expected: unknown[]
    ): void

    Asserts that the function was called with the specified arguments at the specified call index.

  • toHaveLastReturnedWith(expected: unknown): void

    Asserts that the function last returned the specified value.

  • toHaveLength(expected: number): void

    Asserts that the value has the specified length.

  • toHaveNthReturnedWith(
    nth: number,
    expected: unknown
    ): void

    Asserts that the function returned the specified value at the specified call index.

  • toHaveProperty(
    propName: string | string[],
    value?: unknown
    ): void

    Asserts that the value has the specified property with an optional value.

  • Asserts that the function returned.

  • toHaveReturnedTimes(expected: number): void

    Asserts that the function returned the specified number of times.

  • toHaveReturnedWith(expected: unknown): void

    Asserts that the function returned the specified value.

  • toMatch(expected: RegExp): void

    Asserts that the value matches the specified regular expression.

  • toMatchInlineSnapshot(inlineSnapshot?: string): void

    Asserts 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).

  • toMatchObject(expected:
    Record<PropertyKey, unknown>
    | Record<PropertyKey, unknown>[]
    ): void

    Asserts that the value matches the specified object (deep equality).

  • toMatchSnapshot(hint?: string): void

    Asserts that the value matches the most recent snapshot. If no snapshot exists, one will be created when running in update mode (-- --update).

  • toReturn(): void

    Asserts that the function returned.

  • toReturnTimes(expected: number): void

    Asserts that the function returns the specified number of times.

  • toReturnWith(expected: unknown): void

    Asserts that the function returns the specified value.

  • toStrictEqual(candidate: unknown): void

    Asserts that the value is strictly equal to the specified value.

  • toThrow<E extends Error = Error>(expected?: string | RegExp | E | (new (...args: any[]) => E)): void

    Asserts that the function throws an error.

I

State for snapshot testing, following Jest's expect.getState()/expect.setState() API.

  • currentTestName: string | undefined

    The name of the currently running test. Required for snapshot testing.

  • testPath: string | undefined

    The file path of the currently running test. Auto-detected from stack trace if not set.

Type Aliases

T
AnyConstructor = new (...args: any[]) => any

A constructor that accepts any args and returns any value

T
Async<T> = [K in keyof T]: T[K] extends (...args: any[]) => unknown ? (...args: Parameters<T[K]>) => Promise<ReturnType<T[K]>> : T[K]

converts all the methods in an interface to be async functions

Variables

v
expect.addEqualityTesters: (newTesters: Tester[]) => void

You can use expect.addEqualityTesters to add your own methods to test if two objects are equal. For example, let's say you have a class in your code that represents volume and can determine if two volumes using different units are equal. You may want toEqual (and other equality matchers) to use this custom equality method when comparing to Volume classes. You can add a custom equality tester to have toEqual detect and apply custom logic when comparing Volume classes:

v
expect.addSnapshotSerializer: (plugin: SnapshotPlugin) => void

expect.addSnapshotSerializer adds a module that formats application-specific data structures.

v
expect.any: (c: unknown) => ReturnType<asymmetricMatchers.any>

expect.any(constructor) matches anything that was created with the given constructor or if it's a primitive that is of the passed type. You can use it inside toEqual or toHaveBeenCalledWith instead of a literal value.

v
expect.anything: () => ReturnType<asymmetricMatchers.anything>

expect.anything() matches anything but null or undefined. You can use it inside toEqual or toHaveBeenCalledWith instead of a literal value.

v
expect.arrayContaining: (c: any[]) => ReturnType<asymmetricMatchers.arrayContaining>

expect.arrayContaining(array) matches a received array which contains all of the elements in the expected array. That is, the expected array is a subset of the received array. Therefore, it matches a received array which contains elements that are not in the expected array.

v
expect.assertions: (num: number) => void

expect.assertions verifies that a certain number of assertions are called during a test.

v
expect.closeTo: (
num: number,
numDigits?: number
) => ReturnType<asymmetricMatchers.closeTo>

expect.closeTo(number, numDigits?) is useful when comparing floating point numbers in object properties or array item. If you need to compare a number, please use .toBeCloseTo instead.

v
expect.extend: (newExtendMatchers: Matchers) => void

Extend expect() with custom provided matchers.

v
expect.getState: () => ExpectSnapshotState

expect.getState returns the current state for snapshot matchers.

v

expect.hasAssertions verifies that at least one assertion is called during a test.

v
expect.not: { arrayContaining; objectContaining; stringContaining; stringMatching; }

expect.not.arrayContaining matches a received array which does not contain all of the elements in the expected array. That is, the expected array is not a subset of the received array.

v
expect.objectContaining: (obj: Record<string, unknown>) => ReturnType<asymmetricMatchers.objectContaining>

expect.objectContaining(object) matches any received object that recursively matches the expected properties. That is, the expected object is not a subset of the received object. Therefore, it matches a received object which contains properties that are not in the expected object.

v
expect.setState: (state: Partial<ExpectSnapshotState>) => void

expect.setState sets the state for snapshot matchers. This must be called before using toMatchSnapshot() to provide the current test name and optionally the test file path.

v
expect.stringContaining: (str: string) => ReturnType<asymmetricMatchers.stringContaining>

expect.stringContaining(string) matches the received value if it is a string that contains the exact expected string.

v
expect.stringMatching: (pattern: string | RegExp) => ReturnType<asymmetricMatchers.stringMatching>

expect.stringMatching(string | regexp) matches the received value if it is a string that matches the expected string or regular expression.

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.