question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add option to mountHook to use custom mount method

See original GitHub issue

What would you like?

It would be great to have the option to use a custom mount method when using mountHook.

Why is this needed?

Currently I have to mount a component to test hooks that depend on a React.Context or a Provider. With a custom version of mount as described in https://github.com/cypress-io/cypress/blob/develop/npm/react/docs/providers-and-composition.md, this would be possible without creating components.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
sethomascommented, Jun 23, 2021

bump, also here for same thing.

I have context, that needs a provider.

Sure I could just use mount and add the provider there, and the hook, but then I need to render items from the hook and check the rendered items. I’d rather just test the return of the hook directly and the above example with @testing-library/react-hooks is exactly what I want with that wrapper option.

EDIT: I am actually using import {renderHook} from '@testing-library/react-hooks/dom'; in my cypress component testing setup, I can use renderHook with the wrapper option, get the return values, then set my assertions.

1reaction
rockindahizzycommented, Dec 9, 2022

We removed mountHook in v11. We recommend mounting your hook in a test component instead. Here are docs describing the way forward without mountHook

Read more comments on GitHub >

github_iconTop Results From Across the Web

Building Your Own Hooks - React
Custom Hooks are a mechanism to reuse stateful logic (such as setting up a subscription and remembering the current value), but every time...
Read more >
Effective React Tests - Gleb Bahmutov
Let's create a few React components, and I will use react-scripts to bundle them ... import { mount } from 'cypress-react-unit-test'
Read more >
React Examples | Cypress Documentation
Mounting Components Mounting a Component The first step in testing a component ... Below are a few examples that demonstrate using a custom...
Read more >
How to use componentWillMount() in React Hooks?
You cannot use any of the existing lifecycle methods ( componentDidMount , componentDidUpdate , componentWillUnmount etc.) in a hook.
Read more >
12 Recipes for testing React applications using cypress-react ...
We first create a routing environment similar to that in which we'll use the component. We set up that environment so we can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found