@testing-library/react

  • Version 15.0.4
  • Published
  • 4.3 MB
  • 3 dependencies
  • MIT license

Install

npm i @testing-library/react
yarn add @testing-library/react
pnpm add @testing-library/react

Overview

Simple and complete React DOM testing utilities that encourage good testing practices.

Index

Variables

variable act

const act: any;
  • Simply calls ReactDOMTestUtils.act(cb) If that's not available (older version of react) then it simply calls the given callback immediately

Functions

function cleanup

cleanup: () => void;
  • Unmounts React trees that were mounted with render.

function configure

configure: (configDelta: ConfigFn | Partial<Config>) => void;

    function getConfig

    getConfig: () => Config;

      function render

      render: {
      <
      Q extends Queries = any,
      Container extends ReactDOMClient.Container = HTMLElement,
      BaseElement extends Element | DocumentFragment = Container
      >(
      ui: React.ReactNode,
      options: ClientRenderOptions<Q, Container, BaseElement>
      ): RenderResult<Q, Container, BaseElement>;
      <
      Q extends Queries = any,
      Container extends unknown = HTMLElement,
      BaseElement extends Element | DocumentFragment = Container
      >(
      ui: React.ReactNode,
      options: HydrateOptions<Q, Container, BaseElement>
      ): RenderResult<Q, Container, BaseElement>;
      (
      ui: React.ReactNode,
      options?: Omit<RenderOptions<any, HTMLElement, HTMLElement>, 'queries'>
      ): any;
      };
      • Render into a container which is appended to document.body. It should be used with cleanup.

      function renderHook

      renderHook: {
      <
      Result,
      Props,
      Q extends Queries = any,
      Container extends ReactDOMClient.Container = HTMLElement,
      BaseElement extends Element | DocumentFragment = Container
      >(
      render: (initialProps: Props) => Result,
      options?: ClientRenderHookOptions<Props, Q, Container, BaseElement>
      ): RenderHookResult<Result, Props>;
      <
      Result,
      Props,
      Q extends Queries = any,
      Container extends unknown = HTMLElement,
      BaseElement extends Element | DocumentFragment = Container
      >(
      render: (initialProps: Props) => Result,
      options?: HydrateHookOptions<Props, Q, Container, BaseElement>
      ): RenderHookResult<Result, Props>;
      };
      • Allows you to render a hook within a test React component without having to create that component yourself.

      Interfaces

      interface BaseRenderHookOptions

      interface BaseRenderHookOptions<
      Props,
      Q extends Queries,
      Container extends RendererableContainer | HydrateableContainer,
      BaseElement extends Element | DocumentFragment
      > extends BaseRenderOptions<Q, Container, BaseElement> {}

        property initialProps

        initialProps?: Props;
        • The argument passed to the renderHook callback. Can be useful if you plan to use the rerender utility to change the values passed to your hook.

        interface BaseRenderOptions

        interface BaseRenderOptions<
        Q extends Queries,
        Container extends RendererableContainer | HydrateableContainer,
        BaseElement extends Element | DocumentFragment
        > {}

          property baseElement

          baseElement?: BaseElement;
          • Defaults to the container if the container is specified. Otherwise document.body is used for the default. This is used as the base element for the queries as well as what is printed when you use debug().

            See Also

            • https://testing-library.com/docs/react-testing-library/api/#baseelement

          property container

          container?: Container;
          • By default, React Testing Library will create a div and append that div to the document.body. Your React component will be rendered in the created div. If you provide your own HTMLElement container via this option, it will not be appended to the document.body automatically.

            For example: If you are unit testing a <tbody> element, it cannot be a child of a div. In this case, you can specify a table as the render container.

            See Also

            • https://testing-library.com/docs/react-testing-library/api/#container

          property hydrate

          hydrate?: boolean;
          • If hydrate is set to true, then it will render with ReactDOM.hydrate. This may be useful if you are using server-side rendering and use ReactDOM.hydrate to mount your components.

            See Also

            • https://testing-library.com/docs/react-testing-library/api/#hydrate)

          property legacyRoot

          legacyRoot?: boolean;
          • Only works if used with React 18. Set to true if you want to force synchronous ReactDOM.render. Otherwise render will default to concurrent React if available.

          property queries

          queries?: Q;
          • Queries to bind. Overrides the default set from DOM Testing Library unless merged.

            See Also

            • https://testing-library.com/docs/react-testing-library/api/#queries

          property wrapper

          wrapper?: React.JSXElementConstructor<{ children: React.ReactNode }>;
          • Pass a React Component as the wrapper option to have it rendered around the inner element. This is most useful for creating reusable custom render functions for common data providers. See setup for examples.

            See Also

            • https://testing-library.com/docs/react-testing-library/api/#wrapper

          interface ClientRenderHookOptions

          interface ClientRenderHookOptions<
          Props,
          Q extends Queries,
          Container extends Element | DocumentFragment,
          BaseElement extends Element | DocumentFragment = Container
          > extends BaseRenderHookOptions<Props, Q, Container, BaseElement> {}

            property hydrate

            hydrate?: false | undefined;
            • If hydrate is set to true, then it will render with ReactDOM.hydrate. This may be useful if you are using server-side rendering and use ReactDOM.hydrate to mount your components.

              See Also

              • https://testing-library.com/docs/react-testing-library/api/#hydrate)

            interface ClientRenderOptions

            interface ClientRenderOptions<
            Q extends Queries,
            Container extends Element | DocumentFragment,
            BaseElement extends Element | DocumentFragment = Container
            > extends BaseRenderOptions<Q, Container, BaseElement> {}

              property hydrate

              hydrate?: false | undefined;
              • If hydrate is set to true, then it will render with ReactDOM.hydrate. This may be useful if you are using server-side rendering and use ReactDOM.hydrate to mount your components.

                See Also

                • https://testing-library.com/docs/react-testing-library/api/#hydrate)

              interface Config

              interface Config extends ConfigDTL {}

                property reactStrictMode

                reactStrictMode: boolean;

                  interface ConfigFn

                  interface ConfigFn {}

                    call signature

                    (existingConfig: Config): Partial<Config>;

                      interface HydrateHookOptions

                      interface HydrateHookOptions<
                      Props,
                      Q extends Queries,
                      Container extends Element | DocumentFragment,
                      BaseElement extends Element | DocumentFragment = Container
                      > extends BaseRenderHookOptions<Props, Q, Container, BaseElement> {}

                        property hydrate

                        hydrate: true;
                        • If hydrate is set to true, then it will render with ReactDOM.hydrate. This may be useful if you are using server-side rendering and use ReactDOM.hydrate to mount your components.

                          See Also

                          • https://testing-library.com/docs/react-testing-library/api/#hydrate)

                        interface HydrateOptions

                        interface HydrateOptions<
                        Q extends Queries,
                        Container extends Element | DocumentFragment,
                        BaseElement extends Element | DocumentFragment = Container
                        > extends BaseRenderOptions<Q, Container, BaseElement> {}

                          property hydrate

                          hydrate: true;
                          • If hydrate is set to true, then it will render with ReactDOM.hydrate. This may be useful if you are using server-side rendering and use ReactDOM.hydrate to mount your components.

                            See Also

                            • https://testing-library.com/docs/react-testing-library/api/#hydrate)

                          interface RenderHookResult

                          interface RenderHookResult<Result, Props> {}

                            property rerender

                            rerender: (props?: Props) => void;
                            • Triggers a re-render. The props will be passed to your renderHook callback.

                            property result

                            result: {
                            /**
                            * The value returned by your renderHook callback
                            */
                            current: Result;
                            };
                            • This is a stable reference to the latest value returned by your renderHook callback

                            property unmount

                            unmount: () => void;
                            • Unmounts the test component. This is useful for when you need to test any cleanup your useEffects have.

                            Type Aliases

                            type RenderHookOptions

                            type RenderHookOptions<
                            Props,
                            Q extends Queries = typeof queries,
                            Container extends Element | DocumentFragment = HTMLElement,
                            BaseElement extends Element | DocumentFragment = Container
                            > =
                            | ClientRenderHookOptions<Props, Q, Container, BaseElement>
                            | HydrateHookOptions<Props, Q, Container, BaseElement>;

                              type RenderOptions

                              type RenderOptions<
                              Q extends Queries = typeof queries,
                              Container extends RendererableContainer | HydrateableContainer = HTMLElement,
                              BaseElement extends Element | DocumentFragment = Container
                              > =
                              | ClientRenderOptions<Q, Container, BaseElement>
                              | HydrateOptions<Q, Container, BaseElement>;

                                type RenderResult

                                type RenderResult<
                                Q extends Queries = typeof queries,
                                Container extends Element | DocumentFragment = HTMLElement,
                                BaseElement extends Element | DocumentFragment = Container
                                > = {
                                container: Container;
                                baseElement: BaseElement;
                                debug: (
                                baseElement?: Element | DocumentFragment | Array<Element | DocumentFragment>,
                                maxLength?: number,
                                options?: prettyFormat.OptionsReceived
                                ) => void;
                                rerender: (ui: React.ReactNode) => void;
                                unmount: () => void;
                                asFragment: () => DocumentFragment;
                                } & { [P in keyof Q]: BoundFunction<Q[P]> };

                                  Package Files (1)

                                  Dependencies (3)

                                  Dev Dependencies (10)

                                  Peer Dependencies (2)

                                  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/@testing-library/react.

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