expect

  • Version 29.7.0
  • Published
  • 146 kB
  • 5 dependencies
  • MIT license

Install

npm i expect
yarn add expect
pnpm add expect

Overview

This package exports the `expect` function used in [Jest](https://jestjs.io/). You can find its documentation [on Jest's website](https://jestjs.io/docs/expect).

Index

Variables

Classes

Interfaces

Type Aliases

Variables

variable expect

const expect: Expect;

    Classes

    class AsymmetricMatcher

    abstract class AsymmetricMatcher<T> implements AsymmetricMatcher_2 {}

      constructor

      constructor(sample: {}, inverse?: boolean);

        property $$typeof

        $$typeof: Symbol;

          property inverse

          protected inverse: boolean;

            property sample

            protected sample: {};

              method asymmetricMatch

              abstract asymmetricMatch: (other: unknown) => boolean;

                method getExpectedType

                getExpectedType: () => string;

                  method getMatcherContext

                  protected getMatcherContext: () => MatcherContext;

                    method toAsymmetricMatcher

                    toAsymmetricMatcher: () => string;

                      method toString

                      abstract toString: () => string;

                        class JestAssertionError

                        class JestAssertionError extends Error {}

                          property matcherResult

                          matcherResult?: Omit<SyncExpectationResult, 'message'> & { message: string };

                            Interfaces

                            interface AsymmetricMatchers

                            interface AsymmetricMatchers {}

                              method any

                              any: (sample: unknown) => AsymmetricMatcher_2;

                                method anything

                                anything: () => AsymmetricMatcher_2;

                                  method arrayContaining

                                  arrayContaining: (sample: Array<unknown>) => AsymmetricMatcher_2;

                                    method closeTo

                                    closeTo: (sample: number, precision?: number) => AsymmetricMatcher_2;

                                      method objectContaining

                                      objectContaining: (sample: Record<string, unknown>) => AsymmetricMatcher_2;

                                        method stringContaining

                                        stringContaining: (sample: string) => AsymmetricMatcher_2;

                                          method stringMatching

                                          stringMatching: (sample: string | RegExp) => AsymmetricMatcher_2;

                                            interface BaseExpect

                                            interface BaseExpect {}

                                              method addEqualityTesters

                                              addEqualityTesters: (testers: Array<Tester>) => void;

                                                method assertions

                                                assertions: (numberOfAssertions: number) => void;

                                                  method extend

                                                  extend: (matchers: MatchersObject) => void;

                                                    method extractExpectedAssertionsErrors

                                                    extractExpectedAssertionsErrors: () => ExpectedAssertionsErrors;

                                                      method getState

                                                      getState: () => MatcherState;

                                                        method hasAssertions

                                                        hasAssertions: () => void;

                                                          method setState

                                                          setState: (state: Partial<MatcherState>) => void;

                                                            interface Matchers

                                                            interface Matchers<R extends void | Promise<void>, T = unknown> {}

                                                              method lastCalledWith

                                                              lastCalledWith: (...expected: Array<unknown>) => R;
                                                              • Ensures the last call to a mock function was provided specific args.

                                                              method lastReturnedWith

                                                              lastReturnedWith: (expected?: unknown) => R;
                                                              • Ensure that the last call to a mock function has returned a specified value.

                                                              method nthCalledWith

                                                              nthCalledWith: (nth: number, ...expected: Array<unknown>) => R;
                                                              • Ensure that a mock function is called with specific arguments on an Nth call.

                                                              method nthReturnedWith

                                                              nthReturnedWith: (nth: number, expected?: unknown) => R;
                                                              • Ensure that the nth call to a mock function has returned a specified value.

                                                              method toBe

                                                              toBe: (expected: unknown) => R;
                                                              • Checks that a value is what you expect. It calls Object.is to compare values. Don't use toBe with floating-point numbers.

                                                              method toBeCalled

                                                              toBeCalled: () => R;
                                                              • Ensures that a mock function is called.

                                                              method toBeCalledTimes

                                                              toBeCalledTimes: (expected: number) => R;
                                                              • Ensures that a mock function is called an exact number of times.

                                                              method toBeCalledWith

                                                              toBeCalledWith: (...expected: Array<unknown>) => R;
                                                              • Ensure that a mock function is called with specific arguments.

                                                              method toBeCloseTo

                                                              toBeCloseTo: (expected: number, precision?: number) => R;
                                                              • Using exact equality with floating point numbers is a bad idea. Rounding means that intuitive things fail. The default for precision is 2.

                                                              method toBeDefined

                                                              toBeDefined: () => R;
                                                              • Ensure that a variable is not undefined.

                                                              method toBeFalsy

                                                              toBeFalsy: () => R;
                                                              • When you don't care what a value is, you just want to ensure a value is false in a boolean context.

                                                              method toBeGreaterThan

                                                              toBeGreaterThan: (expected: number | bigint) => R;
                                                              • For comparing floating point numbers.

                                                              method toBeGreaterThanOrEqual

                                                              toBeGreaterThanOrEqual: (expected: number | bigint) => R;
                                                              • For comparing floating point numbers.

                                                              method toBeInstanceOf

                                                              toBeInstanceOf: (expected: unknown) => R;
                                                              • Ensure that an object is an instance of a class. This matcher uses instanceof underneath.

                                                              method toBeLessThan

                                                              toBeLessThan: (expected: number | bigint) => R;
                                                              • For comparing floating point numbers.

                                                              method toBeLessThanOrEqual

                                                              toBeLessThanOrEqual: (expected: number | bigint) => R;
                                                              • For comparing floating point numbers.

                                                              method toBeNaN

                                                              toBeNaN: () => R;
                                                              • Used to check that a variable is NaN.

                                                              method toBeNull

                                                              toBeNull: () => R;
                                                              • This is the same as .toBe(null) but the error messages are a bit nicer. So use .toBeNull() when you want to check that something is null.

                                                              method toBeTruthy

                                                              toBeTruthy: () => R;
                                                              • Use when you don't care what a value is, you just want to ensure a value is true in a boolean context. In JavaScript, there are six falsy values: false, 0, '', null, undefined, and NaN. Everything else is truthy.

                                                              method toBeUndefined

                                                              toBeUndefined: () => R;
                                                              • Used to check that a variable is undefined.

                                                              method toContain

                                                              toContain: (expected: unknown) => R;
                                                              • Used when you want to check that an item is in a list. For testing the items in the list, this uses ===, a strict equality check.

                                                              method toContainEqual

                                                              toContainEqual: (expected: unknown) => R;
                                                              • Used when you want to check that an item is in a list. For testing the items in the list, this matcher recursively checks the equality of all fields, rather than checking for object identity.

                                                              method toEqual

                                                              toEqual: (expected: unknown) => R;
                                                              • Used when you want to check that two objects have the same value. This matcher recursively checks the equality of all fields, rather than checking for object identity.

                                                              method toHaveBeenCalled

                                                              toHaveBeenCalled: () => R;
                                                              • Ensures that a mock function is called.

                                                              method toHaveBeenCalledTimes

                                                              toHaveBeenCalledTimes: (expected: number) => R;
                                                              • Ensures that a mock function is called an exact number of times.

                                                              method toHaveBeenCalledWith

                                                              toHaveBeenCalledWith: (...expected: Array<unknown>) => R;
                                                              • Ensure that a mock function is called with specific arguments.

                                                              method toHaveBeenLastCalledWith

                                                              toHaveBeenLastCalledWith: (...expected: Array<unknown>) => R;
                                                              • If you have a mock function, you can use .toHaveBeenLastCalledWith to test what arguments it was last called with.

                                                              method toHaveBeenNthCalledWith

                                                              toHaveBeenNthCalledWith: (nth: number, ...expected: Array<unknown>) => R;
                                                              • Ensure that a mock function is called with specific arguments on an Nth call.

                                                              method toHaveLastReturnedWith

                                                              toHaveLastReturnedWith: (expected?: unknown) => R;
                                                              • Use to test the specific value that a mock function last returned. If the last call to the mock function threw an error, then this matcher will fail no matter what value you provided as the expected return value.

                                                              method toHaveLength

                                                              toHaveLength: (expected: number) => R;
                                                              • Used to check that an object has a .length property and it is set to a certain numeric value.

                                                              method toHaveNthReturnedWith

                                                              toHaveNthReturnedWith: (nth: number, expected?: unknown) => R;
                                                              • Use to test the specific value that a mock function returned for the nth call. If the nth call to the mock function threw an error, then this matcher will fail no matter what value you provided as the expected return value.

                                                              method toHaveProperty

                                                              toHaveProperty: (
                                                              expectedPath: string | Array<string>,
                                                              expectedValue?: unknown
                                                              ) => R;
                                                              • Use to check if property at provided reference keyPath exists for an object. For checking deeply nested properties in an object you may use dot notation or an array containing the keyPath for deep references.

                                                                Optionally, you can provide a value to check if it's equal to the value present at keyPath on the target object. This matcher uses 'deep equality' (like toEqual()) and recursively checks the equality of all fields.

                                                                Example 1

                                                                expect(houseForSale).toHaveProperty('kitchen.area', 20);

                                                              method toHaveReturned

                                                              toHaveReturned: () => R;
                                                              • Use to test that the mock function successfully returned (i.e., did not throw an error) at least one time

                                                              method toHaveReturnedTimes

                                                              toHaveReturnedTimes: (expected: number) => R;
                                                              • Use to ensure that a mock function returned successfully (i.e., did not throw an error) an exact number of times. Any calls to the mock function that throw an error are not counted toward the number of times the function returned.

                                                              method toHaveReturnedWith

                                                              toHaveReturnedWith: (expected?: unknown) => R;
                                                              • Use to ensure that a mock function returned a specific value.

                                                              method toMatch

                                                              toMatch: (expected: string | RegExp) => R;
                                                              • Check that a string matches a regular expression.

                                                              method toMatchObject

                                                              toMatchObject: (
                                                              expected: Record<string, unknown> | Array<Record<string, unknown>>
                                                              ) => R;
                                                              • Used to check that a JavaScript object matches a subset of the properties of an object

                                                              method toReturn

                                                              toReturn: () => R;
                                                              • Ensure that a mock function has returned (as opposed to thrown) at least once.

                                                              method toReturnTimes

                                                              toReturnTimes: (expected: number) => R;
                                                              • Ensure that a mock function has returned (as opposed to thrown) a specified number of times.

                                                              method toReturnWith

                                                              toReturnWith: (expected?: unknown) => R;
                                                              • Ensure that a mock function has returned a specified value at least once.

                                                              method toStrictEqual

                                                              toStrictEqual: (expected: unknown) => R;
                                                              • Use to test that objects have the same types as well as structure.

                                                              method toThrow

                                                              toThrow: (expected?: unknown) => R;
                                                              • Used to test that a function throws when it is called.

                                                              method toThrowError

                                                              toThrowError: (expected?: unknown) => R;
                                                              • If you want to test that a specific error is thrown inside a function.

                                                              interface MatcherState

                                                              interface MatcherState {}

                                                                property assertionCalls

                                                                assertionCalls: number;

                                                                  property currentConcurrentTestName

                                                                  currentConcurrentTestName?: () => string | undefined;

                                                                    property currentTestName

                                                                    currentTestName?: string;

                                                                      property error

                                                                      error?: Error;

                                                                        property expand

                                                                        expand?: boolean;

                                                                          property expectedAssertionsNumber

                                                                          expectedAssertionsNumber: number | null;

                                                                            property expectedAssertionsNumberError

                                                                            expectedAssertionsNumberError?: Error;

                                                                              property isExpectingAssertions

                                                                              isExpectingAssertions: boolean;

                                                                                property isExpectingAssertionsError

                                                                                isExpectingAssertionsError?: Error;

                                                                                  property isNot

                                                                                  isNot?: boolean;

                                                                                    property numPassingAsserts

                                                                                    numPassingAsserts: number;

                                                                                      property promise

                                                                                      promise?: string;

                                                                                        property suppressedErrors

                                                                                        suppressedErrors: Array<Error>;

                                                                                          property testPath

                                                                                          testPath?: string;

                                                                                            interface MatcherUtils

                                                                                            interface MatcherUtils {}

                                                                                              property customTesters

                                                                                              customTesters: Array<Tester>;

                                                                                                property equals

                                                                                                equals: EqualsFunction;

                                                                                                  property utils

                                                                                                  utils: typeof jestMatcherUtils & {
                                                                                                  iterableEquality: Tester;
                                                                                                  subsetEquality: Tester;
                                                                                                  };

                                                                                                    method dontThrow

                                                                                                    dontThrow: () => void;

                                                                                                      Type Aliases

                                                                                                      type AsyncExpectationResult

                                                                                                      type AsyncExpectationResult = Promise<SyncExpectationResult>;

                                                                                                        type Expect

                                                                                                        type Expect = {
                                                                                                        <T = unknown>(actual: T): Matchers<void, T> &
                                                                                                        Inverse<Matchers<void, T>> &
                                                                                                        PromiseMatchers<T>;
                                                                                                        } & BaseExpect &
                                                                                                        AsymmetricMatchers &
                                                                                                        Inverse<Omit<AsymmetricMatchers, 'any' | 'anything'>>;

                                                                                                          type ExpectationResult

                                                                                                          type ExpectationResult = SyncExpectationResult | AsyncExpectationResult;

                                                                                                            type MatcherContext

                                                                                                            type MatcherContext = MatcherUtils & Readonly<MatcherState>;

                                                                                                              type MatcherFunction

                                                                                                              type MatcherFunction<Expected extends Array<unknown> = []> =
                                                                                                              MatcherFunctionWithContext<MatcherContext, Expected>;

                                                                                                                type MatcherFunctionWithContext

                                                                                                                type MatcherFunctionWithContext<
                                                                                                                Context extends MatcherContext = MatcherContext,
                                                                                                                Expected extends Array<any> = [] /** TODO should be: extends Array<unknown> = [] */
                                                                                                                > = (this: Context, actual: unknown, ...expected: Expected) => ExpectationResult;

                                                                                                                  type SyncExpectationResult

                                                                                                                  type SyncExpectationResult = {
                                                                                                                  pass: boolean;
                                                                                                                  message(): string;
                                                                                                                  };

                                                                                                                    Package Files (1)

                                                                                                                    Dependencies (5)

                                                                                                                    Dev Dependencies (6)

                                                                                                                    Peer Dependencies (0)

                                                                                                                    No peer dependencies.

                                                                                                                    Badge

                                                                                                                    To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                                                                    You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/expect.

                                                                                                                    • Markdown
                                                                                                                      [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/expect)
                                                                                                                    • HTML
                                                                                                                      <a href="https://www.jsdocs.io/package/expect"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>