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

State is always initialized under shallow #1849

Closed
abettadapur opened this issue Oct 2, 2018 · 7 comments
Closed

State is always initialized under shallow #1849

abettadapur opened this issue Oct 2, 2018 · 7 comments

Comments

@abettadapur
Copy link

Describe the bug
I have a component that does not initialize any state.

If I shallow mount this component, when render is called, this.state has a {} value, even though I did not initialize it.

Repro:

const Enzyme = require("enzyme");
const React = require("react");
class Foo extends React.Component {
    constructor(props, context) {
        super(props, context);
    }
    render() {
        console.log(`State: ${JSON.stringify(this.state)}`); // prints State: {}
        return (React.createElement("div", null));
    }
}
it("Test render", () => {
    Enzyme.shallow(React.createElement(Foo, null));
});

Expected behavior
this.state should be undefined

** Versions **
Enzyme: 3.3.2
Enzyme-Adapter-React-16: 1.5.0

@abettadapur abettadapur changed the title State is always initialized State is always initialized under shallow Oct 2, 2018
@ljharb
Copy link
Member

ljharb commented Oct 2, 2018

See https://jsfiddle.net/69z2wepo/310645/ - it looks like this.state starts out null when you don't specify it.

I've added some tests that verify this, which seem to be passing locally.

Can you try updating to the latest enzyme (v3.6.0)?

@abettadapur
Copy link
Author

I will upgrade and see.
The state may not be undefined, but it should not be {}

@ljharb
Copy link
Member

ljharb commented Oct 2, 2018

Very true; it looks like actually in React 16.0 - 16.3 this might be the case (please fill out the entire issue template). What React version are you using?

@ljharb
Copy link
Member

ljharb commented Oct 2, 2018

Turns out this is a bug in the React shallow renderer, fixed in facebook/react#11965.

I'll see if I can fix it in the 16.0, 16.1, and 16.2 adapters.

@ljharb ljharb closed this as completed in e18bb65 Oct 2, 2018
@abettadapur
Copy link
Author

@ljharb Thanks for the quick turnaround

Just to close the loop, when will the fix appear in the adapter packages?

@ljharb
Copy link
Member

ljharb commented Oct 3, 2018

The next release will include it; hopefully it will be in the next couple days.

@abettadapur
Copy link
Author

@ljharb Thanks! Appreciate the quick response

ljharb added a commit that referenced this issue Oct 5, 2018
 - [new] add `isCustomComponentElement` (#1832)
 - [fix] (<= v16.2) ensure that `this.state` starts out `null` when unspecified on a custom component (#1849)
 - [fix] `forwardRef`: respect `.displayName` on the forwardRef (#1817)
 - [deps] update `babel-preset-airbnb`, `chai`, `eslint`, `mocha`, `enzyme-adapter-utils`, `react-is`, `airbnb-js-shims`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants