testing-library / testing-library/react-testing-library

renderHook support for multiple parameters

Open
#1,350 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
19.7k
Forks
1.2k
Avg merge
3d 16h
Merged PRs (30d)
1

Description

Describe the feature you'd like:

Testing hooks should support hooks with multiple parameters. Currently, the option "initialProps" of renderHook, and the "rerender" function it returns are supporting only a single parameter.

Suggested implementation:

Obviously, for backwards compatibility, the option "initialProps" cannot be changed. Therefore, I suggest to add a new option "initialArgs" that is an array and renders the hook to be tested by spreading that array.

The function "rerender" returned from "renderHook" will take multiple parameters and forward them to the hook as well.

import useHook from "..."

const { result, rerender } = renderHook(useHook, { initialArgs: [1, 2, 3] })
rerender(3, 4, 5)

It should be an error (exception) to pass both options "initialProps" and "initialArgs" at the same time.

Describe alternatives you've considered:

As a workaround, I currently wrap my multi-arg hooks in a wrapper hook taking an array as "props", but that is cumbersome:

import useHook from "..."

function useTestHook(args) {
    return useHook(...args)
}

const { result, rerender } = renderHook(useTestHook, { initialProps: [1, 2, 3] })
rerender([3, 4, 5])
Teachability, Documentation, Adoption, Migration Strategy:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the renderHook entry point and the existing handling of initialProps and rerender. Check the current renderHook tests before defining coverage for initialArgs, multiple rerender parameters, and rejecting both options; done means multi-parameter hooks work while existing initialProps behavior remains compatible.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.