Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot shallow render a component containing hooks #14091

Closed
ChibiBlasphem opened this issue Nov 4, 2018 · 16 comments
Closed

Cannot shallow render a component containing hooks #14091

ChibiBlasphem opened this issue Nov 4, 2018 · 16 comments

Comments

@ChibiBlasphem
Copy link

Do you want to request a feature or report a bug?
A bug

What is the current behavior?
An error is thrown:
Invariant Violation: Hooks can only be called inside the body of a function component.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:
App.js

import React, { useState } from 'react'

const App = () => {
  const [name, setName] = useState('Chris')
  return (
    <div>Hello { name }</div>
  )
}

export default App

Test.js

import React from 'react'
import ShallowRenderer from 'react-test-renderer/shallow'
import App from './components/App'

const renderer = new ShallowRenderer()
renderer.render(<App />)

What is the expected behavior?
We should be able to shallow render a component with hooks like any other component (Enzyme too)

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
16.7.0-alpha.0

@gaearon
Copy link
Collaborator

gaearon commented Nov 5, 2018

Yeah. We haven't added support to shallow renderer yet.

@ChibiBlasphem
Copy link
Author

Do you know when it will be scheduled ? I don't like really much to full mount my application.

@gaearon
Copy link
Collaborator

gaearon commented Nov 5, 2018

It's definitely a prerequisite to considering Hooks stable. Can't give you any dates though.

@ChibiBlasphem
Copy link
Author

Ok, that's good to know. Thank you for the answers =)

@anaisbetts
Copy link

I figured this out as well, https://codesandbox.io/s/82qv0m8l98 is a super small repro when we get around to testing this

@jamesseanwright
Copy link

For now, I'm working around this by injecting the hooks I require into my components, as per this example; this allows me to use mocks in my tests, upon which I set expectations.

It's important to highlight that this is a temporary workaround; it would be far better to assert the props resulting from a hook-derivative re-render, as they are an implementation detail. This probably also demonstrates why they shouldn't be used in the wild just yet! 😁

@juniusfree
Copy link

+1

@gaearon
Copy link
Collaborator

gaearon commented Jan 18, 2019

Fixed by #14567

@gaearon gaearon closed this as completed Jan 18, 2019
@jamesvsshark
Copy link

Thanks for the fix! When will this be released into alpha? Sorry for the noob question.

@jamesseanwright
Copy link

@jamesvsshark Within a couple of weeks, it would seem!

@ghost
Copy link

ghost commented Feb 4, 2019

Eagerly awaiting this being released to alpha...

@Kyle-Mendes
Copy link

I'm still getting this issue in React 16.8 release. Is there a corresponding jest upgrade that needs to happen to allow shallow testing on hooks?

@gaearon
Copy link
Collaborator

gaearon commented Feb 6, 2019

File a new issue with a reproducing case. Shallow rendering Hooks should work.

You’re sure you didn’t forget to update test renderer?

@Kyle-Mendes
Copy link

I'll double check that, thanks @gaearon.

If it is still not working, I'll make a new issue :)

@LukasBombach
Copy link

Where is the new issue? I too have that problem. I got this string:

    const React = require('react');
      module.exports = function ExpectedClass () {
          const [greeting, setgreeting] = React.useState("World");
          return React.createElement("div", {  }, [`Hello ${greeting}`]);
        }

which I import via require-from-string. This has never been an issue with React classes so I would not assume it's that library's fault. I just switched from class syntax to this syntax and I get that Invariant Error.

@gaearon
Copy link
Collaborator

gaearon commented Mar 19, 2019

@LukasBombach What "this" problem? You didn't provide any details about the error you're seeing. :-)

I'll lock this issue because it seems to attract low-quality reports. If you experience a problem with shallow rendering Hooks, please file a new issue with the exact error you were seeing and a reproducing project. We'd be happy to investigate it then. Thank you!

The originally reported issue was fixed in 16.8.0.

@facebook facebook locked as resolved and limited conversation to collaborators Mar 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants