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

Enzyme fails to return text or html when shallow rendering #897

Open
Sawtaytoes opened this issue Mar 17, 2018 · 5 comments
Open

Enzyme fails to return text or html when shallow rendering #897

Sawtaytoes opened this issue Mar 17, 2018 · 5 comments
Assignees
Labels

Comments

@Sawtaytoes
Copy link

Sawtaytoes commented Mar 17, 2018

If you are reporting a bug or having an issue setting up React Hot Loader, please fill in below. For feature requests, feel free to remove this template entirely.

Description

What you are reporting:
When shallow rendering in enzyme, text and html functions return null strings.

I tried a few things when reproducing. First, this works fine in v3. Merely upgrading to v4 without changing any other code is can cause this issue. I tried removing all React Hot Loader code and nothing changed.

If I do a mount instead of shallow render, it works fine which makes me think something's getting rendered in between.

Expected behavior

What you think should happen:
wrapper.text() should have the value hi and any other text in the component.

Actual behavior

What actually happens:
wrapper.text() returns a null string ''.

Environment

React Hot Loader version: 4.0.0

Run these commands in the project folder and fill in their results:

  1. node -v: v8.9.4
  2. yarn-v: 1.3.2

Then, specify:

  1. Operating system: Windows 10 64-bit
  2. Browser and version: Chrome 66.0.3359.33 beta 64-bit

Reproducible Demo

.babelrc

{
  "plugins": [
    "add-react-displayname",
    "extract-hoc/babel",
    "react-hot-loader/babel",
    "transform-class-properties",
    "transform-object-rest-spread",
    "transform-runtime",
  ],
  "presets": [
    [
      "env", {
        "modules": false,
      }
    ],
    "react",
  ],
}

entrypoint.js

import Adapter from 'enzyme-adapter-react-16'
import React from 'react'
import { configure } from 'enzyme'
import { shallow } from 'enzyme'

// Setup Enzyme for React 16
configure({ adapter: new Adapter() })

const TestComponent = () => <div>hi</div>
const wrapper = shallow(<TestComponent />)

console.log(
  'Should be true:',
  wrapper.text() === 'hi'
)
@theKashey
Copy link
Collaborator

Quite strange - outside of webpack dev env, ie without module.hot enabled, RHL should not affect anything. And not affecting by fact.

@Sawtaytoes
Copy link
Author

That would be because I have hot: true. I'm running unit tests in the browser and React is showing information about the tests, parsing TAP output messages.

@theKashey
Copy link
Collaborator

Now I see.
To be honest - we never tested RHL in such environment. Even more - we had an issue then RHL just blow up Jest, I mean - completely.
I'll try to investigate that is happening and how we could mitigate the problem, but now I can't give you any advise, sorry.

@Sawtaytoes
Copy link
Author

I understand.

I found a workaround for my issues with v3, so I at least have a working solution for now.

v4's hot allows hot reloading React components in legacy applications such as AngularJS which I do have to deal with.

The v4 workaround is doing a full mount instead of shallow. This had the unintended side effect of mounting components that use Redux or React-Router down the chain. Unless I have an explicit need, I don't mock Redux when testing.

@theKashey theKashey self-assigned this Mar 18, 2018
@theKashey theKashey added the WIP label Mar 18, 2018
@theKashey
Copy link
Collaborator

Theoretically - is RHL working ok with mount, it shall work with shallow. Theoretically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants