Skip to main content

externalexpect

Callable

  • expect<T>(actual: T, messageOrOptions?: string | { message?: string }): MakeMatchers<void, T, {}>

  • Type parameters

    • T = unknown

    Parameters

    • externalactual: T
    • externaloptionalmessageOrOptions: string | { message?: string }
      • externaloptionalmessage: string

    Returns MakeMatchers<void, T, {}>

Index

Properties

externalsoft

soft: <T>(actual: T, messageOrOptions?: string | { message?: string }) => MakeMatchers<void, T, {}>

Type declaration

    • <T>(actual: T, messageOrOptions?: string | { message?: string }): MakeMatchers<void, T, {}>
    • Type parameters

      • T = unknown

      Parameters

      • externalactual: T
      • externaloptionalmessageOrOptions: string | { message?: string }
        • externaloptionalmessage: string

      Returns MakeMatchers<void, T, {}>

externalpoll

poll: <T>(actual: () => T | Promise<T>, messageOrOptions?: string | { message?: string; timeout?: number; intervals?: number[] }) => PollMatchers<Promise<void>, T, {}>

Type declaration

    • <T>(actual: () => T | Promise<T>, messageOrOptions?: string | { message?: string; timeout?: number; intervals?: number[] }): PollMatchers<Promise<void>, T, {}>
    • Type parameters

      • T = unknown

      Parameters

      • externalactual: () => T | Promise<T>
        • externaloptionalmessageOrOptions: string | { message?: string; timeout?: number; intervals?: number[] }
          • externaloptionalmessage: string
          • externaloptionaltimeout: number
          • externaloptionalintervals: number[]

        Returns PollMatchers<Promise<void>, T, {}>

    externalconfigure

    configure: (configuration: { message?: string; timeout?: number; soft?: boolean }) => Expect<{}>

    Type declaration

      • (configuration: { message?: string; timeout?: number; soft?: boolean }): Expect<{}>
      • Parameters

        • externalconfiguration: { message?: string; timeout?: number; soft?: boolean }
          • externaloptionalmessage: string
          • externaloptionaltimeout: number
          • externaloptionalsoft: boolean

        Returns Expect<{}>

    externalnot

    not: Omit<AsymmetricMatchers, any | anything>

    Methods

    externalany

    • any(sample: unknown): AsymmetricMatcher
    • expect.any() matches any object instance created from the constructor or a corresponding primitive type. Use it inside expect(value).toEqual(expected) to perform pattern matching.

      Usage

      // Match instance of a class.
      class Example {}
      expect(new Example()).toEqual(expect.any(Example));

      // Match any number.
      expect({ prop: 1 }).toEqual({ prop: expect.any(Number) });

      // Match any string.
      expect('abc').toEqual(expect.any(String));

      Parameters

      • externalsample: unknown

      Returns AsymmetricMatcher

    externalanything

    • anything(): AsymmetricMatcher
    • expect.anything() matches everything except null and undefined. Use it inside expect(value).toEqual(expected) to perform pattern matching.

      Usage

      const value = { prop: 1 };
      expect(value).toEqual({ prop: expect.anything() });
      expect(value).not.toEqual({ otherProp: expect.anything() });

      Returns AsymmetricMatcher

    externalarrayContaining

    • arrayContaining(sample: unknown[]): AsymmetricMatcher
    • expect.arrayContaining() matches an array that contains all of the elements in the expected array, in any order. Note that received array may be a superset of the expected array and contain some extra elements.

      Use this method inside expect(value).toEqual(expected) to perform pattern matching.

      Usage

      expect([1, 2, 3]).toEqual(expect.arrayContaining([3, 1]));
      expect([1, 2, 3]).not.toEqual(expect.arrayContaining([1, 4]));

      Parameters

      • externalsample: unknown[]

      Returns AsymmetricMatcher

    externalcloseTo

    • closeTo(sample: number, precision?: number): AsymmetricMatcher
    • Compares floating point numbers for approximate equality. Use this method inside expect(value).toEqual(expected) to perform pattern matching. When just comparing two numbers, prefer expect(value).toBeCloseTo(expected[, numDigits]).

      Usage

      expect({ prop: 0.1 + 0.2 }).not.toEqual({ prop: 0.3 });
      expect({ prop: 0.1 + 0.2 }).toEqual({ prop: expect.closeTo(0.3, 5) });

      Parameters

      • externalsample: number
      • externaloptionalprecision: number

      Returns AsymmetricMatcher

    externalobjectContaining

    • objectContaining(sample: Record<string, unknown>): AsymmetricMatcher
    • expect.objectContaining() matches an object that contains and matches all of the properties in the expected object. Note that received object may be a superset of the expected object and contain some extra properties.

      Use this method inside expect(value).toEqual(expected) to perform pattern matching. Object properties can be matchers to further relax the expectation. See examples.

      Usage

      // Assert some of the properties.
      expect({ foo: 1, bar: 2 }).toEqual(expect.objectContaining({ foo: 1 }));

      // Matchers can be used on the properties as well.
      expect({ foo: 1, bar: 2 }).toEqual(expect.objectContaining({ bar: expect.any(Number) }));

      // Complex matching of sub-properties.
      expect({
      list: [1, 2, 3],
      obj: { prop: 'Hello world!', another: 'some other value' },
      extra: 'extra',
      }).toEqual(expect.objectContaining({
      list: expect.arrayContaining([2, 3]),
      obj: expect.objectContaining({ prop: expect.stringContaining('Hello') }),
      }));

      Parameters

      • externalsample: Record<string, unknown>

      Returns AsymmetricMatcher

    externalstringContaining

    • stringContaining(sample: string): AsymmetricMatcher
    • expect.stringContaining() matches a string that contains the expected substring. Use this method inside expect(value).toEqual(expected) to perform pattern matching.

      Usage

      expect('Hello world!').toEqual(expect.stringContaining('Hello'));

      Parameters

      • externalsample: string

      Returns AsymmetricMatcher

    externalstringMatching

    • stringMatching(sample: string | RegExp): AsymmetricMatcher
    • expect.stringMatching() matches a received string that in turn matches the expected pattern. Use this method inside expect(value).toEqual(expected) to perform pattern matching.

      Usage

      expect('123ms').toEqual(expect.stringMatching(/\d+m?s/));

      // Inside another matcher.
      expect({
      status: 'passed',
      time: '123ms',
      }).toEqual({
      status: expect.stringMatching(/passed|failed/),
      time: expect.stringMatching(/\d+m?s/),
      });

      Parameters

      • externalsample: string | RegExp

      Returns AsymmetricMatcher

    externalextend

    • extend<MoreMatchers>(matchers: MoreMatchers): Expect<{} & MoreMatchers>
    • Type parameters

      • MoreMatchers: Record<string, (this: ExpectMatcherState, receiver: any, ...args: any[]) => MatcherReturnType | Promise<MatcherReturnType>>

      Parameters

      • externalmatchers: MoreMatchers

      Returns Expect<{} & MoreMatchers>

    externalgetState

    • getState(): unknown
    • Returns unknown